Added getBlock benchmarks

This commit is contained in:
Braydon Fuller 2015-07-07 20:28:48 -04:00
parent 46404e2bee
commit 78c1d3dbb0
3 changed files with 75 additions and 35 deletions

View File

@ -1,34 +1,42 @@
'use strict' 'use strict';
var assert = require('assert');
var benchmark = require('benchmark'); var benchmark = require('benchmark');
var bitcoinconsensus = require('../');
var bitcoin = require('bitcoin'); var bitcoin = require('bitcoin');
var async = require('async'); var async = require('async');
var maxTime = 20;
var maxTime = 10;
console.log('Benchmarking Bitcoind.js native interface versus Bitcoind JSON RPC interface'); console.log('Benchmarking Bitcoind.js native interface versus Bitcoind JSON RPC interface');
console.log('----------------------------------------------------------------------'); console.log('----------------------------------------------------------------------');
// The primary methods needed are:
// getInfo === works // To run the benchmarks a fully synced Bitcore Core directory is needed. The RPC comands
// getRawTransactioni === getrawtransaction "txid" ( verbose ) // can be modified to match the settings in bitcoin.conf.
// The primary methods that are needed:
// getInfo === works
// getRawTransaction === getrawtransaction "txid" ( verbose )
// sendRawTransaction === sendrawtransaction "hexstring" ( allowhighfees ) // sendRawTransaction === sendrawtransaction "hexstring" ( allowhighfees )
// getTransaction === either I need txindex turned on -or- the wallet turned on // getTransaction === either I need txindex turned on -or- the wallet turned on
// Wallet functionality isn't needed, and libbitcoind.so could be compiled with the --disable-wallet flag. // Wallet functionality isn't needed, and libbitcoind.so could be compiled with the --disable-wallet flag.
var fixtureData = {
var blockHashes = [
'00000000fa7a4acea40e5d0591d64faf48fd862fa3561d111d967fc3a6a94177',
'000000000017e9e0afc4bc55339f60ffffb9cbe883f7348a9fbc198a486d5488',
'000000000019ddb889b534c5d85fca2c91a73feef6fd775cd228dea45353bae1',
'0000000000977ac3d9f5261efc88a3c2d25af92a91350750d00ad67744fa8d03'
];
var fixtureData = {
transactions: [ transactions: [
'5523b432c1bd6c101bee704ad6c560fd09aefc483f8a4998df6741feaa74e6eb', '5523b432c1bd6c101bee704ad6c560fd09aefc483f8a4998df6741feaa74e6eb',
'ff48393e7731507c789cfa9cbfae045b10e023ce34ace699a63cdad88c8b43f8', 'ff48393e7731507c789cfa9cbfae045b10e023ce34ace699a63cdad88c8b43f8',
'5d35c5eebf704877badd0a131b0a86588041997d40dbee8ccff21ca5b7e5e333', '5d35c5eebf704877badd0a131b0a86588041997d40dbee8ccff21ca5b7e5e333',
'88842f2cf9d8659c3434f6bc0c515e22d87f33e864e504d2d7117163a572a3aa', '88842f2cf9d8659c3434f6bc0c515e22d87f33e864e504d2d7117163a572a3aa',
] ]
} };
var bitcoind = require('../')({ var bitcoind = require('../')({
directory: '~/.libbitcoind-example' directory: '~/.bitcoin',
testnet: true
}); });
bitcoind.on('error', function(err) { bitcoind.on('error', function(err) {
@ -37,42 +45,64 @@ bitcoind.on('error', function(err) {
bitcoind.on('open', function(status) { bitcoind.on('open', function(status) {
bitcoind.log('status="%s"', status); bitcoind.log('status="%s"', status);
});
bitcoind.on('ready', function() {
bitcoind.log('status="%s"', 'chaintip ready.');
var client = new bitcoin.Client({ var client = new bitcoin.Client({
host: 'localhost', host: 'localhost',
port: 18332, port: 18332,
user: 'bitpaytest', user: 'bitcoin',
pass: 'local321' pass: 'local321'
}); });
async.series([ async.series([
function(next) { function(next) {
function bitcoindJsonRpc() { var c = 0;
var hashesLength = blockHashes.length;
client.getInfo(); function bitcoindGetBlockNative(deffered) {
// var item = Math.floor((Math.random() * fixtures.length)); if (c >= hashesLength) {
// var data = fixtureData.transactions[item]; c = 0;
// }
// client.getTransaction(data, function(err, tx) { var hash = blockHashes[c];
// assert.equal(err, null); bitcoind.getBlock(hash, function(err, block) {
// }); if (err) {
throw err;
}
deffered.resolve();
});
c++;
} }
function bitcoindNative() { function bitcoindGetBlockJsonRpc(deffered) {
if (c >= hashesLength) {
bitcoind.getInfo(); c = 0;
// var item = Math.floor((Math.random() * fixtures.length)); }
// var data = fixtureData.transaction[item]; var hash = blockHashes[c];
// client.getBlock(hash, false, function(err, block) {
// bitcoind.getTransaction(data, function(err, tx) { if (err) {
// assert.equal(err, null); throw err;
// }); }
deffered.resolve();
});
c++;
} }
var suite = new benchmark.Suite(); var suite = new benchmark.Suite();
suite.add('bitcoind json rpc', bitcoindJsonRpc, { maxTime: maxTime }); suite.add('bitcoind getblock (native)', bitcoindGetBlockNative, {
suite.add('bitcoind native', bitcoindNative, { maxTime: maxTime }); defer: true,
maxTime: maxTime
});
suite.add('bitcoind getblock (json rpc)', bitcoindGetBlockJsonRpc, {
defer: true,
maxTime: maxTime
});
suite suite
.on('cycle', function(event) { .on('cycle', function(event) {

View File

@ -369,7 +369,7 @@ Bitcoin.prototype.getBlock = function(blockhash, callback) {
if (bitcoin.stopping) return []; if (bitcoin.stopping) return [];
return bitcoindjs.getBlock(blockhash, function(err, block) { return bitcoindjs.getBlock(blockhash, function(err, block) {
if (err) return callback(err); if (err) return callback(err);
return callback(null, bitcoin.block(block)); return callback(null, block);
}); });
}; };

View File

@ -347,7 +347,14 @@ async_blocks_ready(uv_work_t *req) {
data->result = std::string(""); data->result = std::string("");
while(!chainActive.Tip()) { while(!chainActive.Tip()) {
usleep(1E4); usleep(1E6);
}
CBlockIndex* tip = chainActive.Tip();
uint256 tipHash = tip->GetBlockHash();
while(mapBlockIndex.count(tipHash) == 0) {
usleep(1E6);
} }
} }
@ -876,8 +883,11 @@ async_get_block_after(uv_work_t *req) {
const CBlock& cblock = data->cblock; const CBlock& cblock = data->cblock;
CBlockIndex* cblock_index = data->cblock_index; CBlockIndex* cblock_index = data->cblock_index;
Local<Object> jsblock = NanNew<Object>(); CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
cblock_to_jsblock(cblock, cblock_index, jsblock, false); ssBlock << cblock;
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
Local<String> jsblock = NanNew<String>(strHex);
const unsigned argc = 2; const unsigned argc = 2;
Local<Value> argv[argc] = { Local<Value> argv[argc] = {