Guest User

Node.js cluster benchmark using stream write

a guest
Sep 11th, 2014
325
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.  
  7. if (cluster.isMaster) {
  8.     // Fork workers.
  9.     for (var i = 0; i < numCPUs; i++) {
  10.         cluster.fork();
  11.     }
  12.  
  13.     cluster.on('exit', function (worker, code, signal) {
  14.         console.log('worker ' + worker.process.pid + ' died');
  15.     });
  16. } else {
  17.     http.createServer(function (req, res) {
  18.         res.writeHead(200, {'Content-Type': 'text/html'});
  19.         connection.query('SELECT * FROM users LIMIT 100;', function (err, rows) {
  20.             if (err) {
  21.                 throw new Error('Connection failed');
  22.             }
  23.             var table = '<table><tr><th>id</th><th>username</th><th>password</th><th>banking account</th><th>phone number</th></tr>';
  24.             res.write(table);
  25.  
  26.             for (var i = 0; i < rows.length; i++) {
  27.                 res.write('<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>');
  28.             }
  29.             res.write('</table><div style="color:red">Hello from cluster worker: ' + cluster.worker.id + '</div>');
  30.             res.end();
  31.  
  32.         });
  33.     }).listen(9615);
  34.     console.log("Server running at http://localhost:9615 on cluster worker number " + cluster.worker.id);
  35. }
  36.  
  37. ab -n 100000 -c 200 http://127.0.0.1:9615/
  38. This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
  39. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  40. Licensed to The Apache Software Foundation, http://www.apache.org/
  41.  
  42. Benchmarking 127.0.0.1 (be patient)
  43. Completed 10000 requests
  44. Completed 20000 requests
  45. Completed 30000 requests
  46. Completed 40000 requests
  47. Completed 50000 requests
  48. Completed 60000 requests
  49. Completed 70000 requests
  50. Completed 80000 requests
  51. Completed 90000 requests
  52. Completed 100000 requests
  53. Finished 100000 requests
  54.  
  55.  
  56. Server Software:        
  57. Server Hostname:        127.0.0.1
  58. Server Port:            9615
  59.  
  60. Document Path:          /
  61. Document Length:        10939 bytes
  62.  
  63. Concurrency Level:      200
  64. Time taken for tests:   34.954 seconds
  65. Complete requests:      100000
  66. Failed requests:        0
  67. Total transferred:      1103900000 bytes
  68. HTML transferred:       1093900000 bytes
  69. Requests per second:    2860.90 [#/sec] (mean)
  70. Time per request:       69.908 [ms] (mean)
  71. Time per request:       0.350 [ms] (mean, across all concurrent requests)
  72. Transfer rate:          30841.23 [Kbytes/sec] received
  73.  
  74. Connection Times (ms)
  75.               min  mean[+/-sd] median   max
  76. Connect:        0    0   0.1      0       6
  77. Processing:     1   70  46.1     60     621
  78. Waiting:        0   69  45.7     59     611
  79. Total:          1   70  46.1     60     621
  80.  
  81. Percentage of the requests served within a certain time (ms)
  82.   50%     60
  83.   66%     70
  84.   75%     79
  85.   80%     86
  86.   90%    108
  87.   95%    135
  88.   98%    219
  89.   99%    273
  90.  100%    621 (longest request)
Add Comment
Please, Sign In to add comment