Skip to content

Commit

Permalink
benchmark: (http(2)) refactor
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18320
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and ryzokuken committed Jun 6, 2018
1 parent cf8a4af commit 8a97757
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
7 changes: 1 addition & 6 deletions benchmark/http/bench-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const bench = common.createBenchmark(main, {
n: [1e5],
});


function main(conf) {
const len = conf.len >>> 0;
const n = conf.n >>> 0;
function main({ len, n }) {
var header = `GET /hello HTTP/1.1${CRLF}Content-Type: text/plain${CRLF}`;

for (var i = 0; i < len; i++) {
Expand All @@ -28,7 +25,6 @@ function main(conf) {
processHeader(Buffer.from(header), n);
}


function processHeader(header, n) {
const parser = newParser(REQUEST);

Expand All @@ -40,7 +36,6 @@ function processHeader(header, n) {
bench.end(n);
}


function newParser(type) {
const parser = new HTTPParser(type);

Expand Down
3 changes: 1 addition & 2 deletions benchmark/http/simple.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const common = require('../common.js');
const PORT = common.PORT;

const bench = common.createBenchmark(main, {
// unicode confuses ab on os x.
Expand All @@ -15,7 +14,7 @@ const bench = common.createBenchmark(main, {
function main(conf) {
process.env.PORT = PORT;
var server = require('../fixtures/simple-http-server.js')
.listen(PORT)
.listen(common.PORT)
.on('listening', function() {
const path =
`/${conf.type}/${conf.len}/${conf.chunks}/${conf.res}/${conf.chunkedEnc}`;
Expand Down
51 changes: 51 additions & 0 deletions benchmark/http/upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const common = require('../common.js');
const net = require('net');

const bench = common.createBenchmark(main, {
n: [5, 1000]
});

const reqData = 'GET / HTTP/1.1\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
'\r\n' +
'WjN}|M(6';

const resData = 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
'Upgrade: WebSocket\r\n' +
'Connection: Upgrade\r\n' +
'\r\n\r\n';

function main({ n }) {
var server = require('../fixtures/simple-http-server.js')
.listen(common.PORT)
.on('listening', function() {
bench.start();
doBench(server.address(), n, function() {
bench.end(n);
server.close();
});
})
.on('upgrade', function(req, socket, upgradeHead) {
socket.resume();
socket.write(resData);
socket.end();
});
}

function doBench(address, count, done) {
if (count === 0) {
done();
return;
}

const conn = net.createConnection(address.port);
conn.write(reqData);
conn.resume();

conn.on('end', function() {
doBench(address, count - 1, done);
});
}
3 changes: 1 addition & 2 deletions benchmark/http2/respond-with-fd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common.js');
const PORT = common.PORT;
const path = require('path');
const fs = require('fs');

Expand Down Expand Up @@ -29,7 +28,7 @@ function main(conf) {
stream.respondWithFD(fd);
stream.on('error', (err) => {});
});
server.listen(PORT, () => {
server.listen(common.PORT, () => {
bench.http({
path: '/',
requests: n,
Expand Down
5 changes: 1 addition & 4 deletions benchmark/http2/simple.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict';

const common = require('../common.js');
const PORT = common.PORT;

const path = require('path');
const fs = require('fs');

const file = path.join(path.resolve(__dirname, '../fixtures'), 'alice.html');

const bench = common.createBenchmark(main, {
Expand All @@ -27,7 +24,7 @@ function main(conf) {
out.pipe(stream);
stream.on('error', (err) => {});
});
server.listen(PORT, () => {
server.listen(common.PORT, () => {
bench.http({
path: '/',
requests: n,
Expand Down
3 changes: 1 addition & 2 deletions benchmark/http2/write.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const common = require('../common.js');
const PORT = common.PORT;

const bench = common.createBenchmark(main, {
streams: [100, 200, 1000],
Expand Down Expand Up @@ -29,7 +28,7 @@ function main(conf) {
}
write();
});
server.listen(PORT, () => {
server.listen(common.PORT, () => {
bench.http({
path: '/',
requests: 10000,
Expand Down

0 comments on commit 8a97757

Please sign in to comment.