bench: Buffer read/write benchmarks
authorisaacs <i@izs.me>
Tue, 12 Feb 2013 07:53:27 +0000 (23:53 -0800)
committerisaacs <i@izs.me>
Tue, 19 Feb 2013 22:14:32 +0000 (14:14 -0800)
benchmark/buffer_read.js [deleted file]
benchmark/buffer_write.js [deleted file]
benchmark/buffers/buffer_read.js [new file with mode: 0644]
benchmark/buffers/buffer_write.js [new file with mode: 0644]

diff --git a/benchmark/buffer_read.js b/benchmark/buffer_read.js
deleted file mode 100644 (file)
index f128bf2..0000000
+++ /dev/null
@@ -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);
-  }
-});
diff --git a/benchmark/buffer_write.js b/benchmark/buffer_write.js
deleted file mode 100644 (file)
index 70f9926..0000000
+++ /dev/null
@@ -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);
-  }
-});
diff --git a/benchmark/buffers/buffer_read.js b/benchmark/buffers/buffer_read.js
new file mode 100644 (file)
index 0000000..fccd99d
--- /dev/null
@@ -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);
+}
diff --git a/benchmark/buffers/buffer_write.js b/benchmark/buffers/buffer_write.js
new file mode 100644 (file)
index 0000000..4dbfcb6
--- /dev/null
@@ -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);
+}