Browse Source

Benchmarks for block serialization & 4x performance improvement.

patch-2
Braydon Fuller 10 years ago
parent
commit
b5ada707bd
  1. 1
      benchmark/block-357238.json
  2. 8
      benchmark/package.json
  3. 63
      benchmark/serialization.js
  4. 6
      lib/block/block.js
  5. 24
      lib/transaction/output.js

1
benchmark/block-357238.json

File diff suppressed because one or more lines are too long

8
benchmark/package.json

@ -0,0 +1,8 @@
{
"devDependencies": {
"bcoin": "0.15.0",
"bitcoinjs-lib": "^1.5.7",
"fullnode": "^0.9.0",
"benchmark": "^1.0.0"
}
}

63
benchmark/serialization.js

@ -0,0 +1,63 @@
'use strict';
var benchmark = require('benchmark');
var bitcore = require('..');
var bitcoinjs = require('bitcoinjs-lib');
var bcoin = require('bcoin');
var async = require('async');
var fullnode = require('fullnode');
var blockData = require('./block-357238.json');
var maxTime = 20;
console.log('Benchmarking Block/Transaction Serialization');
console.log('---------------------------------------');
async.series([
function(next) {
var block1;
var block2;
var block3;
function bitcoreTest() {
block1 = bitcore.Block.fromString(blockData);
}
function bitcoinJSTest() {
block2 = bitcoinjs.Block.fromHex(blockData);
}
var parser = new bcoin.protocol.parser();
function bcoinTest() {
var raw = bcoin.utils.toArray(blockData, 'hex');
var data = parser.parseBlock(raw);
block3 = new bcoin.block(data, 'block');
}
var blockDataMessage = '0000000000000000' + blockData; // add mock leading magic and size
function fullnodeTest() {
fullnode.Block().fromHex(blockDataMessage);
}
var suite = new benchmark.Suite();
suite.add('bitcore', bitcoreTest, {maxTime: maxTime});
suite.add('bitcoinjs', bitcoinJSTest, {maxTime: maxTime});
suite.add('bcoin', bcoinTest, {maxTime: maxTime});
suite.add('fullnode', fullnodeTest, {maxTime: maxTime});
suite
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
console.log('----------------------------------------------------------------------');
next();
})
.run();
}
], function(err) {
console.log('Finished');
});

6
lib/block/block.js

@ -69,7 +69,11 @@ Block._fromJSON = function _fromJSON(data) {
Block._fromObject = function _fromObject(data) {
var transactions = [];
data.transactions.forEach(function(tx) {
transactions.push(Transaction().fromJSON(tx));
if (tx instanceof Transaction) {
transactions.push(tx);
} else {
transactions.push(Transaction().fromJSON(tx));
}
});
var info = {
header: BlockHeader.fromObject(data.header),

24
lib/transaction/output.js

@ -16,14 +16,18 @@ function Output(args) {
if (!(this instanceof Output)) {
return new Output(args);
}
if (JSUtil.isValidJSON(args)) {
return Output.fromJSON(args);
} else if (_.isObject(args)) {
if (_.isObject(args)) {
this.satoshis = args.satoshis;
if (_.isString(args.script) && JSUtil.isHexa(args.script)) {
args.script = new buffer.Buffer(args.script, 'hex');
if (bufferUtil.isBuffer(args.script)) {
this._scriptBuffer = args.script;
} else {
if (_.isString(args.script) && JSUtil.isHexa(args.script)) {
args.script = new buffer.Buffer(args.script, 'hex');
}
this.setScript(args.script);
}
this.setScript(args.script);
} else if (JSUtil.isValidJSON(args)) {
return Output.fromJSON(args);
} else {
throw new TypeError('Unrecognized argument for Output');
}
@ -33,7 +37,13 @@ Object.defineProperty(Output.prototype, 'script', {
configurable: false,
enumerable: true,
get: function() {
return this._script;
if (this._script) {
return this._script;
} else {
this.setScriptFromBuffer(this._scriptBuffer);
return this._script;
}
}
});

Loading…
Cancel
Save