4 * Copyright 2016 gRPC authors.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 include_once 'interop_client.php';
22 function stress_main($args)
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";
34 socket_listen($socket);
35 socket_set_nonblock($socket);
37 $start_time = microtime(true);
39 $deadline = $args['test_duration_secs'] ?
40 ($start_time + $args['test_duration_secs']) : false;
41 $num_test_cases = count($args['test_cases']);
45 $current_time = microtime(true);
46 if ($deadline && $current_time > $deadline) {
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);
56 // do actual work, run one interop test case
58 $args['test_cases'][mt_rand(0, $num_test_cases - 1)];
59 $stub = @interop_main($args, $stub);
63 socket_close($socket);
64 echo "Number of interop tests run in $args[test_duration_secs] ".
68 // process command line arguments
69 $raw_args = getopt('',
70 ['server_addresses::',
73 'test_duration_secs::',
74 'num_channels_per_server::',
75 'num_stubs_per_channel::',
80 if (empty($raw_args['server_addresses'])) {
81 $args['server_host'] = 'localhost';
82 $args['server_port'] = '8080';
84 $parts = explode(':', $raw_args['server_addresses']);
85 $args['server_host'] = $parts[0];
86 $args['server_port'] = (count($parts) == 2) ? $parts[1] : '';
89 $args['metrics_port'] = empty($raw_args['metrics_port']) ?
90 '8081' : $raw_args['metrics_port'];
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'];
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]));
102 $args['test_cases'] = $test_cases;