-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: xieweicheng <xieweicheng@bytedance.com>
- Loading branch information
Showing
19 changed files
with
339 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,10 @@ | |
"max-len": [ | ||
"error", | ||
{ "code": 80 } | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
] | ||
}, | ||
"overrides": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ node_modules | |
# Vim | ||
*.swp | ||
|
||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "benchmarks", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "writeToBuffer.js", | ||
"scripts": { | ||
"bench-1": "node writeToBuffer.js", | ||
"bench-2":"VERSION=2.0.2 node writeToBuffer.js", | ||
"bench":"npm run bench-1 && npm run bench-2" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"mcnbt": "2.0.1", | ||
"benchmark": "^2.1.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
'use strict'; | ||
|
||
/** | ||
v: 2.0.1 , r: 1b x 66,319 ops/sec ±70.40% (88 runs sampled) | ||
v: 2.0.1 , r: 1kb x 32,589 ops/sec ±0.96% (90 runs sampled) | ||
v: 2.0.1 , r: 2kb x 18,932 ops/sec ±0.51% (93 runs sampled) | ||
v: 2.0.1 , r: 4kb x 10,554 ops/sec ±0.44% (94 runs sampled) | ||
v: 2.0.1 , r: 8kb x 5,425 ops/sec ±0.56% (91 runs sampled) | ||
v: 2.0.2 , r: 1b x 159,682 ops/sec ±0.49% (92 runs sampled) | ||
v: 2.0.2 , r: 1kb x 35,753 ops/sec ±0.57% (90 runs sampled) | ||
v: 2.0.2 , r: 2kb x 19,579 ops/sec ±0.67% (95 runs sampled) | ||
v: 2.0.2 , r: 4kb x 10,397 ops/sec ±0.48% (95 runs sampled) | ||
v: 2.0.2 , r: 8kb x 5,168 ops/sec ±0.35% (95 runs sampled) | ||
*/ | ||
|
||
let NBT; | ||
let version; | ||
|
||
if (process.env.VERSION === '2.0.2') { | ||
NBT = require('../'); | ||
version = '2.0.2'; | ||
} else { | ||
NBT = require('mcnbt'); | ||
version = '2.0.1'; | ||
} | ||
|
||
const Benchmark = require('benchmark'); | ||
|
||
function test(byteCount = 5) { | ||
const short = new NBT.Tags.TAGShort(); | ||
short.setValue(100); | ||
short.id = 'short'; | ||
|
||
const long = new NBT.Tags.TAGLong(); | ||
long.setValue(1000); | ||
long.id = 'long'; | ||
|
||
const float = new NBT.Tags.TAGFloat(); | ||
float.setValue(1.0); | ||
float.id = 'float'; | ||
|
||
const double = new NBT.Tags.TAGDouble(); | ||
double.setValue(1.0); | ||
double.id = 'double'; | ||
|
||
const byte = new NBT.Tags.TAGByte(); | ||
byte.setValue(1); | ||
byte.id = 'byte'; | ||
|
||
const string = new NBT.Tags.TAGString(); | ||
let str = 'str'; | ||
for (let i = 0; i < byteCount; i++) { | ||
str += 's'; | ||
} | ||
string.setValue(str); | ||
string.id = 'string'; | ||
|
||
const intArray = new NBT.Tags.TAGIntArray(); | ||
intArray.setValue(new Array(byteCount).fill(0)); | ||
intArray.id = 'intArray'; | ||
|
||
const int = new NBT.Tags.TAGInt(); | ||
int.setValue(1); | ||
int.id = 'int'; | ||
|
||
const list = new NBT.Tags.TAGList(); | ||
list.id = 'list'; | ||
|
||
const l = [ short, short ]; | ||
list.setValue(l); | ||
|
||
const resultNBT = new NBT.Tags.TAGCompound(); | ||
resultNBT.id = 'result'; | ||
resultNBT.setValue({ | ||
short, | ||
long, | ||
float, | ||
double, | ||
byte, | ||
string, | ||
intArray, | ||
int, | ||
list, | ||
}); | ||
|
||
const nbt = new NBT(); | ||
nbt.root.result = resultNBT; | ||
|
||
return nbt.writeToBuffer().length; | ||
} | ||
console.log('1b:byteLength:', test(), 'b'); | ||
console.log('1kb:byteLength:', test(1024), 'b'); | ||
console.log('2kb:byteLength:', test(2048), 'b'); | ||
console.log('4kb:byteLength:', test(4096), 'b'); | ||
console.log('8kb:byteLength:', test(8192), 'b'); | ||
|
||
const suite = new Benchmark.Suite('', {}); | ||
|
||
suite | ||
.add('1b', () => { | ||
test(1); | ||
}) | ||
.add('1kb', () => { | ||
test(1024); | ||
}) | ||
.add('2kb', () => { | ||
test(2048); | ||
}) | ||
.add('4kb', () => { | ||
test(4096); | ||
}) | ||
.add('8kb', () => { | ||
test(8192); | ||
}); | ||
|
||
suite | ||
.on('cycle', event => { | ||
console.log('v:', version, ', r:', String(event.target)); | ||
}) | ||
.on('complete', () => { | ||
// | ||
}) | ||
.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
|
||
const defaultPoolSize = 64 * 1024; | ||
if (Buffer.poolSize < defaultPoolSize) { | ||
Buffer.poolSize = defaultPoolSize; | ||
} | ||
|
||
class Bvffer { | ||
constructor(buffByteLength) { | ||
/** | ||
* Buffer store data | ||
*/ | ||
this.buffer = Buffer.allocUnsafe(buffByteLength); | ||
/** | ||
* data length in buffer | ||
*/ | ||
this.dataByteLength = 0; | ||
} | ||
|
||
get dataLength() { | ||
return this.dataByteLength; | ||
} | ||
|
||
get buff() { | ||
return this.buffer; | ||
} | ||
|
||
/** | ||
* write buffer data to buffer | ||
* @param {Buffer} value data to write | ||
*/ | ||
writeBuffer(value) { | ||
this.writeUInt16BE(value.length); | ||
|
||
value.copy(this.buffer, this.dataByteLength); | ||
this.dataByteLength += value.length; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeUInt8(value) { | ||
this.buff[this.dataByteLength] = value; | ||
this.dataByteLength += 1; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeInt8(value) { | ||
this.buff[this.dataByteLength] = value; | ||
this.dataByteLength += 1; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeUInt16BE(value) { | ||
this.buffer.writeUInt16BE(value, this.dataByteLength); | ||
this.dataByteLength += 2; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeInt16BE(value) { | ||
this.buffer.writeInt16BE(value, this.dataByteLength); | ||
this.dataByteLength += 2; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeUInt32BE(value) { | ||
this.buffer.writeUInt32BE(value, this.dataByteLength); | ||
this.dataByteLength += 4; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeInt32BE(value) { | ||
this.buffer.writeInt32BE(value, this.dataByteLength); | ||
this.dataByteLength += 4; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeDoubleBE(value) { | ||
this.buffer.writeDoubleBE(value, this.dataByteLength); | ||
this.dataByteLength += 8; | ||
} | ||
|
||
/** | ||
* @param {number} value value | ||
*/ | ||
writeFloatBE(value) { | ||
this.buffer.writeFloatBE(value, this.dataByteLength); | ||
this.dataByteLength += 4; | ||
} | ||
} | ||
|
||
exports.Bvffer = Bvffer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.