Skip to content

feat: use getByte() instead of get() #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ node_modules
.DS_Store
coverage.html
coverage/
.idea/
.idea/
profile-*/
14 changes: 0 additions & 14 deletions benchmark/decode.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
/**!
* hessian.js - benchmark/decode.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var ByteBuffer = require('byte');
var Benchmark = require('benchmark');
var benchmarks = require('beautify-benchmark');
Expand Down
91 changes: 91 additions & 0 deletions benchmark/decoder.v1.read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

var bench = require('fastbench');
var java = require('js-to-java');
var hessian = require('..');

var max = 10;

var complexObject = {
$class: 'com.hessiantest.org.MockRequest',
$: {
id: 123,
name: 'getData',
args: [1, makeStr('name', 1), makeStr('a', 200)],
conn: {
$class: 'com.hessiantest.org.MockRequestConnection',
$: {
ctx: java.long(1024),
name: makeStr('p', 200),
}
}
}
};
var complexObjectBuf = hessian.encode(complexObject, '1.0');
console.log(JSON.stringify(hessian.decode(complexObjectBuf, '1.0', true), null, 2));

var run = bench([
function readComplexObject(cb) {
for (var i = 0; i < max; i++) {
hessian.decode(complexObjectBuf, '1.0', true);
}
setImmediate(cb);
},
], 10000);

run(run);

function makeStr(str, concats) {
var s = ''
while (concats--) {
s += str
}
return s
}

// node benchmark/decoder.v1.read.js
// {
// "$class": "com.hessiantest.org.MockRequest",
// "$": {
// "id": {
// "$class": "int",
// "$": 123
// },
// "name": {
// "$class": "java.lang.String",
// "$": "getData"
// },
// "args": {
// "$class": "java.util.ArrayList",
// "$": [
// {
// "$class": "int",
// "$": 1
// },
// {
// "$class": "java.lang.String",
// "$": "name"
// },
// {
// "$class": "java.lang.String",
// "$": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
// }
// ]
// },
// "conn": {
// "$class": "com.hessiantest.org.MockRequestConnection",
// "$": {
// "ctx": {
// "$class": "long",
// "$": 1024
// },
// "name": {
// "$class": "java.lang.String",
// "$": "pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"
// }
// }
// }
// }
// }
// readComplexObject*10000: 2062.029ms
// readComplexObject*10000: 1711.482ms
36 changes: 22 additions & 14 deletions benchmark/encode.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
/**!
* hessian.js - benchmark/encode.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

/**
* Module dependencies.
*/

var ByteBuffer = require('byte');
var Benchmark = require('benchmark');
var benchmarks = require('beautify-benchmark');
Expand Down Expand Up @@ -100,6 +86,20 @@ suite
hessian.encode(complexObject, '1.0');
})
.add('hessian2 encode: complex object', function() {
var complexObject = {
$class: 'com.hessiantest.org.MockRequest',
$: {
id: 123,
name: 'getData',
args: [1, makeStr('name', 1), makeStr('a', 200)],
conn: {
$class: 'com.hessiantest.org.MockRequestConnection',
$: {
ctx: java.long(1024)
}
}
}
};
hessian.encode(complexObject, '2.0');
})

Expand Down Expand Up @@ -177,3 +177,11 @@ suite
// hessian2 encode: simple object x 155,580 ops/sec ±0.82% (98 runs sampled)
// hessian1 encode: complex object x 103,974 ops/sec ±1.34% (96 runs sampled)
// hessian2 encode: complex object x 100,160 ops/sec ±1.18% (101 runs sampled)

function makeStr(str, concats) {
var s = ''
while (concats--) {
s += str
}
return s
}
61 changes: 61 additions & 0 deletions benchmark/encoder.v1.write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

var bench = require('fastbench');
var java = require('js-to-java');
var hessian = require('..');

var max = 10;

var complexObject = {
$class: 'com.hessiantest.org.MockRequest',
$: {
id: 123,
name: 'getData',
args: [1, makeStr('name', 1), makeStr('a', 200)],
conn: {
$class: 'com.hessiantest.org.MockRequestConnection',
$: {
ctx: java.long(1024)
}
}
}
};
console.log(hessian.encode(complexObject, '1.0').length, hessian.encode(complexObject, '1.0'));

var run = bench([
function writeComplexObject(cb) {
for (var i = 0; i < max; i++) {
var complexObject = {
$class: 'com.hessiantest.org.MockRequest',
$: {
id: 123,
name: 'getData',
args: [1, makeStr('name', 1), makeStr('a', 200)],
conn: {
$class: 'com.hessiantest.org.MockRequestConnection',
$: {
ctx: java.long(1024)
}
}
}
};
hessian.encode(complexObject, '1.0');
}
setImmediate(cb);
},
], 10000);

run(run);

function makeStr(str, concats) {
var s = ''
while (concats--) {
s += str
}
return s
}

// node benchmark/encoder.v1.write.js
// 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 ... >
// writeComplexObject*10000: 1214.816ms
// writeComplexObject*10000: 1151.431ms
61 changes: 61 additions & 0 deletions benchmark/encoder.v2.write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

var bench = require('fastbench');
var java = require('js-to-java');
var hessian = require('..');

var max = 10;

var complexObject = {
$class: 'com.hessiantest.org.MockRequest',
$: {
id: 123,
name: 'getData',
args: [1, makeStr('name', 1), makeStr('a', 200)],
conn: {
$class: 'com.hessiantest.org.MockRequestConnection',
$: {
ctx: java.long(1024)
}
}
}
};
console.log(hessian.encode(complexObject, '2.0').length, hessian.encode(complexObject, '2.0'));

var run = bench([
function writeComplexObject(cb) {
for (var i = 0; i < max; i++) {
var complexObject = {
$class: 'com.hessiantest.org.MockRequest',
$: {
id: 123,
name: 'getData',
args: [1, makeStr('name', 1), makeStr('a', 200)],
conn: {
$class: 'com.hessiantest.org.MockRequestConnection',
$: {
ctx: java.long(1024)
}
}
}
};
hessian.encode(complexObject, '2.0');
}
setImmediate(cb);
},
], 10000);

run(run);

function makeStr(str, concats) {
var s = ''
while (concats--) {
s += str
}
return s
}

// node benchmark/encoder.v2.write.js
// 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 ... >
// writeComplexObject*10000: 1162.727ms
// writeComplexObject*10000: 1032.622ms
27 changes: 27 additions & 0 deletions benchmark/encoder.v2.writeString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var bench = require('fastbench');
var hessian = require('..');

var max = 10;

console.log(hessian.encode(makeStr('a', 200), '2.0'));

var run = bench([
function writeSmallString(cb) {
for (var i = 0; i < max; i++) {
hessian.encode(makeStr('a', 200), '2.0');
}
setImmediate(cb);
},
], 10000);

run(run);

function makeStr(str, concats) {
var s = ''
while (concats--) {
s += str
}
return s
}
16 changes: 3 additions & 13 deletions lib/v1/decoder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**
* hessian.js - lib/v1/decoder.js
* Copyright(c)
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

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

while (len--) {
head = this.byteBuffer.get();
head = this.byteBuffer.getByte();
l = utils.lengthOfUTF8(head);
this.byteBuffer.skip(l - 1);
}
Expand All @@ -286,11 +276,11 @@ proto._readUTF8String = function (len) {
*/
proto.readString = function (withType) {
var str = '';
var code = this.byteBuffer.get();
var code = this.byteBuffer.getByte();
// get all trunk start with 's'
while (code === 0x73) {
str += this._readUTF8String();
code = this.byteBuffer.get();
code = this.byteBuffer.getByte();
}

if (code === 0x53) {
Expand Down
13 changes: 1 addition & 12 deletions lib/v1/encoder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/**
* hessian.js - lib/encoder.js
* Copyright(c)
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/

'use strict';

var ByteBuffer = require('byte');
Expand Down Expand Up @@ -156,7 +146,6 @@ proto.writeBytes = function (buf) {
* S 0x00 0x03 [0x01 0x02 0x03]
*/
proto.writeString = function (str) {
this._assertType('writeString', 'string', str);
var offset = 0;

var length = str.length;
Expand Down Expand Up @@ -307,7 +296,7 @@ proto._writeObject = function (obj) {
* : {$class: 'java.lang.Map', $: {a: 1}}
*/
proto.writeObject = function (obj) {
if (is.nullOrUndefined(obj) ||
if (is.nullOrUndefined(obj) ||
// : { a: { '$class': 'xxx', '$': null } }
(is.string(obj.$class) && is.nullOrUndefined(obj.$))) {
debug('writeObject with a null');
Expand Down
Loading