2013-03-14 15:48:18 +01:00
|
|
|
// When calling .end(buffer) right away, this triggers a "hot path"
|
|
|
|
// optimization in http.js, to avoid an extra write call.
|
|
|
|
//
|
|
|
|
// However, the overhead of copying a large buffer is higher than
|
|
|
|
// the overhead of an extra write() call, so the hot path was not
|
|
|
|
// always as hot as it could be.
|
|
|
|
//
|
|
|
|
// Verify that our assumptions are valid.
|
2016-02-20 02:03:16 +01:00
|
|
|
'use strict';
|
2013-03-14 15:48:18 +01:00
|
|
|
|
|
|
|
var common = require('../common.js');
|
|
|
|
|
|
|
|
var bench = common.createBenchmark(main, {
|
|
|
|
type: ['asc', 'utf', 'buf'],
|
|
|
|
kb: [64, 128, 256, 1024],
|
|
|
|
c: [100],
|
2015-01-29 07:14:57 +01:00
|
|
|
method: ['write', 'end']
|
2013-03-14 15:48:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
function main(conf) {
|
2016-02-20 02:03:16 +01:00
|
|
|
const http = require('http');
|
2013-03-14 15:48:18 +01:00
|
|
|
var chunk;
|
|
|
|
var len = conf.kb * 1024;
|
|
|
|
switch (conf.type) {
|
|
|
|
case 'buf':
|
2016-01-26 00:00:06 +01:00
|
|
|
chunk = Buffer.alloc(len, 'x');
|
2013-03-14 15:48:18 +01:00
|
|
|
break;
|
|
|
|
case 'utf':
|
2017-04-02 23:32:50 +02:00
|
|
|
chunk = 'ü'.repeat(len / 2);
|
2013-03-14 15:48:18 +01:00
|
|
|
break;
|
|
|
|
case 'asc':
|
2017-04-02 23:32:50 +02:00
|
|
|
chunk = 'a'.repeat(len);
|
2013-03-14 15:48:18 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
function write(res) {
|
|
|
|
res.write(chunk);
|
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
function end(res) {
|
|
|
|
res.end(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
var method = conf.method === 'write' ? write : end;
|
|
|
|
|
|
|
|
var server = http.createServer(function(req, res) {
|
|
|
|
method(res);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(common.PORT, function() {
|
2016-08-05 11:34:50 +02:00
|
|
|
bench.http({
|
|
|
|
connections: conf.c
|
|
|
|
}, function() {
|
2013-03-14 15:48:18 +01:00
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|