Skip to content

Commit 597bfba

Browse files
committed
feat: use getByte() instead of get()
- improve read performance node-modules/byte#29 - use fastbench instead of benchmark
1 parent 8e8cfc6 commit 597bfba

File tree

12 files changed

+296
-109
lines changed

12 files changed

+296
-109
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ node_modules
1616
.DS_Store
1717
coverage.html
1818
coverage/
19-
.idea/
19+
.idea/
20+
profile-*/

benchmark/decode.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
1-
/**!
2-
* hessian.js - benchmark/decode.js
3-
*
4-
* Copyright(c) fengmk2 and other contributors.
5-
* MIT Licensed
6-
*
7-
* Authors:
8-
* fengmk2 <[email protected]> (http://fengmk2.github.com)
9-
*/
10-
111
'use strict';
122

13-
/**
14-
* Module dependencies.
15-
*/
16-
173
var ByteBuffer = require('byte');
184
var Benchmark = require('benchmark');
195
var benchmarks = require('beautify-benchmark');

benchmark/decoder.v1.read.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
var bench = require('fastbench');
4+
var java = require('js-to-java');
5+
var hessian = require('..');
6+
7+
var max = 10;
8+
9+
var complexObject = {
10+
$class: 'com.hessiantest.org.MockRequest',
11+
$: {
12+
id: 123,
13+
name: 'getData',
14+
args: [1, makeStr('name', 1), makeStr('a', 200)],
15+
conn: {
16+
$class: 'com.hessiantest.org.MockRequestConnection',
17+
$: {
18+
ctx: java.long(1024),
19+
name: makeStr('p', 200),
20+
}
21+
}
22+
}
23+
};
24+
var complexObjectBuf = hessian.encode(complexObject, '1.0');
25+
console.log(JSON.stringify(hessian.decode(complexObjectBuf, '1.0', true), null, 2));
26+
27+
var run = bench([
28+
function readComplexObject(cb) {
29+
for (var i = 0; i < max; i++) {
30+
hessian.decode(complexObjectBuf, '1.0', true);
31+
}
32+
setImmediate(cb);
33+
},
34+
], 10000);
35+
36+
run(run);
37+
38+
function makeStr(str, concats) {
39+
var s = ''
40+
while (concats--) {
41+
s += str
42+
}
43+
return s
44+
}
45+
46+
// node benchmark/decoder.v1.read.js
47+
// {
48+
// "$class": "com.hessiantest.org.MockRequest",
49+
// "$": {
50+
// "id": {
51+
// "$class": "int",
52+
// "$": 123
53+
// },
54+
// "name": {
55+
// "$class": "java.lang.String",
56+
// "$": "getData"
57+
// },
58+
// "args": {
59+
// "$class": "java.util.ArrayList",
60+
// "$": [
61+
// {
62+
// "$class": "int",
63+
// "$": 1
64+
// },
65+
// {
66+
// "$class": "java.lang.String",
67+
// "$": "name"
68+
// },
69+
// {
70+
// "$class": "java.lang.String",
71+
// "$": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
72+
// }
73+
// ]
74+
// },
75+
// "conn": {
76+
// "$class": "com.hessiantest.org.MockRequestConnection",
77+
// "$": {
78+
// "ctx": {
79+
// "$class": "long",
80+
// "$": 1024
81+
// },
82+
// "name": {
83+
// "$class": "java.lang.String",
84+
// "$": "pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"
85+
// }
86+
// }
87+
// }
88+
// }
89+
// }
90+
// readComplexObject*10000: 2062.029ms
91+
// readComplexObject*10000: 1711.482ms

benchmark/encode.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
1-
/**!
2-
* hessian.js - benchmark/encode.js
3-
*
4-
* Copyright(c) fengmk2 and other contributors.
5-
* MIT Licensed
6-
*
7-
* Authors:
8-
* fengmk2 <[email protected]> (http://fengmk2.github.com)
9-
*/
10-
111
'use strict';
122

13-
/**
14-
* Module dependencies.
15-
*/
16-
173
var ByteBuffer = require('byte');
184
var Benchmark = require('benchmark');
195
var benchmarks = require('beautify-benchmark');
@@ -100,6 +86,20 @@ suite
10086
hessian.encode(complexObject, '1.0');
10187
})
10288
.add('hessian2 encode: complex object', function() {
89+
var complexObject = {
90+
$class: 'com.hessiantest.org.MockRequest',
91+
$: {
92+
id: 123,
93+
name: 'getData',
94+
args: [1, makeStr('name', 1), makeStr('a', 200)],
95+
conn: {
96+
$class: 'com.hessiantest.org.MockRequestConnection',
97+
$: {
98+
ctx: java.long(1024)
99+
}
100+
}
101+
}
102+
};
103103
hessian.encode(complexObject, '2.0');
104104
})
105105

@@ -177,3 +177,11 @@ suite
177177
// hessian2 encode: simple object x 155,580 ops/sec ±0.82% (98 runs sampled)
178178
// hessian1 encode: complex object x 103,974 ops/sec ±1.34% (96 runs sampled)
179179
// hessian2 encode: complex object x 100,160 ops/sec ±1.18% (101 runs sampled)
180+
181+
function makeStr(str, concats) {
182+
var s = ''
183+
while (concats--) {
184+
s += str
185+
}
186+
return s
187+
}

benchmark/encoder.v1.write.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var bench = require('fastbench');
4+
var java = require('js-to-java');
5+
var hessian = require('..');
6+
7+
var max = 10;
8+
9+
var complexObject = {
10+
$class: 'com.hessiantest.org.MockRequest',
11+
$: {
12+
id: 123,
13+
name: 'getData',
14+
args: [1, makeStr('name', 1), makeStr('a', 200)],
15+
conn: {
16+
$class: 'com.hessiantest.org.MockRequestConnection',
17+
$: {
18+
ctx: java.long(1024)
19+
}
20+
}
21+
}
22+
};
23+
console.log(hessian.encode(complexObject, '1.0').length, hessian.encode(complexObject, '1.0'));
24+
25+
var run = bench([
26+
function writeComplexObject(cb) {
27+
for (var i = 0; i < max; i++) {
28+
var complexObject = {
29+
$class: 'com.hessiantest.org.MockRequest',
30+
$: {
31+
id: 123,
32+
name: 'getData',
33+
args: [1, makeStr('name', 1), makeStr('a', 200)],
34+
conn: {
35+
$class: 'com.hessiantest.org.MockRequestConnection',
36+
$: {
37+
ctx: java.long(1024)
38+
}
39+
}
40+
}
41+
};
42+
hessian.encode(complexObject, '1.0');
43+
}
44+
setImmediate(cb);
45+
},
46+
], 10000);
47+
48+
run(run);
49+
50+
function makeStr(str, concats) {
51+
var s = ''
52+
while (concats--) {
53+
s += str
54+
}
55+
return s
56+
}
57+
58+
// node benchmark/encoder.v1.write.js
59+
// 360 <Buffer 4d 74 00 1f 63 6f 6d 2e 68 65 73 73 69 61 6e 74 65 73 74 2e 6f 72 67 2e 4d 6f 63 6b 52 65 71 75 65 73 74 53 00 02 69 64 49 00 00 00 7b 53 00 04 6e 61 ... >
60+
// writeComplexObject*10000: 1214.816ms
61+
// writeComplexObject*10000: 1151.431ms

benchmark/encoder.v2.write.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var bench = require('fastbench');
4+
var java = require('js-to-java');
5+
var hessian = require('..');
6+
7+
var max = 10;
8+
9+
var complexObject = {
10+
$class: 'com.hessiantest.org.MockRequest',
11+
$: {
12+
id: 123,
13+
name: 'getData',
14+
args: [1, makeStr('name', 1), makeStr('a', 200)],
15+
conn: {
16+
$class: 'com.hessiantest.org.MockRequestConnection',
17+
$: {
18+
ctx: java.long(1024)
19+
}
20+
}
21+
}
22+
};
23+
console.log(hessian.encode(complexObject, '2.0').length, hessian.encode(complexObject, '2.0'));
24+
25+
var run = bench([
26+
function writeComplexObject(cb) {
27+
for (var i = 0; i < max; i++) {
28+
var complexObject = {
29+
$class: 'com.hessiantest.org.MockRequest',
30+
$: {
31+
id: 123,
32+
name: 'getData',
33+
args: [1, makeStr('name', 1), makeStr('a', 200)],
34+
conn: {
35+
$class: 'com.hessiantest.org.MockRequestConnection',
36+
$: {
37+
ctx: java.long(1024)
38+
}
39+
}
40+
}
41+
};
42+
hessian.encode(complexObject, '2.0');
43+
}
44+
setImmediate(cb);
45+
},
46+
], 10000);
47+
48+
run(run);
49+
50+
function makeStr(str, concats) {
51+
var s = ''
52+
while (concats--) {
53+
s += str
54+
}
55+
return s
56+
}
57+
58+
// node benchmark/encoder.v2.write.js
59+
// 324 <Buffer 43 1f 63 6f 6d 2e 68 65 73 73 69 61 6e 74 65 73 74 2e 6f 72 67 2e 4d 6f 63 6b 52 65 71 75 65 73 74 94 02 69 64 04 6e 61 6d 65 04 61 72 67 73 04 63 6f ... >
60+
// writeComplexObject*10000: 1162.727ms
61+
// writeComplexObject*10000: 1032.622ms
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
var bench = require('fastbench');
4+
var hessian = require('..');
5+
6+
var max = 10;
7+
8+
console.log(hessian.encode(makeStr('a', 200), '2.0'));
9+
10+
var run = bench([
11+
function writeSmallString(cb) {
12+
for (var i = 0; i < max; i++) {
13+
hessian.encode(makeStr('a', 200), '2.0');
14+
}
15+
setImmediate(cb);
16+
},
17+
], 10000);
18+
19+
run(run);
20+
21+
function makeStr(str, concats) {
22+
var s = ''
23+
while (concats--) {
24+
s += str
25+
}
26+
return s
27+
}

lib/v1/decoder.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**
2-
* hessian.js - lib/v1/decoder.js
3-
* Copyright(c)
4-
* MIT Licensed
5-
*
6-
* Authors:
7-
* dead_horse <[email protected]> (http://deadhorse.me)
8-
* fengmk2 <[email protected]> (http://fengmk2.github.com)
9-
*/
10-
111
'use strict';
122

133
var debug = require('debug')('hessian:v1:decoder');
@@ -261,7 +251,7 @@ proto._readUTF8String = function (len) {
261251
}
262252

263253
while (len--) {
264-
head = this.byteBuffer.get();
254+
head = this.byteBuffer.getByte();
265255
l = utils.lengthOfUTF8(head);
266256
this.byteBuffer.skip(l - 1);
267257
}
@@ -286,11 +276,11 @@ proto._readUTF8String = function (len) {
286276
*/
287277
proto.readString = function (withType) {
288278
var str = '';
289-
var code = this.byteBuffer.get();
279+
var code = this.byteBuffer.getByte();
290280
// get all trunk start with 's'
291281
while (code === 0x73) {
292282
str += this._readUTF8String();
293-
code = this.byteBuffer.get();
283+
code = this.byteBuffer.getByte();
294284
}
295285

296286
if (code === 0x53) {

lib/v1/encoder.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**
2-
* hessian.js - lib/encoder.js
3-
* Copyright(c)
4-
* MIT Licensed
5-
*
6-
* Authors:
7-
* dead_horse <[email protected]> (http://deadhorse.me)
8-
* fengmk2 <[email protected]> (http://fengmk2.github.com)
9-
*/
10-
111
'use strict';
122

133
var ByteBuffer = require('byte');
@@ -156,7 +146,6 @@ proto.writeBytes = function (buf) {
156146
* S 0x00 0x03 [0x01 0x02 0x03]
157147
*/
158148
proto.writeString = function (str) {
159-
this._assertType('writeString', 'string', str);
160149
var offset = 0;
161150

162151
var length = str.length;
@@ -307,7 +296,7 @@ proto._writeObject = function (obj) {
307296
* : {$class: 'java.lang.Map', $: {a: 1}}
308297
*/
309298
proto.writeObject = function (obj) {
310-
if (is.nullOrUndefined(obj) ||
299+
if (is.nullOrUndefined(obj) ||
311300
// : { a: { '$class': 'xxx', '$': null } }
312301
(is.string(obj.$class) && is.nullOrUndefined(obj.$))) {
313302
debug('writeObject with a null');

0 commit comments

Comments
 (0)