-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
146 lines (139 loc) · 4.44 KB
/
index.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');
const suite = new Benchmark.Suite();
const assert = require('assert');
const hessian = require('hessian.js-1');
const protocol = require('../');
const classMap = require('../test/fixtures/class_map');
const obj = {
$class: 'com.alipay.test.TestObj',
$: {
b: true,
name: 'testname',
field: 'xxxxx',
testObj2: { name: 'xxx', finalField: 'xxx' },
testEnum: { name: 'B' },
testEnum2: [{ name: 'B' }, { name: 'C' }],
bs: new Buffer([ 0x02, 0x00, 0x01, 0x07 ]),
list1: [{ name: 'A' }, { name: 'B' }],
list2: [ 2017, 2016 ],
list3: [{ name: 'aaa', finalField: 'xxx' },
{ name: 'bbb', finalField: 'xxx' },
],
list4: [ 'xxx', 'yyy' ],
list5: [ new Buffer([ 0x02, 0x00, 0x01, 0x07 ]), new Buffer([ 0x02, 0x00, 0x01, 0x06 ]) ],
map1: { 2017: { name: 'B' } },
map2: new Map([
[ 2107, 2016 ],
]),
map3: {},
map4: { xxx: 'yyy' },
map5: { 2017: new Buffer([ 0x02, 0x00, 0x01, 0x06 ]) },
},
};
const targetObj = {
$class: 'com.alipay.test.TestObj',
$: {
b: { $class: 'boolean', $: true },
testObj2: { $class: 'com.alipay.test.sub.TestObj2', $: { name: { $class: 'java.lang.String', $: 'xxx' }, finalField: { $class: 'java.lang.String', $: 'xxx' } } },
name: { $class: 'java.lang.String', $: 'testname' },
field: { $class: 'java.lang.String', $: 'xxxxx' },
testEnum: { $class: 'com.alipay.test.TestEnum', $: { name: 'B' } },
testEnum2: {
$class: '[com.alipay.test.TestEnum',
$: [
{ $class: 'com.alipay.test.TestEnum', $: { name: 'B' } },
{ $class: 'com.alipay.test.TestEnum', $: { name: 'C' } },
],
},
bs: new Buffer([ 0x02, 0x00, 0x01, 0x07 ]),
list1: {
$class: 'java.util.List',
$: [
{ $class: 'com.alipay.test.TestEnum', $: { name: 'A' } }, { $class: 'com.alipay.test.TestEnum', $: { name: 'B' } },
],
},
list2: {
$class: 'java.util.List',
$: [
{ $class: 'java.lang.Integer', $: 2017 }, { $class: 'java.lang.Integer', $: 2016 },
],
},
list3: {
$class: 'java.util.List',
$: [
{ $class: 'com.alipay.test.sub.TestObj2', $: { name: { $class: 'java.lang.String', $: 'aaa' }, finalField: { $class: 'java.lang.String', $: 'xxx' } } }, { $class: 'com.alipay.test.sub.TestObj2', $: { name: { $class: 'java.lang.String', $: 'bbb' }, finalField: { $class: 'java.lang.String', $: 'xxx' } } },
],
},
list4: {
$class: 'java.util.List',
$: [
{ $class: 'java.lang.String', $: 'xxx' }, { $class: 'java.lang.String', $: 'yyy' },
],
},
list5: {
$class: 'java.util.List',
$: [ new Buffer([ 0x02, 0x00, 0x01, 0x07 ]), new Buffer([ 0x02, 0x00, 0x01, 0x06 ]) ],
},
map1: {
$class: 'java.util.Map',
$: new Map([
[{ $class: 'java.lang.Long', $: 2017 }, {
$class: 'com.alipay.test.TestEnum',
$: { name: 'B' },
}],
]),
},
map2: {
$class: 'java.util.Map',
$: new Map([
[{ $class: 'java.lang.Integer', $: 2107 }, { $class: 'java.lang.Integer', $: 2016 }],
]),
},
map3: { $class: 'java.util.Map', $: {} },
map4: { $class: 'java.util.Map', $: { xxx: { $class: 'java.lang.String', $: 'yyy' } } },
map5: {
$class: 'java.util.Map',
$: new Map([
[ '2017', new Buffer([ 0x02, 0x00, 0x01, 0x06 ]) ],
]),
},
},
};
let buf1 = hessian.encode(targetObj, '1.0');
let buf2 = protocol.encode(obj, '1.0', classMap);
assert.deepEqual(buf1, buf2);
buf1 = hessian.encode(targetObj, '2.0');
buf2 = protocol.encode(obj, '2.0', classMap);
assert.deepEqual(buf1, buf2);
// add tests
suite
.add('hessian old - 1.0', function() {
hessian.encode(targetObj, '1.0');
})
.add('hessian new - 1.0', function() {
protocol.encode({
$class: 'com.alipay.test.TestObj',
$: obj,
}, '1.0', classMap);
})
.add('hessian old - 2.0', function() {
hessian.encode(targetObj, '2.0');
})
.add('hessian new - 2.0', function() {
protocol.encode({
$class: 'com.alipay.test.TestObj',
$: obj,
}, '2.0', classMap);
})
.on('cycle', function(event) {
benchmarks.add(event.target);
})
.on('start', function() {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', function done() {
benchmarks.log();
})
.run({ async: false });