|
| 1 | +// original: https://github.com/kawanet/msgpack-lite/master/lib/benchmark.js |
| 2 | + |
| 3 | +// automatically use the dist version if available |
| 4 | +var msgpack_msgpack = try_require("..") || require("../src"); |
| 5 | + |
| 6 | +var msgpack_node = try_require("msgpack"); |
| 7 | +var msgpack_lite = try_require("msgpack-lite"); |
| 8 | +var msgpack_js = try_require("msgpack-js"); |
| 9 | +var msgpack_js_v5 = try_require("msgpack-js-v5"); |
| 10 | +var msgpack5 = try_require("msgpack5"); |
| 11 | +var msgpack_unpack = try_require("msgpack-unpack"); |
| 12 | +var notepack = try_require("notepack"); |
| 13 | + |
| 14 | +msgpack5 = msgpack5 && msgpack5(); |
| 15 | + |
| 16 | +var pkg = require("../package.json"); |
| 17 | +var data = require("./benchmark-from-msgpack-lite-data.json"); |
| 18 | +var packed = msgpack_lite.encode(data); |
| 19 | +var expected = JSON.stringify(data); |
| 20 | + |
| 21 | +var argv = Array.prototype.slice.call(process.argv, 2); |
| 22 | + |
| 23 | +if (argv[0] === "-v") { |
| 24 | + console.warn(pkg.name + " " + pkg.version); |
| 25 | + process.exit(0); |
| 26 | +} |
| 27 | + |
| 28 | +var limit = 5; |
| 29 | +if (argv[0] - 0) limit = argv.shift() - 0; |
| 30 | +limit *= 1000; |
| 31 | + |
| 32 | +var COL1 = 65; |
| 33 | +var COL2 = 7; |
| 34 | +var COL3 = 5; |
| 35 | +var COL4 = 7; |
| 36 | + |
| 37 | +console.log(`Benchmark on NodeJS/${process.version}\n`) |
| 38 | +console.log(rpad("operation", COL1), "|", " op ", "|", " ms ", "|", " op/s "); |
| 39 | +console.log(rpad("", COL1, "-"), "|", lpad(":", COL2, "-"), "|", lpad(":", COL3, "-"), "|", lpad(":", COL4, "-")); |
| 40 | + |
| 41 | +var buf, obj; |
| 42 | + |
| 43 | +if (JSON) { |
| 44 | + buf = bench('buf = Buffer.from(JSON.stringify(obj));', JSON_stringify, data); |
| 45 | + buf = bench('buf = JSON.stringify(obj);', JSON.stringify, data); |
| 46 | + obj = bench('obj = JSON.parse(buf);', JSON.parse, buf); |
| 47 | + runTest(obj); |
| 48 | +} |
| 49 | + |
| 50 | +if (msgpack_lite) { |
| 51 | + buf = bench('buf = require("msgpack-lite").encode(obj);', msgpack_lite.encode, data); |
| 52 | + obj = bench('obj = require("msgpack-lite").decode(buf);', msgpack_lite.decode, packed); |
| 53 | + runTest(obj); |
| 54 | +} |
| 55 | + |
| 56 | +if (msgpack_node) { |
| 57 | + buf = bench('buf = require("msgpack").pack(obj);', msgpack_node.pack, data); |
| 58 | + obj = bench('obj = require("msgpack").unpack(buf);', msgpack_node.unpack, buf); |
| 59 | + runTest(obj); |
| 60 | +} |
| 61 | + |
| 62 | +if (msgpack_msgpack) { |
| 63 | + buf = bench('buf = require("@msgpack/msgpack").encode(obj);', msgpack_msgpack.encode, data); |
| 64 | + buf = bench('buf = Buffer.from(require("@msgpack/msgpack").encode(obj));', msgpack_msgpack_encode, data); |
| 65 | + obj = bench('obj = require("@msgpack/msgpack").decode(buf);', msgpack_msgpack.decode, buf); |
| 66 | + runTest(obj); |
| 67 | +} |
| 68 | + |
| 69 | +if (msgpack_js_v5) { |
| 70 | + buf = bench('buf = require("msgpack-js-v5").encode(obj);', msgpack_js_v5.encode, data); |
| 71 | + obj = bench('obj = require("msgpack-js-v5").decode(buf);', msgpack_js_v5.decode, buf); |
| 72 | + runTest(obj); |
| 73 | +} |
| 74 | + |
| 75 | +if (msgpack_js) { |
| 76 | + buf = bench('buf = require("msgpack-js").encode(obj);', msgpack_js.encode, data); |
| 77 | + obj = bench('obj = require("msgpack-js").decode(buf);', msgpack_js.decode, buf); |
| 78 | + runTest(obj); |
| 79 | +} |
| 80 | + |
| 81 | +if (msgpack5) { |
| 82 | + buf = bench('buf = require("msgpack5")().encode(obj);', msgpack5.encode, data); |
| 83 | + obj = bench('obj = require("msgpack5")().decode(buf);', msgpack5.decode, buf); |
| 84 | + runTest(obj); |
| 85 | +} |
| 86 | + |
| 87 | +if (notepack) { |
| 88 | + buf = bench('buf = require("notepack").encode(obj);', notepack.encode, data); |
| 89 | + obj = bench('obj = require("notepack").decode(buf);', notepack.decode, buf); |
| 90 | + runTest(obj); |
| 91 | +} |
| 92 | + |
| 93 | +if (msgpack_unpack) { |
| 94 | + obj = bench('obj = require("msgpack-unpack").decode(buf);', msgpack_unpack, packed); |
| 95 | + runTest(obj); |
| 96 | +} |
| 97 | + |
| 98 | +function JSON_stringify(src: any) { |
| 99 | + return Buffer.from(JSON.stringify(src)); |
| 100 | +} |
| 101 | + |
| 102 | +function msgpack_msgpack_encode(data: any) { |
| 103 | + return Buffer.from(msgpack_msgpack.encode(data)); |
| 104 | +} |
| 105 | + |
| 106 | +function bench(name: string, func: (...args: any[]) => any, src: any) { |
| 107 | + if (argv.length) { |
| 108 | + var match = argv.filter(function(grep) { |
| 109 | + return (name.indexOf(grep) > -1); |
| 110 | + }); |
| 111 | + if (!match.length) return SKIP; |
| 112 | + } |
| 113 | + // warm up |
| 114 | + func(src); |
| 115 | + |
| 116 | + var ret, duration: number; |
| 117 | + var start = Date.now(); |
| 118 | + var count = 0; |
| 119 | + while (1) { |
| 120 | + var end = Date.now(); |
| 121 | + duration = end - start; |
| 122 | + if (duration >= limit) break; |
| 123 | + while ((++count) % 100) ret = func(src); |
| 124 | + } |
| 125 | + name = rpad(name, COL1); |
| 126 | + var score = Math.floor(count / duration! * 1000); |
| 127 | + console.log(name, "|", lpad(`${count}`, COL2), "|", lpad(`${duration}`, COL3), "|", lpad(`${score}`, COL4)); |
| 128 | + return ret; |
| 129 | +} |
| 130 | + |
| 131 | +function rpad(str: string, len: number, chr = " ") { |
| 132 | + return str.padEnd(len, chr); |
| 133 | +} |
| 134 | + |
| 135 | +function lpad(str: string, len: number, chr = " ") { |
| 136 | + return str.padStart(len, chr); |
| 137 | +} |
| 138 | + |
| 139 | +function runTest(actual: any) { |
| 140 | + if (actual === SKIP) return; |
| 141 | + actual = JSON.stringify(actual); |
| 142 | + if (actual === expected) return; |
| 143 | + console.warn("expected: " + expected); |
| 144 | + console.warn("actual: " + actual); |
| 145 | +} |
| 146 | + |
| 147 | +function SKIP() { |
| 148 | +} |
| 149 | + |
| 150 | +function try_require(name: string) { |
| 151 | + try { |
| 152 | + return require(name); |
| 153 | + } catch (e) { |
| 154 | + // ignore |
| 155 | + } |
| 156 | +} |
0 commit comments