benchmark_server_express.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /**
  34. * Benchmark server module
  35. * @module
  36. */
  37. 'use strict';
  38. var fs = require('fs');
  39. var path = require('path');
  40. var http = require('http');
  41. var https = require('https');
  42. var EventEmitter = require('events');
  43. var util = require('util');
  44. var express = require('express');
  45. var bodyParser = require('body-parser')
  46. function unaryCall(req, res) {
  47. var reqObj = req.body;
  48. var payload = {body: '0'.repeat(reqObj.response_size)};
  49. res.json(payload);
  50. }
  51. function BenchmarkServer(host, port, tls, generic, response_size) {
  52. var app = express();
  53. app.use(bodyParser.json())
  54. app.put('/serviceProto.BenchmarkService.service/unaryCall', unaryCall);
  55. this.input_host = host;
  56. this.input_port = port;
  57. if (tls) {
  58. var credentials = {};
  59. var key_path = path.join(__dirname, '../test/data/server1.key');
  60. var pem_path = path.join(__dirname, '../test/data/server1.pem');
  61. var key_data = fs.readFileSync(key_path);
  62. var pem_data = fs.readFileSync(pem_path);
  63. credentials['key'] = key_data;
  64. credentials['cert'] = pem_data;
  65. this.server = https.createServer(credentials, app);
  66. } else {
  67. this.server = http.createServer(app);
  68. }
  69. }
  70. util.inherits(BenchmarkServer, EventEmitter);
  71. BenchmarkServer.prototype.start = function() {
  72. var self = this;
  73. this.server.listen(this.input_port, this.input_hostname, function() {
  74. self.last_wall_time = process.hrtime();
  75. self.emit('started');
  76. });
  77. };
  78. BenchmarkServer.prototype.getPort = function() {
  79. return this.server.address().port;
  80. };
  81. BenchmarkServer.prototype.mark = function(reset) {
  82. var wall_time_diff = process.hrtime(this.last_wall_time);
  83. if (reset) {
  84. this.last_wall_time = process.hrtime();
  85. }
  86. return {
  87. time_elapsed: wall_time_diff[0] + wall_time_diff[1] / 1e9,
  88. // Not sure how to measure these values
  89. time_user: 0,
  90. time_system: 0
  91. };
  92. };
  93. BenchmarkServer.prototype.stop = function(callback) {
  94. this.server.close(callback);
  95. };
  96. module.exports = BenchmarkServer;