testing/xpcshell/node-spdy/test/benchmarks/syn.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 var tls = require('tls'),
michael@0 2 frames = require('../fixtures/frames');
michael@0 3
michael@0 4 var uri = require('url').parse(process.argv[2]),
michael@0 5 host = uri.hostname,
michael@0 6 port = +uri.port,
michael@0 7 url = uri.path;
michael@0 8
michael@0 9 frames.createSynStream(host, url, function(syn_stream) {
michael@0 10 var start = +new Date,
michael@0 11 num = 3000;
michael@0 12
michael@0 13 batch(port, host, syn_stream, 100, num, function() {
michael@0 14 var end = +new Date;
michael@0 15 console.log('requests/sec : %d', 1000 * num / (end - start));
michael@0 16 });
michael@0 17 });
michael@0 18
michael@0 19 function request(port, host, data, callback) {
michael@0 20 var socket = tls.connect(port, host, {NPNProtocols: ['spdy/2']}, function() {
michael@0 21 socket.write(data);
michael@0 22 socket.once('data', function() {
michael@0 23 socket.destroy();
michael@0 24 callback();
michael@0 25 });
michael@0 26 });
michael@0 27
michael@0 28 return socket;
michael@0 29 };
michael@0 30
michael@0 31 function batch(port, host, data, parallel, num, callback) {
michael@0 32 var left = num,
michael@0 33 done = 0;
michael@0 34
michael@0 35 for (var i = 0; i < parallel; i++) {
michael@0 36 run(i);
michael@0 37 }
michael@0 38
michael@0 39 function run(i) {
michael@0 40 left--;
michael@0 41 if (left < 0) return;
michael@0 42
michael@0 43 request(port, host, data, function() {
michael@0 44 if (++done === num) return callback();
michael@0 45 run(i);
michael@0 46 });
michael@0 47 }
michael@0 48 };

mercurial