benchmark_server_express.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /**
  19. * Benchmark server module
  20. * @module
  21. */
  22. 'use strict';
  23. var fs = require('fs');
  24. var path = require('path');
  25. var http = require('http');
  26. var https = require('https');
  27. var EventEmitter = require('events');
  28. var util = require('util');
  29. var express = require('express');
  30. var bodyParser = require('body-parser');
  31. function unaryCall(req, res) {
  32. var reqObj = req.body;
  33. var payload = {body: '0'.repeat(reqObj.response_size)};
  34. res.json(payload);
  35. }
  36. function BenchmarkServer(host, port, tls, generic, response_size) {
  37. var app = express();
  38. app.use(bodyParser.json());
  39. app.put('/serviceProto.BenchmarkService.service/unaryCall', unaryCall);
  40. this.input_host = host;
  41. this.input_port = port;
  42. if (tls) {
  43. var credentials = {};
  44. var key_path = path.join(__dirname, '../test/data/server1.key');
  45. var pem_path = path.join(__dirname, '../test/data/server1.pem');
  46. var key_data = fs.readFileSync(key_path);
  47. var pem_data = fs.readFileSync(pem_path);
  48. credentials['key'] = key_data;
  49. credentials['cert'] = pem_data;
  50. this.server = https.createServer(credentials, app);
  51. } else {
  52. this.server = http.createServer(app);
  53. }
  54. }
  55. util.inherits(BenchmarkServer, EventEmitter);
  56. BenchmarkServer.prototype.start = function() {
  57. var self = this;
  58. this.server.listen(this.input_port, this.input_hostname, function() {
  59. self.last_wall_time = process.hrtime();
  60. self.last_usage = process.cpuUsage();
  61. self.emit('started');
  62. });
  63. };
  64. BenchmarkServer.prototype.getPort = function() {
  65. return this.server.address().port;
  66. };
  67. BenchmarkServer.prototype.mark = function(reset) {
  68. var wall_time_diff = process.hrtime(this.last_wall_time);
  69. var usage_diff = process.cpuUsage(this.last_usage);
  70. if (reset) {
  71. this.last_wall_time = process.hrtime();
  72. this.last_usage = process.cpuUsage();
  73. }
  74. return {
  75. time_elapsed: wall_time_diff[0] + wall_time_diff[1] / 1e9,
  76. time_user: usage_diff.user / 1000000,
  77. time_system: usage_diff.system / 1000000
  78. };
  79. };
  80. BenchmarkServer.prototype.stop = function(callback) {
  81. this.server.close(callback);
  82. };
  83. module.exports = BenchmarkServer;