OLD | NEW |
(Empty) | |
| 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 |
| 34 /** |
| 35 * Benchmark server module |
| 36 * @module |
| 37 */ |
| 38 |
| 39 'use strict'; |
| 40 |
| 41 var fs = require('fs'); |
| 42 var path = require('path'); |
| 43 |
| 44 var grpc = require('../../../'); |
| 45 var serviceProto = grpc.load({ |
| 46 root: __dirname + '/../../..', |
| 47 file: 'src/proto/grpc/testing/services.proto'}).grpc.testing; |
| 48 |
| 49 /** |
| 50 * Create a buffer filled with size zeroes |
| 51 * @param {number} size The length of the buffer |
| 52 * @return {Buffer} The new buffer |
| 53 */ |
| 54 function zeroBuffer(size) { |
| 55 var zeros = new Buffer(size); |
| 56 zeros.fill(0); |
| 57 return zeros; |
| 58 } |
| 59 |
| 60 /** |
| 61 * Handler for the unary benchmark method. Simply responds with a payload |
| 62 * containing the requested number of zero bytes. |
| 63 * @param {Call} call The call object to be handled |
| 64 * @param {function} callback The callback to call with the response |
| 65 */ |
| 66 function unaryCall(call, callback) { |
| 67 var req = call.request; |
| 68 var payload = {body: zeroBuffer(req.response_size)}; |
| 69 callback(null, {payload: payload}); |
| 70 } |
| 71 |
| 72 /** |
| 73 * Handler for the streaming benchmark method. Simply responds to each request |
| 74 * with a payload containing the requested number of zero bytes. |
| 75 * @param {Call} call The call object to be handled |
| 76 */ |
| 77 function streamingCall(call) { |
| 78 call.on('data', function(value) { |
| 79 var payload = {body: zeroBuffer(value.response_size)}; |
| 80 call.write({payload: payload}); |
| 81 }); |
| 82 call.on('end', function() { |
| 83 call.end(); |
| 84 }); |
| 85 } |
| 86 |
| 87 /** |
| 88 * BenchmarkServer class. Constructed based on parameters from the driver and |
| 89 * stores statistics. |
| 90 * @param {string} host The host to serve on |
| 91 * @param {number} port The port to listen to |
| 92 * @param {tls} Indicates whether TLS should be used |
| 93 */ |
| 94 function BenchmarkServer(host, port, tls) { |
| 95 var server_creds; |
| 96 var host_override; |
| 97 if (tls) { |
| 98 var key_path = path.join(__dirname, '../test/data/server1.key'); |
| 99 var pem_path = path.join(__dirname, '../test/data/server1.pem'); |
| 100 |
| 101 var key_data = fs.readFileSync(key_path); |
| 102 var pem_data = fs.readFileSync(pem_path); |
| 103 server_creds = grpc.ServerCredentials.createSsl(null, |
| 104 [{private_key: key_data, |
| 105 cert_chain: pem_data}]); |
| 106 } else { |
| 107 server_creds = grpc.ServerCredentials.createInsecure(); |
| 108 } |
| 109 |
| 110 var server = new grpc.Server(); |
| 111 this.port = server.bind(host + ':' + port, server_creds); |
| 112 server.addProtoService(serviceProto.BenchmarkService.service, { |
| 113 unaryCall: unaryCall, |
| 114 streamingCall: streamingCall |
| 115 }); |
| 116 this.server = server; |
| 117 } |
| 118 |
| 119 /** |
| 120 * Start the benchmark server. |
| 121 */ |
| 122 BenchmarkServer.prototype.start = function() { |
| 123 this.server.start(); |
| 124 this.last_wall_time = process.hrtime(); |
| 125 }; |
| 126 |
| 127 /** |
| 128 * Return the port number that the server is bound to. |
| 129 * @return {Number} The port number |
| 130 */ |
| 131 BenchmarkServer.prototype.getPort = function() { |
| 132 return this.port; |
| 133 }; |
| 134 |
| 135 /** |
| 136 * Return current statistics for the server. If reset is set, restart |
| 137 * statistic collection. |
| 138 * @param {boolean} reset Indicates that statistics should be reset |
| 139 * @return {object} Server statistics |
| 140 */ |
| 141 BenchmarkServer.prototype.mark = function(reset) { |
| 142 var wall_time_diff = process.hrtime(this.last_wall_time); |
| 143 if (reset) { |
| 144 this.last_wall_time = process.hrtime(); |
| 145 } |
| 146 return { |
| 147 time_elapsed: wall_time_diff[0] + wall_time_diff[1] / 1e9, |
| 148 // Not sure how to measure these values |
| 149 time_user: 0, |
| 150 time_system: 0 |
| 151 }; |
| 152 }; |
| 153 |
| 154 /** |
| 155 * Stop the server. |
| 156 * @param {function} callback Called when the server has finished shutting down |
| 157 */ |
| 158 BenchmarkServer.prototype.stop = function(callback) { |
| 159 this.server.tryShutdown(callback); |
| 160 }; |
| 161 |
| 162 module.exports = BenchmarkServer; |
OLD | NEW |