worker_service_impl.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. *
  3. * Copyright 2015-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 os = require('os');
  35. var BenchmarkClient = require('./benchmark_client');
  36. var BenchmarkServer = require('./benchmark_server');
  37. exports.quitWorker = function quitWorker(call, callback) {
  38. callback(null, {});
  39. process.exit(0);
  40. }
  41. exports.runClient = function runClient(call) {
  42. var client;
  43. call.on('data', function(request) {
  44. var stats;
  45. switch (request.argtype) {
  46. case 'setup':
  47. var setup = request.setup;
  48. client = new BenchmarkClient(setup.server_targets,
  49. setup.client_channels,
  50. setup.histogram_params,
  51. setup.security_params);
  52. client.on('error', function(error) {
  53. call.emit('error', error);
  54. });
  55. switch (setup.load_params.load) {
  56. case 'closed_loop':
  57. client.startClosedLoop(setup.outstanding_rpcs_per_channel,
  58. setup.rpc_type,
  59. setup.payload_config.simple_params.req_size,
  60. setup.payload_config.simple_params.resp_size);
  61. break;
  62. case 'poisson':
  63. client.startPoisson(setup.outstanding_rpcs_per_channel,
  64. setup.rpc_type, setup.payload_config.req_size,
  65. setup.payload_config.resp_size,
  66. setup.load_params.poisson.offered_load);
  67. break;
  68. default:
  69. call.emit('error', new Error('Unsupported LoadParams type' +
  70. setup.load_params.load));
  71. }
  72. stats = client.mark();
  73. call.write({
  74. stats: stats
  75. });
  76. break;
  77. case 'mark':
  78. if (client) {
  79. stats = client.mark(request.mark.reset);
  80. call.write({
  81. stats: stats
  82. });
  83. } else {
  84. call.emit('error', new Error('Got Mark before ClientConfig'));
  85. }
  86. break;
  87. default:
  88. throw new Error('Nonexistent client argtype option: ' + request.argtype);
  89. }
  90. });
  91. call.on('end', function() {
  92. client.stop(function() {
  93. call.end();
  94. });
  95. });
  96. };
  97. exports.runServer = function runServer(call) {
  98. var server;
  99. call.on('data', function(request) {
  100. var stats;
  101. switch (request.argtype) {
  102. case 'setup':
  103. server = new BenchmarkServer('[::]', request.setup.port,
  104. request.setup.security_params);
  105. server.start();
  106. stats = server.mark();
  107. call.write({
  108. stats: stats,
  109. port: server.getPort()
  110. });
  111. break;
  112. case 'mark':
  113. if (server) {
  114. stats = server.mark(request.mark.reset);
  115. call.write({
  116. stats: stats,
  117. port: server.getPort(),
  118. cores: 1
  119. });
  120. } else {
  121. call.emit('error', new Error('Got Mark before ServerConfig'));
  122. }
  123. break;
  124. default:
  125. throw new Error('Nonexistent server argtype option');
  126. }
  127. });
  128. call.on('end', function() {
  129. server.stop(function() {
  130. call.end();
  131. });
  132. });
  133. };
  134. exports.coreCount = function coreCount(call, callback) {
  135. callback(null, {cores: os.cpus().length});
  136. };