worker_service_impl.js 5.2 KB

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