Skip to content

Commit

Permalink
benchmark: (buffer) refactor
Browse files Browse the repository at this point in the history
PR-URL: #18320
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed Feb 21, 2018
1 parent 9acf754 commit cb13c7c
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 100 deletions.
3 changes: 1 addition & 2 deletions benchmark/buffers/buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ const chars = [

function main({ n, len, encoding }) {
var strings = [];
var results;
var results = [ len * 16 ];
if (encoding === 'buffer') {
strings = [ Buffer.alloc(len * 16, 'a') ];
results = [ len * 16 ];
} else {
for (const string of chars) {
// Strings must be built differently, depending on encoding
Expand Down
12 changes: 4 additions & 8 deletions benchmark/buffers/buffer-compare-offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,22 @@ const bench = common.createBenchmark(main, {
});

function compareUsingSlice(b0, b1, len, iter) {
var i;
bench.start();
for (i = 0; i < iter; i++)
for (var i = 0; i < iter; i++)
Buffer.compare(b0.slice(1, len), b1.slice(1, len));
bench.end(iter / 1e6);
}

function compareUsingOffset(b0, b1, len, iter) {
var i;
bench.start();
for (i = 0; i < iter; i++)
for (var i = 0; i < iter; i++)
b0.compare(b1, 1, len, 1, len);
bench.end(iter / 1e6);
}

function main({ millions, size, method }) {
const iter = millions * 1e6;
const fn = method === 'slice' ? compareUsingSlice : compareUsingOffset;
bench.start();
fn(Buffer.alloc(size, 'a'),
Buffer.alloc(size, 'b'),
size >> 1,
iter);
bench.end(millions);
}
43 changes: 15 additions & 28 deletions benchmark/buffers/buffer-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,38 @@ const bench = common.createBenchmark(main, {
});

function main({ len, n, type }) {
let fn, i;
switch (type) {
case '':
case 'fast-alloc':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.alloc(len);
}
bench.end(n);
fn = Buffer.alloc;
break;
case 'fast-alloc-fill':
bench.start();
for (let i = 0; i < n * 1024; i++) {
for (i = 0; i < n * 1024; i++) {
Buffer.alloc(len, 0);
}
bench.end(n);
break;
return;
case 'fast-allocUnsafe':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.allocUnsafe(len);
}
bench.end(n);
fn = Buffer.allocUnsafe;
break;
case 'slow-allocUnsafe':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer.allocUnsafeSlow(len);
}
bench.end(n);
fn = Buffer.allocUnsafeSlow;
break;
case 'slow':
bench.start();
for (let i = 0; i < n * 1024; i++) {
SlowBuffer(len);
}
bench.end(n);
fn = SlowBuffer;
break;
case 'buffer()':
bench.start();
for (let i = 0; i < n * 1024; i++) {
Buffer(len);
}
bench.end(n);
fn = Buffer;
break;
default:
assert.fail(null, null, 'Should not get here');
assert.fail('Should not get here');
}

bench.start();
for (i = 0; i < n * 1024; i++) {
fn(len);
}
bench.end(n);
}
5 changes: 3 additions & 2 deletions benchmark/buffers/buffer-hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ const bench = common.createBenchmark(main, {

function main({ len, n }) {
const buf = Buffer.alloc(len);
var i;

for (let i = 0; i < buf.length; i++)
for (i = 0; i < buf.length; i++)
buf[i] = i & 0xff;

const hex = buf.toString('hex');

bench.start();

for (let i = 0; i < n; i += 1)
for (i = 0; i < n; i += 1)
Buffer.from(hex, 'hex');

bench.end(n);
Expand Down
18 changes: 5 additions & 13 deletions benchmark/buffers/buffer-iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,30 @@ function main({ size, type, method, n }) {
const clazz = type === 'fast' ? Buffer : SlowBuffer;
const buffer = new clazz(size);
buffer.fill(0);
methods[method || 'for'](buffer, n);
}

const fn = methods[method || 'for'];

function benchFor(buffer, n) {
bench.start();
fn(buffer, n);
bench.end(n);
}

function benchFor(buffer, n) {
for (var k = 0; k < n; k++) {
for (var i = 0; i < buffer.length; i++) {
assert(buffer[i] === 0);
}
}

bench.end(n);
}

function benchForOf(buffer, n) {
bench.start();

for (var k = 0; k < n; k++) {
for (const b of buffer) {
assert(b === 0);
}
}
bench.end(n);
}

function benchIterator(buffer, n) {
bench.start();

for (var k = 0; k < n; k++) {
const iter = buffer[Symbol.iterator]();
var cur = iter.next();
Expand All @@ -60,6 +54,4 @@ function benchIterator(buffer, n) {
}

}

bench.end(n);
}
23 changes: 9 additions & 14 deletions benchmark/buffers/buffer-read-float.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ const bench = common.createBenchmark(main, {
millions: [1]
});

function main(conf) {
const noAssert = conf.noAssert === 'true';
const len = +conf.millions * 1e6;
function main({ noAssert, millions, type, endian, value }) {
noAssert = noAssert === 'true';
type = type || 'Double';
const buff = Buffer.alloc(8);
const type = conf.type || 'Double';
const endian = conf.endian;
const fn = `read${type}${endian}`;
const values = {
Double: {
Expand All @@ -32,15 +30,12 @@ function main(conf) {
nan: NaN,
},
};
const value = values[type][conf.value];

buff[`write${type}${endian}`](value, 0, noAssert);
const testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) {
buff.${fn}(0, ${JSON.stringify(noAssert)});
}
`);
buff[`write${type}${endian}`](values[type][value], 0, noAssert);

bench.start();
testFunction(buff);
bench.end(len / 1e6);
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](0, noAssert);
}
bench.end(millions);
}
12 changes: 4 additions & 8 deletions benchmark/buffers/buffer-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ const bench = common.createBenchmark(main, {

function main({ noAssert, millions, buf, type }) {
noAssert = noAssert === 'true';
const len = millions * 1e6;
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const buff = new clazz(8);
const fn = `read${type || 'UInt8'}`;

buff.writeDoubleLE(0, 0, noAssert);
const testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) {
buff.${fn}(0, ${JSON.stringify(noAssert)});
}
`);
bench.start();
testFunction(buff);
bench.end(len / 1e6);
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](0, noAssert);
}
bench.end(millions);
}
31 changes: 12 additions & 19 deletions benchmark/buffers/buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,29 @@ const mod = {
};

function main({ noAssert, millions, buf, type }) {
const len = millions * 1e6;
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const buff = new clazz(8);
const fn = `write${type || 'UInt8'}`;

if (/Int/.test(fn))
benchInt(buff, fn, len, noAssert);
benchInt(buff, fn, millions, noAssert);
else
benchFloat(buff, fn, len, noAssert);
benchFloat(buff, fn, millions, noAssert);
}

function benchInt(buff, fn, len, noAssert) {
function benchInt(buff, fn, millions, noAssert) {
const m = mod[fn];
const testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) {
buff.${fn}(i & ${m}, 0, ${noAssert});
}
`);
bench.start();
testFunction(buff);
bench.end(len / 1e6);
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](i & m, 0, noAssert);
}
bench.end(millions);
}

function benchFloat(buff, fn, len, noAssert) {
const testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) {
buff.${fn}(i, 0, ${noAssert});
}
`);
function benchFloat(buff, fn, millions, noAssert) {
bench.start();
testFunction(buff);
bench.end(len / 1e6);
for (var i = 0; i !== millions * 1e6; i++) {
buff[fn](i, 0, noAssert);
}
bench.end(millions);
}
9 changes: 3 additions & 6 deletions benchmark/buffers/buffer_zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ const zeroBuffer = Buffer.alloc(0);
const zeroString = '';

function main({ n, type }) {
bench.start();

if (type === 'buffer')
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroBuffer);
else if (type === 'string')
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroString);
const data = type === 'buffer' ? zeroBuffer : zeroString;

bench.start();
for (var i = 0; i < n * 1024; i++) Buffer.from(data);
bench.end(n);
}

0 comments on commit cb13c7c

Please sign in to comment.