UNPKG

2.07 kBJavaScriptView Raw
1'use strict';
2
3var benchmark = require('benchmark');
4var bitcore = require('..');
5var async = require('async');
6var blockData = require('./block-357238.json');
7
8var maxTime = 30;
9
10console.log('Benchmarking Script');
11console.log('---------------------------------------');
12
13async.series([
14 function(next) {
15
16 var c = 0;
17 var scripts = [];
18 var block = bitcore.Block.fromString(blockData);
19 for (var i = 0; i < block.transactions.length; i++) {
20 var tx = block.transactions[i];
21 for (var j = 0; j < tx.inputs.length; j++) {
22 var input = tx.inputs[j];
23 if (input.script) {
24 scripts.push(input.script);
25 }
26 }
27 for (var k = 0; k < tx.outputs.length; k++) {
28 var output = tx.outputs[k];
29 if (output.script) {
30 scripts.push(output.script);
31 }
32 }
33 }
34
35 function isPublicKeyOut() {
36 if (c >= scripts.length) {
37 c = 0;
38 }
39 scripts[c].isPublicKeyOut();
40 c++;
41 }
42
43 function isPublicKeyHashIn() {
44 if (c >= scripts.length) {
45 c = 0;
46 }
47 scripts[c].isPublicKeyHashIn();
48 c++;
49 }
50
51 function toAddress() {
52 if (c >= scripts.length) {
53 c = 0;
54 }
55 scripts[c].toAddress();
56 c++;
57 }
58
59 function getAddressInfo() {
60 if (c >= scripts.length) {
61 c = 0;
62 }
63 scripts[c].getAddressInfo();
64 c++;
65 }
66
67 var suite = new benchmark.Suite();
68 suite.add('isPublicKeyHashIn', isPublicKeyHashIn, {maxTime: maxTime});
69 suite.add('isPublicKeyOut', isPublicKeyOut, {maxTime: maxTime});
70 suite.add('toAddress', toAddress, {maxTime: maxTime});
71 suite.add('getAddressInfo', getAddressInfo, {maxTime: maxTime});
72 suite
73 .on('cycle', function(event) {
74 console.log(String(event.target));
75 })
76 .on('complete', function() {
77 console.log('Done');
78 console.log('----------------------------------------------------------------------');
79 next();
80 })
81 .run();
82 }
83], function(err) {
84 console.log('Finished');
85});