stress_client.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. 'use strict';
  34. var _ = require('lodash');
  35. var grpc = require('../../..');
  36. var interop_client = require('../interop/interop_client');
  37. var MetricsServer = require('./metrics_server');
  38. var running;
  39. var metrics_server;
  40. var start_time;
  41. var query_count;
  42. function makeCall(client, test_cases) {
  43. if (!running) {
  44. return;
  45. }
  46. var test_case = test_cases[_.random(test_cases.length - 1)];
  47. interop_client.test_cases[test_case].run(client, function() {
  48. query_count += 1;
  49. makeCall(client, test_cases);
  50. });
  51. }
  52. function makeCalls(client, test_cases, parallel_calls_per_channel) {
  53. _.times(parallel_calls_per_channel, function() {
  54. makeCall(client, test_cases);
  55. });
  56. }
  57. function getQps() {
  58. var diff = process.hrtime(start_time);
  59. var seconds = diff[0] + diff[1] / 1e9;
  60. return {long_value: query_count / seconds};
  61. }
  62. function start(server_addresses, test_cases, channels_per_server,
  63. parallel_calls_per_channel, metrics_port) {
  64. running = true;
  65. /* Assuming that we are not calling unimplemented_method. The client class
  66. * used by empty_unary is (currently) the client class used by every interop
  67. * test except unimplemented_method */
  68. var Client = interop_client.test_cases.empty_unary.Client;
  69. /* Make channels_per_server clients connecting to each server address */
  70. var channels = _.flatten(_.times(
  71. channels_per_server, _.partial(_.map, server_addresses, function(address) {
  72. return new Client(address, grpc.credentials.createInsecure());
  73. })));
  74. metrics_server = new MetricsServer(metrics_port);
  75. metrics_server.registerGauge('qps', getQps);
  76. start_time = process.hrtime();
  77. query_count = 0;
  78. _.each(channels, _.partial(makeCalls, _, test_cases,
  79. parallel_calls_per_channel));
  80. metrics_server.start();
  81. }
  82. function stop() {
  83. running = false;
  84. metrics_server.shutdown();
  85. console.log('QPS: ' + getQps().long_value);
  86. }
  87. function main() {
  88. var parseArgs = require('minimist');
  89. var argv = parseArgs(process.argv, {
  90. string: ['server_addresses', 'test_cases', 'metrics_port'],
  91. default: {'server_addresses': 'localhost:8080',
  92. 'test_duration_secs': -1,
  93. 'num_channels_per_server': 1,
  94. 'num_stubs_per_channel': 1,
  95. 'metrics_port': '8081'}
  96. });
  97. var server_addresses = argv.server_addresses.split(',');
  98. /* Generate an array of test cases, where the number of instances of each name
  99. * corresponds to the number given in the argument.
  100. * e.g. 'empty_unary:1,large_unary:2' =>
  101. * ['empty_unary', 'large_unary', 'large_unary'] */
  102. var test_cases = _.flatten(_.map(argv.test_cases.split(','), function(value) {
  103. var split = value.split(':');
  104. return _.times(split[1], _.constant(split[0]));
  105. }));
  106. start(server_addresses, test_cases, argv.num_channels_per_server,
  107. argv.num_stubs_per_channel, argv.metrics_port);
  108. if (argv.test_duration_secs > -1) {
  109. setTimeout(stop, argv.test_duration_secs * 1000);
  110. }
  111. }
  112. main();