forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.js
More file actions
executable file
·157 lines (131 loc) · 4.14 KB
/
benchmark.js
File metadata and controls
executable file
·157 lines (131 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
var msgpack_node = try_require("msgpack");
var msgpack_lite = try_require("../index");
var msgpack_js = try_require("msgpack-js");
var msgpack_js_v5 = try_require("msgpack-js-v5");
var msgpack5 = try_require("msgpack5");
var msgpack_unpack = try_require("msgpack-unpack");
var msgpack_codec = try_require("msgpack.codec");
var notepack = try_require("notepack");
msgpack5 = msgpack5 && msgpack5();
msgpack_codec = msgpack_codec && msgpack_codec.msgpack;
var pkg = require("../package.json");
var data = require("../test/example");
var packed = msgpack_lite.encode(data);
var expected = JSON.stringify(data);
var argv = Array.prototype.slice.call(process.argv, 2);
if (argv[0] === "-v") {
console.warn(pkg.name + " " + pkg.version);
process.exit(0);
}
var limit = 5;
if (argv[0] - 0) limit = argv.shift() - 0;
limit *= 1000;
var COL1 = 57;
var COL2 = 6;
var COL3 = 5;
var COL4 = 6;
console.log(rpad("operation", COL1), "|", " op ", "|", " ms ", "|", " op/s ");
console.log(rpad("", COL1, "-"), "|", lpad(":", COL2, "-"), "|", lpad(":", COL3, "-"), "|", lpad(":", COL4, "-"));
var buf, obj;
if (JSON) {
buf = bench('buf = Buffer(JSON.stringify(obj));', JSON_stringify, data);
obj = bench('obj = JSON.parse(buf);', JSON.parse, buf);
test(obj);
}
if (msgpack_lite) {
buf = bench('buf = require("msgpack-lite").encode(obj);', msgpack_lite.encode, data);
obj = bench('obj = require("msgpack-lite").decode(buf);', msgpack_lite.decode, packed);
test(obj);
}
if (msgpack_node) {
buf = bench('buf = require("msgpack").pack(obj);', msgpack_node.pack, data);
obj = bench('obj = require("msgpack").unpack(buf);', msgpack_node.unpack, buf);
test(obj);
}
if (msgpack_codec) {
buf = bench('buf = Buffer(require("msgpack.codec").msgpack.pack(obj));', msgpack_codec_pack, data);
obj = bench('obj = require("msgpack.codec").msgpack.unpack(buf);', msgpack_codec.unpack, buf);
test(obj);
}
if (msgpack_js_v5) {
buf = bench('buf = require("msgpack-js-v5").encode(obj);', msgpack_js_v5.encode, data);
obj = bench('obj = require("msgpack-js-v5").decode(buf);', msgpack_js_v5.decode, buf);
test(obj);
}
if (msgpack_js) {
buf = bench('buf = require("msgpack-js").encode(obj);', msgpack_js.encode, data);
obj = bench('obj = require("msgpack-js").decode(buf);', msgpack_js.decode, buf);
test(obj);
}
if (msgpack5) {
buf = bench('buf = require("msgpack5")().encode(obj);', msgpack5.encode, data);
obj = bench('obj = require("msgpack5")().decode(buf);', msgpack5.decode, buf);
test(obj);
}
if (notepack) {
buf = bench('buf = require("notepack").encode(obj);', notepack.encode, data);
obj = bench('obj = require("notepack").decode(buf);', notepack.decode, buf);
test(obj);
}
if (msgpack_unpack) {
obj = bench('obj = require("msgpack-unpack").decode(buf);', msgpack_unpack, packed);
test(obj);
}
function JSON_stringify(src) {
return Buffer(JSON.stringify(src));
}
function msgpack_codec_pack(data) {
return Buffer(msgpack_codec.pack(data));
}
function bench(name, func, src) {
if (argv.length) {
var match = argv.filter(function(grep) {
return (name.indexOf(grep) > -1);
});
if (!match.length) return SKIP;
}
var ret, duration;
var start = new Date() - 0;
var count = 0;
while (1) {
var end = new Date() - 0;
duration = end - start;
if (duration >= limit) break;
while ((++count) % 100) ret = func(src);
}
name = rpad(name, COL1);
var score = Math.floor(count / duration * 1000);
count = lpad(count, COL2);
duration = lpad(duration, COL3);
score = lpad(score, COL4);
console.log(name, "|", count, "|", duration, "|", score);
return ret;
}
function rpad(str, len, chr) {
if (!chr) chr = " ";
while (str.length < len) str += chr;
return str;
}
function lpad(str, len, chr) {
if (!chr) chr = " ";
str += "";
while (str.length < len) str = chr + str;
return str;
}
function test(actual) {
if (actual === SKIP) return;
actual = JSON.stringify(actual);
if (actual === expected) return;
console.warn("expected: " + expected);
console.warn("actual: " + actual);
}
function SKIP() {
}
function try_require(name) {
try {
return require(name);
} catch (e) {
// ignore
}
}