mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
bench: Buffer read/write benchmarks
This commit is contained in:
1 parent
419607e8eb
commit
55aa2571a0
4 files changed
+91
-200
No files matched your search
@@ -1,97 +0,0 @@
|
||||
const LEN = 1e7;
|
||||
const noAssert = process.argv[3] == 'true' ? true
|
||||
: process.argv[3] == 'false' ? false
|
||||
: undefined;
|
||||
|
||||
var timer = require('./_bench_timer');
|
||||
|
||||
var buff = (process.argv[2] == 'slow') ?
|
||||
(new require('buffer').SlowBuffer(8)) :
|
||||
(new Buffer(8));
|
||||
var i;
|
||||
|
||||
buff.writeDoubleLE(0, 0, noAssert);
|
||||
|
||||
timer('readUInt8', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readUInt8(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readUInt16LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readUInt16LE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readUInt16BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readUInt16BE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readUInt32LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readUInt32LE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readUInt32BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readUInt32BE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readInt8', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readInt8(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readInt16LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readInt16LE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readInt16BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readInt16BE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readInt32LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readInt32LE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readInt32BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readInt32BE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readFloatLE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readFloatLE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readFloatBE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readFloatBE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readDoubleLE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readDoubleLE(0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('readDoubleBE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.readDoubleBE(0, noAssert);
|
||||
}
|
||||
});
|
||||
@@ -1,103 +0,0 @@
|
||||
const LEN = 1e7;
|
||||
|
||||
const INT8 = 0x7f;
|
||||
const INT16 = 0x7fff;
|
||||
const INT32 = 0x7fffffff;
|
||||
const UINT8 = INT8 * 2;
|
||||
const UINT16 = INT16 * 2;
|
||||
const UINT32 = INT32 * 2;
|
||||
|
||||
const noAssert = process.argv[3] == 'true' ? true
|
||||
: process.argv[3] == 'false' ? false
|
||||
: undefined;
|
||||
|
||||
var timer = require('./_bench_timer');
|
||||
|
||||
var buff = (process.argv[2] == 'slow') ?
|
||||
(new require('buffer').SlowBuffer(8)) :
|
||||
(new Buffer(8));
|
||||
var i;
|
||||
|
||||
timer('writeUInt8', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeUInt8(i % UINT8, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeUInt16LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeUInt16LE(i % UINT16, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeUInt16BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeUInt16BE(i % UINT16, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeUInt32LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeUInt32LE(i % UINT32, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeUInt32BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeUInt32BE(i % UINT32, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeInt8', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeInt8(i % INT8, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeInt16LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeInt16LE(i % INT16, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeInt16BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeInt16BE(i % INT16, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeInt32LE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeInt32LE(i % INT32, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeInt32BE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeInt32BE(i % INT32, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeFloatLE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeFloatLE(i * 0.1, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeFloatBE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeFloatBE(i * 0.1, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeDoubleLE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeDoubleLE(i * 0.1, 0, noAssert);
|
||||
}
|
||||
});
|
||||
|
||||
timer('writeDoubleBE', function() {
|
||||
for (i = 0; i < LEN; i++) {
|
||||
buff.writeDoubleBE(i * 0.1, 0, noAssert);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
var common = require('../common.js');
|
||||
|
||||
var bench = common.createBenchmark(main, {
|
||||
noAssert: [false, true],
|
||||
buffer: ['fast', 'slow'],
|
||||
type: ['UInt8', 'UInt16LE', 'UInt16BE',
|
||||
'UInt32LE', 'UInt32BE',
|
||||
'Int8', 'Int16LE', 'Int16BE',
|
||||
'Int32LE', 'Int32BE',
|
||||
'FloatLE', 'FloatBE',
|
||||
'DoubleLE', 'DoubleBE'],
|
||||
millions: [1]
|
||||
});
|
||||
|
||||
function main(conf) {
|
||||
var noAssert = conf.noAssert === 'true';
|
||||
var len = +conf.millions * 1e6;
|
||||
var clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
|
||||
var buff = new clazz(8);
|
||||
var fn = 'read' + conf.type;
|
||||
|
||||
buff.writeDoubleLE(0, 0, noAssert);
|
||||
bench.start();
|
||||
for (var i = 0; i < len; i++) {
|
||||
buff[fn](0, noAssert);
|
||||
}
|
||||
bench.end(len / 1e6);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
|
||||
var common = require('../common.js');
|
||||
var bench = common.createBenchmark(main, {
|
||||
noAssert: [false, true],
|
||||
buffer: ['fast', 'slow'],
|
||||
type: ['UInt8', 'UInt16LE', 'UInt16BE',
|
||||
'UInt32LE', 'UInt32BE',
|
||||
'Int8', 'Int16LE', 'Int16BE',
|
||||
'Int32LE', 'Int32BE',
|
||||
'FloatLE', 'FloatBE',
|
||||
'DoubleLE', 'DoubleBE'],
|
||||
millions: [1]
|
||||
});
|
||||
|
||||
const INT8 = 0x7f;
|
||||
const INT16 = 0x7fff;
|
||||
const INT32 = 0x7fffffff;
|
||||
const UINT8 = INT8 * 2;
|
||||
const UINT16 = INT16 * 2;
|
||||
const UINT32 = INT32 * 2;
|
||||
|
||||
var mod = {
|
||||
writeInt8: INT8,
|
||||
writeInt16BE: INT16,
|
||||
writeInt16LE: INT16,
|
||||
writeInt32BE: INT32,
|
||||
writeInt32LE: INT32,
|
||||
writeUInt8: UINT8,
|
||||
writeUInt16BE: UINT16,
|
||||
writeUInt16LE: UINT16,
|
||||
writeUInt32BE: UINT32,
|
||||
writeUInt32LE: UINT32
|
||||
};
|
||||
|
||||
function main(conf) {
|
||||
var noAssert = conf.noAssert === 'true';
|
||||
var len = +conf.millions * 1e6;
|
||||
var clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
|
||||
var buff = new clazz(8);
|
||||
var fn = 'write' + conf.type;
|
||||
|
||||
if (fn.match(/Int/))
|
||||
benchInt(buff, fn, len, noAssert);
|
||||
else
|
||||
benchFloat(buff, fn, len, noAssert);
|
||||
}
|
||||
|
||||
function benchInt(buff, fn, len, noAssert) {
|
||||
var m = mod[fn];
|
||||
bench.start();
|
||||
for (var i = 0; i < len; i++) {
|
||||
buff[fn](i % m, 0, noAssert);
|
||||
}
|
||||
bench.end(len / 1e6);
|
||||
}
|
||||
|
||||
function benchFloat(buff, fn, len, noAssert) {
|
||||
bench.start();
|
||||
for (var i = 0; i < len; i++) {
|
||||
buff[fn](i * 0.1, 0, noAssert);
|
||||
}
|
||||
bench.end(len / 1e6);
|
||||
}
|
||||
Reference in new issue
Block a user