You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.8 KiB

  1. var cluster = require('cluster')
  2. , WebSocket = require('../')
  3. , WebSocketServer = WebSocket.Server
  4. , crypto = require('crypto')
  5. , util = require('util')
  6. , ansi = require('ansi');
  7. require('tinycolor');
  8. function roundPrec(num, prec) {
  9. var mul = Math.pow(10, prec);
  10. return Math.round(num * mul) / mul;
  11. }
  12. function humanSize(bytes) {
  13. if (bytes >= 1048576) return roundPrec(bytes / 1048576, 2) + ' MB';
  14. if (bytes >= 1024) return roundPrec(bytes / 1024, 2) + ' kB';
  15. return roundPrec(bytes, 2) + ' B';
  16. }
  17. function generateRandomData(size) {
  18. var buffer = new Buffer(size);
  19. for (var i = 0; i < size; ++i) {
  20. buffer[i] = ~~(Math.random() * 127);
  21. }
  22. return buffer;
  23. }
  24. if (cluster.isMaster) {
  25. var wss = new WebSocketServer({port: 8181}, function() {
  26. cluster.fork();
  27. });
  28. wss.on('connection', function(ws) {
  29. ws.on('message', function(data, flags) {
  30. ws.send(data, {binary: flags&&flags.binary});
  31. });
  32. ws.on('close', function() {});
  33. });
  34. cluster.on('death', function(worker) {
  35. wss.close();
  36. });
  37. }
  38. else {
  39. var cursor = ansi(process.stdout);
  40. var configs = [
  41. [true, 10000, 64],
  42. [true, 5000, 16*1024],
  43. [true, 1000, 128*1024],
  44. [true, 100, 1024*1024],
  45. [true, 1, 500*1024*1024],
  46. [false, 10000, 64],
  47. [false, 5000, 16*1024],
  48. [false, 1000, 128*1024],
  49. [false, 100, 1024*1024],
  50. ];
  51. var largest = configs[0][1];
  52. for (var i = 0, l = configs.length; i < l; ++i) {
  53. if (configs[i][2] > largest) largest = configs[i][2];
  54. }
  55. console.log('Generating %s of test data ...', humanSize(largest));
  56. var randomBytes = generateRandomData(largest);
  57. function roundtrip(useBinary, roundtrips, size, cb) {
  58. var data = randomBytes.slice(0, size);
  59. var prefix = util.format('Running %d roundtrips of %s %s data', roundtrips, humanSize(size), useBinary ? 'binary' : 'text');
  60. console.log(prefix);
  61. var client = new WebSocket('ws://localhost:' + '8181');
  62. var dt;
  63. var roundtrip = 0;
  64. function send() {
  65. client.send(data, {binary: useBinary});
  66. }
  67. client.on('error', function(e) {
  68. console.error(e);
  69. process.exit();
  70. });
  71. client.on('open', function() {
  72. dt = Date.now();
  73. send();
  74. });
  75. client.on('message', function(data, flags) {
  76. if (++roundtrip == roundtrips) {
  77. var elapsed = Date.now() - dt;
  78. cursor.up();
  79. console.log('%s:\t%ss\t%s'
  80. , useBinary ? prefix.green : prefix.cyan
  81. , roundPrec(elapsed / 1000, 1).toString().green.bold
  82. , (humanSize((size * roundtrips) / elapsed * 1000) + '/s').blue.bold);
  83. client.close();
  84. cb();
  85. return;
  86. }
  87. process.nextTick(send);
  88. });
  89. }
  90. (function run() {
  91. if (configs.length == 0) process.exit();
  92. var config = configs.shift();
  93. config.push(run);
  94. roundtrip.apply(null, config);
  95. })();
  96. }