stress_client.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. 'use strict';
  19. var _ = require('lodash');
  20. var grpc = require('../../..');
  21. var interop_client = require('../interop/interop_client');
  22. var MetricsServer = require('./metrics_server');
  23. var running;
  24. var metrics_server;
  25. var start_time;
  26. var query_count;
  27. function makeCall(client, test_cases) {
  28. if (!running) {
  29. return;
  30. }
  31. var test_case = test_cases[_.random(test_cases.length - 1)];
  32. interop_client.test_cases[test_case].run(client, function() {
  33. query_count += 1;
  34. makeCall(client, test_cases);
  35. });
  36. }
  37. function makeCalls(client, test_cases, parallel_calls_per_channel) {
  38. _.times(parallel_calls_per_channel, function() {
  39. makeCall(client, test_cases);
  40. });
  41. }
  42. function getQps() {
  43. var diff = process.hrtime(start_time);
  44. var seconds = diff[0] + diff[1] / 1e9;
  45. return {long_value: query_count / seconds};
  46. }
  47. function start(server_addresses, test_cases, channels_per_server,
  48. parallel_calls_per_channel, metrics_port) {
  49. running = true;
  50. /* Assuming that we are not calling unimplemented_method. The client class
  51. * used by empty_unary is (currently) the client class used by every interop
  52. * test except unimplemented_method */
  53. var Client = interop_client.test_cases.empty_unary.Client;
  54. /* Make channels_per_server clients connecting to each server address */
  55. var channels = _.flatten(_.times(
  56. channels_per_server, _.partial(_.map, server_addresses, function(address) {
  57. return new Client(address, grpc.credentials.createInsecure());
  58. })));
  59. metrics_server = new MetricsServer(metrics_port);
  60. metrics_server.registerGauge('qps', getQps);
  61. start_time = process.hrtime();
  62. query_count = 0;
  63. _.each(channels, _.partial(makeCalls, _, test_cases,
  64. parallel_calls_per_channel));
  65. metrics_server.start();
  66. }
  67. function stop() {
  68. running = false;
  69. metrics_server.shutdown();
  70. console.log('QPS: ' + getQps().long_value);
  71. }
  72. function main() {
  73. var parseArgs = require('minimist');
  74. var argv = parseArgs(process.argv, {
  75. string: ['server_addresses', 'test_cases', 'metrics_port'],
  76. default: {'server_addresses': 'localhost:8080',
  77. 'test_duration_secs': -1,
  78. 'num_channels_per_server': 1,
  79. 'num_stubs_per_channel': 1,
  80. 'metrics_port': '8081'}
  81. });
  82. var server_addresses = argv.server_addresses.split(',');
  83. /* Generate an array of test cases, where the number of instances of each name
  84. * corresponds to the number given in the argument.
  85. * e.g. 'empty_unary:1,large_unary:2' =>
  86. * ['empty_unary', 'large_unary', 'large_unary'] */
  87. var test_cases = _.flatten(_.map(argv.test_cases.split(','), function(value) {
  88. var split = value.split(':');
  89. return _.times(split[1], _.constant(split[0]));
  90. }));
  91. start(server_addresses, test_cases, argv.num_channels_per_server,
  92. argv.num_stubs_per_channel, argv.metrics_port);
  93. if (argv.test_duration_secs > -1) {
  94. setTimeout(stop, argv.test_duration_secs * 1000);
  95. }
  96. }
  97. main();