Advertisement
Guest User

Simple Node.js Cluster database access code

a guest
Sep 11th, 2014
467
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 async = require('async');
  4. var connection = mysql.createConnection({ user: 'benchmark', database: 'benchmark', password: 'benchpass'});
  5. var cluster = require('cluster');
  6. var numCPUs = require('os').cpus().length;
  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.             var table = '<table><tr><th>id</th><th>username</th><th>password</th><th>banking account</th><th>phone number</th></tr>';
  25.             async.forEach(rows, function (row, callback) {
  26.                 table += '<tr><td>' + row.id + '</td><td>' + row.username + '</td><td>' + row.password + '</td><td>' + row.banking_account + '</td><td>' + row.phone_number + '</td></tr>';
  27.                 callback();
  28.             }, function (err) {
  29.                 if (err) {
  30.                     throw new Error('Async failed!');
  31.                 }
  32.                 table += '</table>';
  33.                 table += '<div style="color:red">Hello from cluster worker: ' + cluster.worker.id + '</div>';
  34.                 res.end(table);
  35.             });
  36.         });
  37.     }).listen(9615);
  38.     console.log("Server running at http://localhost:9615 on cluster worker number " + cluster.worker.id);
  39. }
  40.  
  41. Benchmark results:
  42. ab -n 100000 -c 200 http://127.0.0.1:9615/
  43. This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
  44. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  45. Licensed to The Apache Software Foundation, http://www.apache.org/
  46.  
  47. Benchmarking 127.0.0.1 (be patient)
  48. Completed 10000 requests
  49. Completed 20000 requests
  50. Completed 30000 requests
  51. Completed 40000 requests
  52. Completed 50000 requests
  53. Completed 60000 requests
  54. Completed 70000 requests
  55. Completed 80000 requests
  56. Completed 90000 requests
  57. Completed 100000 requests
  58. Finished 100000 requests
  59.  
  60.  
  61. Server Software:        
  62. Server Hostname:        127.0.0.1
  63. Server Port:            9615
  64.  
  65. Document Path:          /
  66. Document Length:        11001 bytes
  67.  
  68. Concurrency Level:      200
  69. Time taken for tests:   11.692 seconds
  70. Complete requests:      100000
  71. Failed requests:        0
  72. Total transferred:      1110100000 bytes
  73. HTML transferred:       1100100000 bytes
  74. Requests per second:    8553.03 [#/sec] (mean)
  75. Time per request:       23.384 [ms] (mean)
  76. Time per request:       0.117 [ms] (mean, across all concurrent requests)
  77. Transfer rate:          92721.84 [Kbytes/sec] received
  78.  
  79. Connection Times (ms)
  80.               min  mean[+/-sd] median   max
  81. Connect:        0    0   0.1      0       3
  82. Processing:     0   23  16.8     20     113
  83. Waiting:        0   23  16.8     20     113
  84. Total:          0   23  16.8     20     113
  85.  
  86. Percentage of the requests served within a certain time (ms)
  87.   50%     20
  88.   66%     28
  89.   75%     34
  90.   80%     37
  91.   90%     45
  92.   95%     52
  93.   98%     63
  94.   99%     80
  95.  100%    113 (longest request)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement