Debian Codegraph @

src/php/tests/interop/stress_client.php

raw
1 <?php
2 /*
3 *
4 * Copyright 2016 gRPC authors.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 include_once 'interop_client.php';
21
22 function stress_main($args)
23 {
24 mt_srand();
25 set_time_limit(0);
26
27 // open socket to listen as metrics server
28 $socket = socket_create(AF_INET, SOCK_STREAM, 0);
29 socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
30 if (@!socket_bind($socket, 'localhost', $args['metrics_port'])) {
31 echo "Cannot create socket for metrics server...\n";
32 exit(1);
33 }
34 socket_listen($socket);
35 socket_set_nonblock($socket);
36
37 $start_time = microtime(true);
38 $count = 0;
39 $deadline = $args['test_duration_secs'] ?
40 ($start_time + $args['test_duration_secs']) : false;
41 $num_test_cases = count($args['test_cases']);
42 $stub = false;
43
44 while (true) {
45 $current_time = microtime(true);
46 if ($deadline && $current_time > $deadline) {
47 break;
48 }
49 if ($client_connection = socket_accept($socket)) {
50 // there is an incoming request, respond with qps metrics
51 $input = socket_read($client_connection, 1024);
52 $qps = round($count / ($current_time - $start_time));
53 socket_write($client_connection, "qps: $qps");
54 socket_close($client_connection);
55 } else {
56 // do actual work, run one interop test case
57 $args['test_case'] =
58 $args['test_cases'][mt_rand(0, $num_test_cases - 1)];
59 $stub = @interop_main($args, $stub);
60 ++$count;
61 }
62 }
63 socket_close($socket);
64 echo "Number of interop tests run in $args[test_duration_secs] ".
65 "seconds: $count.\n";
66 }
67
68 // process command line arguments
69 $raw_args = getopt('',
70 ['server_addresses::',
71 'test_cases:',
72 'metrics_port::',
73 'test_duration_secs::',
74 'num_channels_per_server::',
75 'num_stubs_per_channel::',
76 ]);
77
78 $args = [];
79
80 if (empty($raw_args['server_addresses'])) {
81 $args['server_host'] = 'localhost';
82 $args['server_port'] = '8080';
83 } else {
84 $parts = explode(':', $raw_args['server_addresses']);
85 $args['server_host'] = $parts[0];
86 $args['server_port'] = (count($parts) == 2) ? $parts[1] : '';
87 }
88
89 $args['metrics_port'] = empty($raw_args['metrics_port']) ?
90 '8081' : $raw_args['metrics_port'];
91
92 $args['test_duration_secs'] = empty($raw_args['test_duration_secs']) ||
93 $raw_args['test_duration_secs'] == -1 ?
94 false : $raw_args['test_duration_secs'];
95
96 $test_cases = [];
97 $test_case_strs = explode(',', $raw_args['test_cases']);
98 foreach ($test_case_strs as $test_case_str) {
99 $parts = explode(':', $test_case_str);
100 $test_cases = array_merge($test_cases, array_fill(0, $parts[1], $parts[0]));
101 }
102 $args['test_cases'] = $test_cases;
103
104 stress_main($args);