Guest User

Node.js cluster streaming benchmark

a guest
Sep 11th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2. var mysql = require('mysql2');
  3. var connection = mysql.createConnection({ user: 'benchmark', database: 'benchmark', password: 'benchpass'});
  4. var cluster = require('cluster');
  5. var numCPUs = require('os').cpus().length;
  6. var Readable = require('stream').Readable;
  7.  
  8. if (cluster.isMaster) {
  9.     // Fork workers.
  10.     for (var i = 0; i < numCPUs; i++) {
  11.         cluster.fork();
  12.     }
  13.  
  14.     cluster.on('exit', function (worker, code, signal) {
  15.         console.log('worker ' + worker.process.pid + ' died');
  16.     });
  17. } else {
  18.     http.createServer(function (req, res) {
  19.         res.writeHead(200, {'Content-Type': 'text/html'});
  20.         connection.query('SELECT * FROM users LIMIT 100;', function (err, rows) {
  21.             if (err) {
  22.                 throw new Error('Connection failed');
  23.             }
  24.  
  25.             var rs = new Readable;
  26.             rs._read = function () {};
  27.  
  28.             rs.pipe(res);
  29.  
  30.             rs.push('<table><tr><th>id</th><th>username</th><th>password</th><th>banking account</th><th>phone number</th></tr>');
  31.  
  32.             for (var i = 0; i < rows.length; i++) {
  33.                 rs.push('<tr><td>' + rows[i].id + '</td><td>' + rows[i].username + '</td><td>' + rows[i].password + '</td><td>' + rows[i].banking_account + '</td><td>' + rows[i].phone + '</td></tr>');
  34.             }
  35.             rs.push('</table><div style="color:red">Hello from cluster worker: ' + cluster.worker.id + '</div>');
  36.             rs.push(null);
  37.         });
  38.     }).listen(9615);
  39.     console.log("Server running at http://localhost:9615 on cluster worker number " + cluster.worker.id);
  40. }
  41.  
  42.  
  43. ab -n 100000 -c 200 http://127.0.0.1:9615/
  44. This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
  45. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  46. Licensed to The Apache Software Foundation, http://www.apache.org/
  47.  
  48. Benchmarking 127.0.0.1 (be patient)
  49. Completed 10000 requests
  50. Completed 20000 requests
  51. Completed 30000 requests
  52. Completed 40000 requests
  53. Completed 50000 requests
  54. Completed 60000 requests
  55. Completed 70000 requests
  56. Completed 80000 requests
  57. Completed 90000 requests
  58. Completed 100000 requests
  59. Finished 100000 requests
  60.  
  61.  
  62. Server Software:        
  63. Server Hostname:        127.0.0.1
  64. Server Port:            9615
  65.  
  66. Document Path:          /
  67. Document Length:        10939 bytes
  68.  
  69. Concurrency Level:      200
  70. Time taken for tests:   46.781 seconds
  71. Complete requests:      100000
  72. Failed requests:        0
  73. Total transferred:      1103900000 bytes
  74. HTML transferred:       1093900000 bytes
  75. Requests per second:    2137.63 [#/sec] (mean)
  76. Time per request:       93.562 [ms] (mean)
  77. Time per request:       0.468 [ms] (mean, across all concurrent requests)
  78. Transfer rate:          23044.23 [Kbytes/sec] received
  79.  
  80. Connection Times (ms)
  81.               min  mean[+/-sd] median   max
  82. Connect:        0    0   0.1      0       5
  83. Processing:     1   93  74.0     74     717
  84. Waiting:        0   92  73.7     73     712
  85. Total:          1   93  74.0     74     717
  86.  
  87. Percentage of the requests served within a certain time (ms)
  88.   50%     74
  89.   66%     97
  90.   75%    117
  91.   80%    131
  92.   90%    178
  93.   95%    232
  94.   98%    303
  95.   99%    376
  96.  100%    717 (longest request)
Add Comment
Please, Sign In to add comment