-
Notifications
You must be signed in to change notification settings - Fork 33
/
benchmarks.js
122 lines (107 loc) · 4.14 KB
/
benchmarks.js
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
'use strict';
var Benchmark = require('benchmark');
var sp, msgpack, protobuf, lzstring;
sp = require('./schemapack');
msgpack = require("msgpack-lite"); // Comment out if you didn't `npm install msgpack-lite`
protobuf = require("protobufjs"); // Comment out if you didn't `npm install protobufjs`
lzstring = require('lz-string'); // Comment out if you didn't `npm install lz-string`
function byteCount(testName, len, baseLen) {
console.log(testName + " Byte Count: " + len + (baseLen ? ', ' + Math.round(len / baseLen * 100) + '%' : ''));
}
function runSuite(suite) {
suite.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
var fastest = this.filter('fastest');
console.log('Fastest is ' + fastest.map('name'));
var fhz = fastest.map('hz');
Array.prototype.forEach.call(this, function(item) {
console.log(item['name'] + ' : ' + Math.round(fhz / item['hz'] * 100) + '% slower');
});
})
.run({ 'async': false, 'minTime': .01, 'minSamples': 0 });
}
exports.runBenchmark = function(schema, val) {
console.log('###############################');
console.log("Benchmark beginning..");
var built = sp.build(schema);
if (protobuf) {
var pbJS = protobuf.loadJson(pbjsPlayerSchema);
var pbMsg = pbJS.build("Message");
}
var playerFixture = require('./fixtures/player');
var suiteEncode = new Benchmark.Suite;
suiteEncode.add('JSON Encode', function() { JSON.stringify(val); });
suiteEncode.add('SchemaPack Encode', function() { built.encode(val); });
if (msgpack) suiteEncode.add('MsgPack Encode', function() { msgpack.encode(val); });
if (protobuf) suiteEncode.add('ProtoBuf Encode', function() { pbMsg.encode(playerFixture.items[0]).toArrayBuffer(); });
if (lzstring) suiteEncode.add('LZ-String Encode', function() { lzstring.compressToBase64(JSON.stringify(val)); });
runSuite(suiteEncode);
console.log("--------------------------------------");
var spEncoded = built.encode(val);
var jsonEncoded = JSON.stringify(val);
if (msgpack) { var msgPackEncoded = msgpack.encode(val); }
if (protobuf) { var protobufEncoded = pbMsg.encode(playerFixture.items[0]).toArrayBuffer(); }
if (lzstring) { var lzstringEncoded = lzstring.compressToBase64(JSON.stringify(val)); }
var suiteDecode = new Benchmark.Suite;
suiteDecode.add('JSON Decode', function() { JSON.parse(jsonEncoded); });
suiteDecode.add('SchemaPack Decode', function() { built.decode(spEncoded); });
if (msgpack) suiteDecode.add('MsgPack Decode', function() { msgpack.decode(msgPackEncoded); });
if (protobuf) suiteDecode.add('ProtoBuf Decode', function() { pbMsg.decode(protobufEncoded); });
if (lzstring) suiteDecode.add('LZ-String Decode', function() { JSON.parse(lzstring.decompressFromBase64(lzstringEncoded)); });
runSuite(suiteDecode);
console.log("--------------------------------------");
byteCount("JSON", jsonEncoded.length);
byteCount("SchemaPack", spEncoded.length, jsonEncoded.length);
if (msgpack) { byteCount("MsgPack", msgPackEncoded.length, jsonEncoded.length); }
if (protobuf) { byteCount("ProtoBuf", protobufEncoded.byteLength, jsonEncoded.length); }
if (lzstring) { byteCount("LZ-String", lzstringEncoded.length, jsonEncoded.length); }
}
// Used pbjs CLI to create this from .proto file for the player schema
var pbjsPlayerSchema = {
"package": null,
"messages": [{
"name": "Message",
"fields": [{
"rule": "required",
"type": "int32",
"name": "health",
"id": 1
}, {
"rule": "required",
"type": "bool",
"name": "jumping",
"id": 2
}, {
"rule": "repeated",
"type": "int32",
"name": "position",
"id": 3
}, {
"rule": "required",
"type": "Attributes",
"name": "attributes",
"id": 4
}],
"messages": [{
"name": "Attributes",
"fields": [{
"rule": "required",
"type": "int32",
"name": "str",
"id": 1
}, {
"rule": "required",
"type": "int32",
"name": "agi",
"id": 2
}, {
"rule": "required",
"type": "int32",
"name": "int",
"id": 3
}]
}]
}]
};