Advertisement
Guest User

Refactored Node.js Cluster benchmark with for loop & results

a guest
Sep 11th, 2014
368
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.  
  25.             for (var i = 0; i < rows.length; i++) {
  26.                 table += '<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>';
  27.             }
  28.  
  29.             table += '</table>';
  30.             table += '<div style="color:red">Hello from cluster worker: ' + cluster.worker.id + '</div>';
  31.  
  32.             res.end(table);
  33.  
  34.         });
  35.     }).listen(9615);
  36.     console.log("Server running at http://localhost:9615 on cluster worker number " + cluster.worker.id);
  37. }
  38.  
  39. ab -n 100000 -c 200 http://127.0.0.1:9615/
  40. This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
  41. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  42. Licensed to The Apache Software Foundation, http://www.apache.org/
  43.  
  44. Benchmarking 127.0.0.1 (be patient)
  45. Completed 10000 requests
  46. Completed 20000 requests
  47. Completed 30000 requests
  48. Completed 40000 requests
  49. Completed 50000 requests
  50. Completed 60000 requests
  51. Completed 70000 requests
  52. Completed 80000 requests
  53. Completed 90000 requests
  54. Completed 100000 requests
  55. Finished 100000 requests
  56.  
  57.  
  58. Server Software:        
  59. Server Hostname:        127.0.0.1
  60. Server Port:            9615
  61.  
  62. Document Path:          /
  63. Document Length:        10939 bytes
  64.  
  65. Concurrency Level:      200
  66. Time taken for tests:   10.900 seconds
  67. Complete requests:      100000
  68. Failed requests:        0
  69. Total transferred:      1103900000 bytes
  70. HTML transferred:       1093900000 bytes
  71. Requests per second:    9174.47 [#/sec] (mean)
  72. Time per request:       21.800 [ms] (mean)
  73. Time per request:       0.109 [ms] (mean, across all concurrent requests)
  74. Transfer rate:          98903.28 [Kbytes/sec] received
  75.  
  76. Connection Times (ms)
  77.               min  mean[+/-sd] median   max
  78. Connect:        0    0  15.5      0     998
  79. Processing:     0   21  18.3     16     115
  80. Waiting:        0   21  18.3     16     115
  81. Total:          0   22  23.9     16    1049
  82.  
  83. Percentage of the requests served within a certain time (ms)
  84.   50%     16
  85.   66%     23
  86.   75%     30
  87.   80%     35
  88.   90%     48
  89.   95%     57
  90.   98%     73
  91.   99%     87
  92.  100%   1049 (longest request)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement