Skip to content

Instantly share code, notes, and snippets.

@einaros
Created June 17, 2012 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einaros/d53e8234ac692bcaad75 to your computer and use it in GitHub Desktop.
Save einaros/d53e8234ac692bcaad75 to your computer and use it in GitHub Desktop.

output

Sending 2 frames, each 2 byte header + 10485760 byte payload, using buffer merging strategy
Merging operation: 11ms
Merging operation: 10ms
Send duration: 11064ms
Sending 2 frames, each 2 byte header + 10485760 byte payload, using frame splitting strategy
Splitting operation: 0ms
Splitting operation: 0ms
Send duration: 11237ms

code

var net = require('net')
  , send = null
  , strategy = null;

if (process.argv[2] == 'm'){
  strategy = 'buffer merging';
  send = function(socket, data, cb) {
    console.time('Merging operation');
    var header = new Buffer('HI');
    var merged = new Buffer(data.length + header.length);
    header.copy(merged);
    data.copy(merged, header.length);
    socket.write(merged, cb);
    console.timeEnd('Merging operation');
  }
}
else if (process.argv[2] == 's') {
  strategy = 'frame splitting';
  send = function(socket, data, cb) {
    var header = new Buffer('HI');
    console.time('Splitting operation');
    socket.write(header);
    socket.write(data, cb);
    console.timeEnd('Splitting operation');
  }
}
else process.exit();

var socket = net.connect(8124, '10.90.90.10', function() {
  socket.setNoDelay(true);
  var count = 2;
  var size = 10*1024*1024;
  var completed = 0;
  console.log('Sending %d frames, each 2 byte header + %d byte payload, using %s strategy', count, size, strategy);
  function complete() {
    completed += 1;
    if (completed == count) {
      console.timeEnd('Send duration');
      process.exit();
    }
  }
  console.time('Send duration');
  for (var i = 0; i < count; ++i) {
    var data = new Buffer(size);
    send(socket, data, complete);
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment