diff options
author | isaacs <i@izs.me> | 2013-02-07 19:13:26 -0800 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-02-19 14:14:30 -0800 |
commit | e82f97401f9d7c54155b52b310f1893e45205c46 (patch) | |
tree | 10d9f4d3d9c8052aefead3bac89b3a249a8084c9 /benchmark/net/tcp-raw-s2c.js | |
parent | baea73ccda8a54d93f0de4f60ffcee568dac8c01 (diff) | |
download | android-node-v8-e82f97401f9d7c54155b52b310f1893e45205c46.tar.gz android-node-v8-e82f97401f9d7c54155b52b310f1893e45205c46.tar.bz2 android-node-v8-e82f97401f9d7c54155b52b310f1893e45205c46.zip |
bench: net benchmarks using common script
Diffstat (limited to 'benchmark/net/tcp-raw-s2c.js')
-rw-r--r-- | benchmark/net/tcp-raw-s2c.js | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/benchmark/net/tcp-raw-s2c.js b/benchmark/net/tcp-raw-s2c.js new file mode 100644 index 0000000000..94a90f29de --- /dev/null +++ b/benchmark/net/tcp-raw-s2c.js @@ -0,0 +1,136 @@ +// In this benchmark, we connect a client to the server, and write +// as many bytes as we can in the specified time (default = 10s) + +var common = require('../common.js'); + +// if there are dur=N and len=N args, then +// run the function with those settings. +// if not, then queue up a bunch of child processes. +var bench = common.createBenchmark(main, { + len: [102400, 1024 * 1024 * 16], + type: ['utf', 'asc', 'buf'], + dur: [1, 3], +}); + +var TCP = process.binding('tcp_wrap').TCP; +var PORT = common.PORT; + +var dur; +var len; +var type; + +function main(conf) { + dur = +conf.dur; + len = +conf.len; + type = conf.type; + server(); +} + + +function fail(syscall) { + var e = new Error(syscall + ' ' + errno); + e.errno = e.code = errno; + e.syscall = syscall; + throw e; +} + +function server() { + var serverHandle = new TCP(); + var r = serverHandle.bind('127.0.0.1', PORT); + if (r) + fail('bind'); + + var r = serverHandle.listen(511); + if (r) + fail('listen'); + + serverHandle.onconnection = function(clientHandle) { + if (!clientHandle) + fail('connect'); + + var chunk; + switch (type) { + case 'buf': + chunk = new Buffer(len); + chunk.fill('x'); + break; + case 'utf': + chunk = new Array(len / 2 + 1).join('ΓΌ'); + break; + case 'asc': + chunk = new Array(len + 1).join('x'); + break; + default: + throw new Error('invalid type: ' + type); + break; + } + + clientHandle.readStart(); + + while (clientHandle.writeQueueSize === 0) + write(); + + function write() { + var writeReq + switch (type) { + case 'buf': + writeReq = clientHandle.writeBuffer(chunk); + break; + case 'utf': + writeReq = clientHandle.writeUtf8String(chunk); + break; + case 'asc': + writeReq = clientHandle.writeAsciiString(chunk); + break; + } + + if (!writeReq) + fail('write'); + + writeReq.oncomplete = afterWrite; + } + + function afterWrite(status, handle, req) { + if (status) + fail('write'); + + while (clientHandle.writeQueueSize === 0) + write(); + } + }; + + client(); +} + +function client() { + var clientHandle = new TCP(); + var connectReq = clientHandle.connect('127.0.0.1', PORT); + + if (!connectReq) + fail('connect'); + + connectReq.oncomplete = function() { + var bytes = 0; + clientHandle.onread = function(buffer, offset, length) { + // we're not expecting to ever get an EOF from the client. + // just lots of data forever. + if (!buffer) + fail('read'); + + // don't slice the buffer. the point of this is to isolate, not + // simulate real traffic. + // var chunk = buffer.slice(offset, offset + length); + bytes += length; + }; + + clientHandle.readStart(); + + // the meat of the benchmark is right here: + bench.start(); + + setTimeout(function() { + // report in Gb/sec + bench.end((bytes * 8) / (1024 * 1024 * 1024)); + }, dur * 1000); + }; +} |