Skip to content

Instantly share code, notes, and snippets.

@joelrbrandt
Created July 14, 2013 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelrbrandt/5995120 to your computer and use it in GitHub Desktop.
Save joelrbrandt/5995120 to your computer and use it in GitHub Desktop.
Benchmark test for reading from Node.js stream2, with addition of a firstBufferLength property
(function () {
"use strict";
var stream = require("stream");
var i, b,
startTime, endTime, totalTime,
readCount, bytesRead, bytesReadThisTest,
rs;
var TEST_ITERATIONS = 400,
MIN_BLOCK_SIZE = 1000,
BYTES_PER_TEST = MIN_BLOCK_SIZE * 10000;
// pushes totalBytes onto readable in chunks, where each chunk
// is randomly sized between minBytes and 2*minBytes
function doPushes(readable, totalBytes, minBytes) {
var count = 0, n;
while (count < totalBytes) {
n = Math.min(
~~(minBytes + Math.random()*minBytes), // ~~ coerces float to int
totalBytes-count // total amount remaining to write
);
readable.push(new Buffer(n));
count += n;
}
}
function addHRTime(accumulator, addition) {
accumulator[0] += addition[0];
accumulator[1] += addition[1];
if (accumulator[1] > 1000000000) {
accumulator[0] += 1;
accumulator[1] -= 1000000000;
}
}
function formatHRTime(time) {
return "" + (time[0] + time[1]/1000000000);
}
// Do a throw-away test to warm up the buffer pool
rs = new stream.Readable({highWaterMark: 1024 * 1024 * 128});
rs._read = function () {};
totalTime = [0,0];
readCount = 0;
bytesRead = 0;
for (i = 0; i < TEST_ITERATIONS; i++) {
doPushes(rs, BYTES_PER_TEST, MIN_BLOCK_SIZE);
startTime = process.hrtime();
bytesReadThisTest = 0;
while (bytesReadThisTest < BYTES_PER_TEST) {
b = rs.read();
readCount++;
if (b) {
bytesRead += b.length;
bytesReadThisTest += b.length;
}
}
endTime = process.hrtime(startTime);
addHRTime(totalTime, endTime);
}
console.log("Throw-away test done, read %d total bytes in %d read calls. Total time %s seconds", bytesRead, readCount, formatHRTime(totalTime));
///////////////
rs = new stream.Readable({highWaterMark: 1024 * 1024 * 128});
rs._read = function () {};
totalTime = [0,0];
readCount = 0;
bytesRead = 0;
for (i = 0; i < TEST_ITERATIONS; i++) {
doPushes(rs, BYTES_PER_TEST, MIN_BLOCK_SIZE);
startTime = process.hrtime();
bytesReadThisTest = 0;
while (bytesReadThisTest < BYTES_PER_TEST) {
b = rs.read();
readCount++;
if (b) {
bytesRead += b.length;
bytesReadThisTest += b.length;
}
}
endTime = process.hrtime(startTime);
addHRTime(totalTime, endTime);
}
console.log("Read entire contents test done, read %d total bytes in %d read calls. Total time %s seconds", bytesRead, readCount, formatHRTime(totalTime));
///////////////
rs = new stream.Readable({highWaterMark: 1024 * 1024 * 128});
rs._read = function () {};
totalTime = [0,0];
readCount = 0;
bytesRead = 0;
for (i = 0; i < TEST_ITERATIONS; i++) {
doPushes(rs, BYTES_PER_TEST, MIN_BLOCK_SIZE);
startTime = process.hrtime();
bytesReadThisTest = 0;
while (bytesReadThisTest < BYTES_PER_TEST) {
b = rs.read(MIN_BLOCK_SIZE);
readCount++;
if (b) {
bytesRead += b.length;
bytesReadThisTest += b.length;
}
}
endTime = process.hrtime(startTime);
addHRTime(totalTime, endTime);
}
console.log("Read MIN_BLOCK_SIZE test done, read %d total bytes in %d read calls. Total time %s seconds", bytesRead, readCount, formatHRTime(totalTime));
///////////////
rs = new stream.Readable({highWaterMark: 1024 * 1024 * 128});
rs._read = function () {};
totalTime = [0,0];
readCount = 0;
bytesRead = 0;
for (i = 0; i < TEST_ITERATIONS; i++) {
doPushes(rs, BYTES_PER_TEST, MIN_BLOCK_SIZE);
startTime = process.hrtime();
bytesReadThisTest = 0;
while (bytesReadThisTest < BYTES_PER_TEST) {
b = rs.read(rs.firstBufferLength);
readCount++;
if (b) {
bytesRead += b.length;
bytesReadThisTest += b.length;
}
}
endTime = process.hrtime(startTime);
addHRTime(totalTime, endTime);
}
console.log("Read firstBufferLength test done, read %d total bytes in %d read calls. Total time %s seconds", bytesRead, readCount, formatHRTime(totalTime));
///////////////
rs = new stream.Readable({highWaterMark: 1024 * 1024 * 128, objectMode: true });
rs._read = function () {};
totalTime = [0,0];
readCount = 0;
bytesRead = 0;
for (i = 0; i < TEST_ITERATIONS; i++) {
doPushes(rs, BYTES_PER_TEST, MIN_BLOCK_SIZE);
startTime = process.hrtime();
bytesReadThisTest = 0;
while (bytesReadThisTest < BYTES_PER_TEST) {
b = rs.read();
readCount++;
if (b) {
bytesRead += b.length;
bytesReadThisTest += b.length;
}
}
endTime = process.hrtime(startTime);
addHRTime(totalTime, endTime);
}
console.log("Object mode test done, read %d total bytes in %d read calls. Total time %s seconds", bytesRead, readCount, formatHRTime(totalTime));
///////////////
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment