stress_client.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. *
  4. * Copyright 2016, Google Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following disclaimer
  15. * in the documentation and/or other materials provided with the
  16. * distribution.
  17. * * Neither the name of Google Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. include_once 'interop_client.php';
  35. function stress_main($args)
  36. {
  37. mt_srand();
  38. set_time_limit(0);
  39. // open socket to listen as metrics server
  40. $socket = socket_create(AF_INET, SOCK_STREAM, 0);
  41. socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
  42. if (@!socket_bind($socket, 'localhost', $args['metrics_port'])) {
  43. echo "Cannot create socket for metrics server...\n";
  44. exit(1);
  45. }
  46. socket_listen($socket);
  47. socket_set_nonblock($socket);
  48. $start_time = microtime(true);
  49. $count = 0;
  50. $deadline = $args['test_duration_secs'] ?
  51. ($start_time + $args['test_duration_secs']) : false;
  52. $num_test_cases = count($args['test_cases']);
  53. $stub = false;
  54. while (true) {
  55. $current_time = microtime(true);
  56. if ($deadline && $current_time > $deadline) {
  57. break;
  58. }
  59. if ($client_connection = socket_accept($socket)) {
  60. // there is an incoming request, respond with qps metrics
  61. $input = socket_read($client_connection, 1024);
  62. $qps = round($count / ($current_time - $start_time));
  63. socket_write($client_connection, "qps: $qps");
  64. socket_close($client_connection);
  65. } else {
  66. // do actual work, run one interop test case
  67. $args['test_case'] =
  68. $args['test_cases'][mt_rand(0, $num_test_cases - 1)];
  69. $stub = @interop_main($args, $stub);
  70. ++$count;
  71. }
  72. }
  73. socket_close($socket);
  74. echo "Number of interop tests run in $args[test_duration_secs] ".
  75. "seconds: $count.\n";
  76. }
  77. // process command line arguments
  78. $raw_args = getopt('',
  79. ['server_addresses::',
  80. 'test_cases:',
  81. 'metrics_port::',
  82. 'test_duration_secs::',
  83. 'num_channels_per_server::',
  84. 'num_stubs_per_channel::',
  85. ]);
  86. $args = [];
  87. if (empty($raw_args['server_addresses'])) {
  88. $args['server_host'] = 'localhost';
  89. $args['server_port'] = '8080';
  90. } else {
  91. $parts = explode(':', $raw_args['server_addresses']);
  92. $args['server_host'] = $parts[0];
  93. $args['server_port'] = (count($parts) == 2) ? $parts[1] : '';
  94. }
  95. $args['metrics_port'] = empty($raw_args['metrics_port']) ?
  96. '8081' : $args['metrics_port'];
  97. $args['test_duration_secs'] = empty($raw_args['test_duration_secs']) ||
  98. $raw_args['test_duration_secs'] == -1 ?
  99. false : $raw_args['test_duration_secs'];
  100. $test_cases = [];
  101. $test_case_strs = explode(',', $raw_args['test_cases']);
  102. foreach ($test_case_strs as $test_case_str) {
  103. $parts = explode(':', $test_case_str);
  104. $test_cases = array_merge($test_cases, array_fill(0, $parts[1], $parts[0]));
  105. }
  106. $args['test_cases'] = $test_cases;
  107. stress_main($args);