Skip to content

Commit b415d49

Browse files
committed
test: use fastbench instead of benchmark
1 parent 8e8cfc6 commit b415d49

File tree

5 files changed

+161
-69
lines changed

5 files changed

+161
-69
lines changed

benchmark/encode.js

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,58 +48,72 @@ var complexObject = {
4848

4949
suite
5050

51-
.add('hessian1 encode: number', function() {
52-
hessian.encode(1, '1.0');
53-
})
54-
.add('hessian2 encode: number', function() {
55-
hessian.encode(1, '2.0');
56-
})
57-
58-
.add('hessian1 encode: date', function() {
59-
hessian.encode(new Date(), '1.0');
60-
})
61-
.add('hessian2 encode: date', function() {
62-
hessian.encode(new Date(), '2.0');
63-
})
64-
65-
.add('hessian1 encode: long', function() {
66-
hessian.encode(java.long(300), '1.0');
67-
})
68-
.add('hessian2 encode: long', function() {
69-
hessian.encode(java.long(300), '2.0');
70-
})
71-
72-
.add('hessian1 encode: string', function() {
73-
hessian.encode('xxx1231231231231xxx123', '1.0');
74-
})
75-
.add('hessian2 encode: string', function() {
76-
hessian.encode('xxx1231231231231xxx123', '2.0');
77-
})
78-
79-
.add('hessian1 encode: [1, 2, 3]', function() {
80-
hessian.encode([1, 2, 3], '1.0');
81-
})
82-
.add('hessian2 encode: [1, 2, 3]', function() {
83-
hessian.encode([1, 2, 3], '2.0');
84-
})
85-
.add('hessian1 encode array', function() {
86-
hessian.encode([1, "name", "xxx1231231231231xxx123"], '1.0');
87-
})
88-
.add('hessian2 encode array', function() {
89-
hessian.encode([1, "name", "xxx1231231231231xxx123"], '2.0');
90-
})
91-
92-
.add('hessian1 encode: simple object', function() {
93-
hessian.encode(simpleObject, '1.0');
94-
})
95-
.add('hessian2 encode: simple object', function() {
96-
hessian.encode(simpleObject, '2.0');
97-
})
98-
99-
.add('hessian1 encode: complex object', function() {
100-
hessian.encode(complexObject, '1.0');
101-
})
51+
// .add('hessian1 encode: number', function() {
52+
// hessian.encode(1, '1.0');
53+
// })
54+
// .add('hessian2 encode: number', function() {
55+
// hessian.encode(1, '2.0');
56+
// })
57+
//
58+
// .add('hessian1 encode: date', function() {
59+
// hessian.encode(new Date(), '1.0');
60+
// })
61+
// .add('hessian2 encode: date', function() {
62+
// hessian.encode(new Date(), '2.0');
63+
// })
64+
//
65+
// .add('hessian1 encode: long', function() {
66+
// hessian.encode(java.long(300), '1.0');
67+
// })
68+
// .add('hessian2 encode: long', function() {
69+
// hessian.encode(java.long(300), '2.0');
70+
// })
71+
//
72+
// .add('hessian1 encode: string', function() {
73+
// hessian.encode('xxx1231231231231xxx123', '1.0');
74+
// })
75+
// .add('hessian2 encode: string', function() {
76+
// hessian.encode('xxx1231231231231xxx123', '2.0');
77+
// })
78+
//
79+
// .add('hessian1 encode: [1, 2, 3]', function() {
80+
// hessian.encode([1, 2, 3], '1.0');
81+
// })
82+
// .add('hessian2 encode: [1, 2, 3]', function() {
83+
// hessian.encode([1, 2, 3], '2.0');
84+
// })
85+
// .add('hessian1 encode array', function() {
86+
// hessian.encode([1, "name", "xxx1231231231231xxx123"], '1.0');
87+
// })
88+
// .add('hessian2 encode array', function() {
89+
// hessian.encode([1, "name", "xxx1231231231231xxx123"], '2.0');
90+
// })
91+
//
92+
// .add('hessian1 encode: simple object', function() {
93+
// hessian.encode(simpleObject, '1.0');
94+
// })
95+
// .add('hessian2 encode: simple object', function() {
96+
// hessian.encode(simpleObject, '2.0');
97+
// })
98+
99+
// .add('hessian1 encode: complex object', function() {
100+
// hessian.encode(complexObject, '1.0');
101+
// })
102102
.add('hessian2 encode: complex object', function() {
103+
var complexObject = {
104+
$class: 'com.hessiantest.org.MockRequest',
105+
$: {
106+
id: 123,
107+
name: 'getData',
108+
args: [1, makeStr('name', 1), makeStr('a', 200)],
109+
conn: {
110+
$class: 'com.hessiantest.org.MockRequestConnection',
111+
$: {
112+
ctx: java.long(1024)
113+
}
114+
}
115+
}
116+
};
103117
hessian.encode(complexObject, '2.0');
104118
})
105119

@@ -177,3 +191,11 @@ suite
177191
// hessian2 encode: simple object x 155,580 ops/sec ±0.82% (98 runs sampled)
178192
// hessian1 encode: complex object x 103,974 ops/sec ±1.34% (96 runs sampled)
179193
// hessian2 encode: complex object x 100,160 ops/sec ±1.18% (101 runs sampled)
194+
195+
function makeStr(str, concats) {
196+
var s = ''
197+
while (concats--) {
198+
s += str
199+
}
200+
return s
201+
}

benchmark/encoder.v2.write.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
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/v2/encoder.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
/**
2-
* hessian.js - lib/v2/encoder.js
3-
*
4-
* Copyright(c)
5-
* MIT Licensed
6-
*
7-
* Authors:
8-
* fengmk2 <[email protected]> (http://fengmk2.github.com)
9-
*/
10-
11-
"use strict";
12-
13-
/**
14-
* Module dependencies.
15-
*/
1+
'use strict';
162

173
var debug = require('debug')('hessian:v2:encoder');
4+
var flatstr = require('flatstr');
185
var is = require('is-type-of');
196
var util = require('util');
207
var EncoderV1 = require('../v1/encoder');
@@ -367,8 +354,6 @@ proto.writeBytes = function (buf) {
367354
* ```
368355
*/
369356
proto.writeString = function (str) {
370-
this._assertType('writeString', 'string', str);
371-
372357
var length = str.length;
373358
var strOffset = 0;
374359
var sublen;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"byte": "^1.1.6",
3838
"debug": "^2.6.8",
39+
"flatstr": "^1.0.5",
3940
"is-type-of": "^1.1.0",
4041
"long": "^3.2.0",
4142
"utility": "^1.12.0"
@@ -44,6 +45,7 @@
4445
"autod": "*",
4546
"beautify-benchmark": "*",
4647
"benchmark": "*",
48+
"fastbench": "^1.0.1",
4749
"istanbul": "*",
4850
"js-to-java": "2",
4951
"jshint": "*",
@@ -52,4 +54,4 @@
5254
"engines": {
5355
"node": ">= 0.12.0"
5456
}
55-
}
57+
}

0 commit comments

Comments
 (0)