bench: clean up benchmarks.

This commit is contained in:
Christopher Jeffrey 2016-12-10 13:31:38 -08:00
parent 1d6eb802e7
commit 5e3b05ff11
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 153 additions and 165 deletions

View File

@ -1,36 +1,38 @@
'use strict'; 'use strict';
var chachapoly = require('../lib/crypto/chachapoly'); var chachapoly = require('../lib/crypto/chachapoly');
var crypto = require('../lib/crypto/crypto');
var bench = require('./bench'); var bench = require('./bench');
var i, chacha, iv, poly, key, data, end;
console.log('note: rate measured in kb/s'); console.log('note: rate measured in kb/s');
var chacha = new chachapoly.ChaCha20(); chacha = new chachapoly.ChaCha20();
var iv = new Buffer('0102030405060708', 'hex'); iv = new Buffer('0102030405060708', 'hex');
chacha.init(iv, 0); chacha.init(iv, 0);
var data = new Buffer(32); data = new Buffer(32);
for (var i = 0; i < 32; i++) for (i = 0; i < 32; i++)
data[i] = i; data[i] = i;
var end = bench('encrypt'); end = bench('encrypt');
for (var i = 0; i < 1000000; i++) for (i = 0; i < 1000000; i++)
chacha.encrypt(data); chacha.encrypt(data);
end(i * 32 / 1024); end(i * 32 / 1024);
var poly = new chachapoly.Poly1305(); poly = new chachapoly.Poly1305();
var key = new Buffer('000102030405060708090a0b0c0d0e0f', 'hex'); key = new Buffer('000102030405060708090a0b0c0d0e0f', 'hex');
poly.init(key); poly.init(key);
var data = new Buffer(32); data = new Buffer(32);
for (var i = 0; i < 32; i++) for (i = 0; i < 32; i++)
data[i] = i & 0xff; data[i] = i & 0xff;
var end = bench('update'); end = bench('update');
for (var i = 0; i < 1000000; i++) for (i = 0; i < 1000000; i++)
poly.update(data); poly.update(data);
end(i * 32 / 1024); end(i * 32 / 1024);
var end = bench('finish'); end = bench('finish');
for (var i = 0; i < 1000000; i++) { for (i = 0; i < 1000000; i++) {
poly.init(key); poly.init(key);
poly.update(data); poly.update(data);
poly.finish(); poly.finish();
@ -38,8 +40,7 @@ for (var i = 0; i < 1000000; i++) {
end(i * 32 / 1024); end(i * 32 / 1024);
// For reference: // For reference:
var crypto = require('../lib/crypto/crypto'); end = bench('sha256');
var end = bench('sha256'); for (i = 0; i < 1000000; i++)
for (var i = 0; i < 1000000; i++)
crypto.hash256(data); crypto.hash256(data);
end(i * 32 / 1024); end(i * 32 / 1024);

View File

@ -1,44 +1,38 @@
'use strict'; 'use strict';
var BN = require('bn.js');
var constants = require('../lib/protocol/constants');
var util = require('../lib/utils/util');
var assert = require('assert');
var scriptTypes = constants.scriptTypes;
var bench = require('./bench');
var fs = require('fs'); var fs = require('fs');
var assert = require('assert');
var util = require('../lib/utils/util');
var Coins = require('../lib/blockchain/coins'); var Coins = require('../lib/blockchain/coins');
var TX = require('../lib/primitives/tx'); var TX = require('../lib/primitives/tx');
var bench = require('./bench');
var wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8'); var wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8');
var i, j, coins, raw, end;
wtx = TX.fromRaw(wtx.trim(), 'hex'); wtx = TX.fromRaw(wtx.trim(), 'hex');
coins = Coins.fromTX(wtx);
var coins = Coins.fromTX(wtx); end = bench('serialize');
var raw; for (i = 0; i < 10000; i++)
//raw = coins.toRaw();
//console.log(Coins.fromRaw(raw));
var end = bench('serialize');
for (var i = 0; i < 10000; i++)
raw = coins.toRaw(); raw = coins.toRaw();
end(i); end(i);
var end = bench('parse'); end = bench('parse');
for (var i = 0; i < 10000; i++) for (i = 0; i < 10000; i++)
Coins.fromRaw(raw); Coins.fromRaw(raw);
end(i); end(i);
var end = bench('parse-single'); end = bench('parse-single');
var hash = wtx.hash('hex'); hash = wtx.hash('hex');
for (var i = 0; i < 10000; i++) for (i = 0; i < 10000; i++)
Coins.parseCoin(raw, hash, 5); Coins.parseCoin(raw, hash, 5);
end(i); end(i);
var coins = Coins.fromRaw(raw); coins = Coins.fromRaw(raw);
var end = bench('get'); end = bench('get');
var j;
for (var i = 0; i < 10000; i++) for (i = 0; i < 10000; i++)
for (var j = 0; j < coins.outputs.length; j++) for (j = 0; j < coins.outputs.length; j++)
coins.get(j); coins.get(j);
end(i * coins.outputs.length); end(i * coins.outputs.length);

View File

@ -1,19 +1,17 @@
'use strict'; 'use strict';
var BN = require('bn.js');
var bcoin = require('../').set('main');
var constants = bcoin.constants;
var util = bcoin.util;
var assert = require('assert'); var assert = require('assert');
var bench = require('./bench'); var bench = require('./bench');
var Mnemonic = bcoin.hd.Mnemonic; var HD = require('../lib/hd');
var Mnemonic = require('../lib/hd/mnemonic');
var key = bcoin.hd.fromMnemonic(); var key = HD.fromMnemonic();
var phrase = key.mnemonic.getPhrase(); var phrase = key.mnemonic.getPhrase();
var i, end;
assert.equal(Mnemonic.fromPhrase(phrase).getPhrase(), phrase); assert.equal(Mnemonic.fromPhrase(phrase).getPhrase(), phrase);
var end = bench('fromPhrase'); end = bench('fromPhrase');
for (var i = 0; i < 10000; i++) for (i = 0; i < 10000; i++)
Mnemonic.fromPhrase(phrase); Mnemonic.fromPhrase(phrase);
end(i); end(i);

View File

@ -1,17 +1,14 @@
'use strict'; 'use strict';
var BN = require('bn.js');
var bcoin = require('../').set('main');
var constants = bcoin.constants;
var util = bcoin.util;
var assert = require('assert');
var scriptTypes = constants.scriptTypes;
var opcodes = constants.opcodes;
var bench = require('./bench');
var fs = require('fs'); var fs = require('fs');
var Script = bcoin.script; var assert = require('assert');
var constants = require('../lib/protocol/constants');
bcoin.cache(); var util = require('../lib/utils/util');
var crypto = require('../lib/crypto/crypto');
var Script = require('../lib/script/script');
var bench = require('./bench');
var opcodes = constants.opcodes;
var i, hashes, end;
Script.prototype.fromPubkeyhashOld = function fromScripthash(hash) { Script.prototype.fromPubkeyhashOld = function fromScripthash(hash) {
assert(Buffer.isBuffer(hash) && hash.length === 20); assert(Buffer.isBuffer(hash) && hash.length === 20);
@ -28,16 +25,16 @@ Script.fromPubkeyhashOld = function fromScripthash(hash) {
return new Script().fromPubkeyhashOld(hash); return new Script().fromPubkeyhashOld(hash);
}; };
var hashes = []; hashes = [];
for (var i = 0; i < 100000; i++) for (i = 0; i < 100000; i++)
hashes.push(bcoin.crypto.randomBytes(20)); hashes.push(crypto.randomBytes(20));
var end = bench('old'); end = bench('old');
for (var i = 0; i < hashes.length; i++) for (i = 0; i < hashes.length; i++)
Script.fromPubkeyhashOld(hashes[i]); Script.fromPubkeyhashOld(hashes[i]);
end(i); end(i);
var end = bench('hash'); end = bench('hash');
for (var i = 0; i < hashes.length; i++) for (i = 0; i < hashes.length; i++)
Script.fromPubkeyhash(hashes[i]); Script.fromPubkeyhash(hashes[i]);
end(i); end(i);

View File

@ -1,102 +1,116 @@
'use strict'; 'use strict';
var BN = require('bn.js');
var bcoin = require('../').set('main');
var constants = bcoin.constants;
var util = bcoin.util;
var assert = require('assert');
var scriptTypes = constants.scriptTypes;
var bench = require('./bench');
var fs = require('fs'); var fs = require('fs');
var assert = require('assert');
var Block = require('../lib/primitives/block');
var Address = require('../lib/primitives/address');
var TX = require('../lib/primitives/tx');
var MTX = require('../lib/primitives/mtx');
var Coin = require('../lib/primitives/coin');
var CoinView = require('../lib/blockchain/coinview');
var constants = require('../lib/protocol/constants');
var crypto = require('../lib/crypto/crypto');
var util = require('../lib/utils/util');
var bench = require('./bench');
bcoin.cache(); var json = require('../test/data/block300025.json');
var block = Block.fromJSON(json);
var block = bcoin.block.fromJSON(require('../test/data/block300025.json')); var btx = { tx: block.txs[397], view: new CoinView() };
var btx = block.txs[397];
var tx1 = parseTX('../test/data/tx3.hex'); var tx1 = parseTX('../test/data/tx3.hex');
var tx4 = parseTX('../test/data/tx4.hex');
var wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8'); var wtx = fs.readFileSync(__dirname + '/../test/data/wtx.hex', 'utf8');
wtx = new Buffer(wtx.trim(), 'hex'); var i, tx, raw, end, flags, input;
var tx;
function parseTX(file) { wtx = new Buffer(wtx.trim(), 'hex');
file = fs.readFileSync(__dirname + '/' + file, 'utf8').trim().split(/\n+/);
var tx = bcoin.tx.fromRaw(file.shift().trim(), 'hex'); tx = json.txs[397];
for (var i = 0; i < file.length; i++) { for (i = 0; i < tx.inputs.length; i++) {
var coin = bcoin.tx.fromRaw(file[i].trim(), 'hex'); input = tx.inputs[i];
tx.fillCoins(coin); btx.view.addCoin(Coin.fromJSON(input.coin));
}
return tx;
} }
var end = bench('parse'); function parseTX(file) {
for (var i = 0; i < 1000; i++) var data = fs.readFileSync(__dirname + '/' + file, 'utf8');
tx = bcoin.tx.fromRaw(wtx); var parts = data.trim().split(/\n+/);
var raw = parts[0];
var tx = TX.fromRaw(raw.trim(), 'hex');
var view = new CoinView();
var i, prev;
for (i = 1; i < parts.length; i++) {
raw = parts[i];
prev = TX.fromRaw(raw.trim(), 'hex');
view.addTX(prev, -1);
}
return { tx: tx, view: view };
}
end = bench('parse');
for (i = 0; i < 1000; i++)
tx = TX.fromRaw(wtx);
end(i); end(i);
var end = bench('serialize'); end = bench('serialize');
var raw; for (i = 0; i < 1000; i++) {
for (var i = 0; i < 1000; i++) {
tx._raw = null; tx._raw = null;
raw = tx.toRaw(); raw = tx.toRaw();
} }
end(i); end(i);
var end = bench('hash'); end = bench('hash');
for (var i = 0; i < 3000; i++) { for (i = 0; i < 3000; i++) {
tx1.hash(); tx1.tx.hash();
tx1._hash = null; tx1.tx._hash = null;
} }
end(i); end(i);
var end = bench('witness hash'); end = bench('witness hash');
for (var i = 0; i < 3000; i++) { for (i = 0; i < 3000; i++) {
tx.witnessHash(); tx.witnessHash();
tx._whash = null; tx._whash = null;
} }
end(i); end(i);
var end = bench('fee'); end = bench('sanity');
for (var i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
tx.getFee();
end(i);
var end = bench('sanity');
for (var i = 0; i < 1000; i++)
tx.isSane(); tx.isSane();
end(i); end(i);
var end = bench('input hashes'); end = bench('input hashes');
for (var i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
tx.getInputHashes('hex'); tx.getInputHashes(null, 'hex');
end(i); end(i);
var end = bench('output hashes'); end = bench('output hashes');
for (var i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
tx.getOutputHashes('hex'); tx.getOutputHashes('hex');
end(i); end(i);
var end = bench('all hashes'); end = bench('all hashes');
for (var i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
tx.getHashes('hex'); tx.getHashes(null, 'hex');
end(i); end(i);
var end = bench('verify'); end = bench('verify');
for (var i = 0; i < 3000; i++) for (i = 0; i < 3000; i++)
tx1.verify(constants.flags.VERIFY_P2SH); tx1.tx.verify(tx1.view, constants.flags.VERIFY_P2SH);
end(i * tx1.inputs.length); end(i * tx1.tx.inputs.length);
var flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG; end = bench('fee');
var end = bench('verify multisig'); for (i = 0; i < 1000; i++)
for (var i = 0; i < 3000; i++) tx1.tx.getFee(tx1.view);
btx.verify(flags); end(i);
end(i * btx.inputs.length);
var tx = bcoin.mtx(); flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG;
end = bench('verify multisig');
for (i = 0; i < 3000; i++)
btx.tx.verify(btx.view, flags);
end(i * btx.tx.inputs.length);
for (var i = 0; i < 100; i++) { tx = new MTX();
for (i = 0; i < 100; i++) {
tx.addInput({ tx.addInput({
prevout: { prevout: {
hash: constants.NULL_HASH, hash: constants.NULL_HASH,
@ -104,28 +118,28 @@ for (var i = 0; i < 100; i++) {
}, },
script: [ script: [
new Buffer(9), new Buffer(9),
bcoin.crypto.randomBytes(33) crypto.randomBytes(33)
] ]
}); });
tx.addOutput({ tx.addOutput({
address: bcoin.address.fromHash(bcoin.crypto.randomBytes(20)), address: Address.fromHash(crypto.randomBytes(20)),
value: 0 value: 0
}); });
} }
tx = tx.toTX(); tx = tx.toTX();
var end = bench('input hashes'); end = bench('input hashes');
for (var i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
tx.getInputHashes('hex'); tx.getInputHashes(null, 'hex');
end(i); end(i);
var end = bench('output hashes'); end = bench('output hashes');
for (var i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
tx.getOutputHashes('hex'); tx.getOutputHashes('hex');
end(i); end(i);
var end = bench('all hashes'); end = bench('all hashes');
for (var i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
tx.getHashes('hex'); tx.getHashes(null, 'hex');
end(i); end(i);

View File

@ -1,16 +1,13 @@
'use strict'; 'use strict';
var BN = require('bn.js');
var bcoin = require('../').set('main');
var constants = bcoin.constants;
var util = bcoin.util;
var assert = require('assert'); var assert = require('assert');
var scriptTypes = constants.scriptTypes; var util = require('../lib/utils/util');
var bench = require('./bench'); var bench = require('./bench');
var co = require('../lib/utils/co'); var co = require('../lib/utils/co');
var crypto = require('../lib/crypto/crypto'); var crypto = require('../lib/crypto/crypto');
var WalletDB = require('../lib/wallet/walletdb');
bcoin.cache(); var MTX = require('../lib/primitives/mtx');
var walletdb, runBench;
function dummy() { function dummy() {
var hash = crypto.randomBytes(32).toString('hex'); var hash = crypto.randomBytes(32).toString('hex');
@ -18,32 +15,18 @@ function dummy() {
prevout: { prevout: {
hash: hash, hash: hash,
index: 0 index: 0
}, }
coin: {
version: 1,
height: 0,
value: 50460 * 4,
script: new bcoin.script(),
coinbase: false,
hash: hash,
index: 0
},
script: new bcoin.script(),
witness: new bcoin.witness(),
sequence: 0xffffffff
}; };
} }
var walletdb = new bcoin.walletdb({ walletdb = new WalletDB({
name: 'wallet-test', name: 'wallet-test',
// location: __dirname + '/../walletdb-bench',
// db: 'leveldb'
db: 'memory', db: 'memory',
resolution: false, resolution: false,
verify: false verify: false
}); });
var runBench = co(function* runBench() { runBench = co(function* runBench() {
var i, j, wallet, addrs, jobs, end; var i, j, wallet, addrs, jobs, end;
var result, tx, options; var result, tx, options;
@ -81,7 +64,7 @@ var runBench = co(function* runBench() {
// TX deposit // TX deposit
jobs = []; jobs = [];
for (i = 0; i < 10000; i++) { for (i = 0; i < 10000; i++) {
tx = bcoin.mtx() tx = MTX()
.addInput(dummy()) .addInput(dummy())
.addOutput(addrs[(i + 0) % addrs.length], 50460) .addOutput(addrs[(i + 0) % addrs.length], 50460)
.addOutput(addrs[(i + 1) % addrs.length], 50460) .addOutput(addrs[(i + 1) % addrs.length], 50460)
@ -99,7 +82,7 @@ var runBench = co(function* runBench() {
// TX redemption // TX redemption
jobs = []; jobs = [];
for (i = 0; i < 10000; i++) { for (i = 0; i < 10000; i++) {
tx = bcoin.mtx() tx = MTX()
.addInput(tx, 0) .addInput(tx, 0)
.addInput(tx, 1) .addInput(tx, 1)
.addInput(tx, 2) .addInput(tx, 2)

View File

@ -920,10 +920,11 @@ TX.prototype._getInputAddresses = function getInputAddresses(view) {
TX.prototype._getOutputAddresses = function getOutputAddresses() { TX.prototype._getOutputAddresses = function getOutputAddresses() {
var table = {}; var table = {};
var addrs = []; var addrs = [];
var i, address, hash; var i, output, address, hash;
for (i = 0; i < this.outputs.length; i++) { for (i = 0; i < this.outputs.length; i++) {
address = this.outputs[i].getAddress(); output = this.outputs[i];
address = output.getAddress();
if (!address) if (!address)
continue; continue;