-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.js
204 lines (188 loc) · 6.42 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict"
const IOTA = require('iota.lib.js')
const program = require('commander')
const columnify = require('columnify')
const generate = require('iota-generate-seed')
const semaphore = require('semaphore')
const { performance } = require('perf_hooks')
program
.version('1.0.0')
.option('-b, --broadcast', 'Broadcast transactions (for instance, to test Carriota Field nodes)', false)
.option('-m, --mwm [mwm]', 'Minimum Weight Magnitude', 14)
.option('-f, --format [format]', 'Format (JSON, human)', 'human')
.option('-M, --max-bundle-size [maxbundles]', 'Maximum Bundle Size', 3)
.option('-c, --concurrency [concurrency]', 'Max Concurrent Requests', 1)
.option('-d, --depth [depth]', 'Number of times to repeat each test', 10)
.option('-h, --host [host]', 'POW API Hostname', 'http://localhost')
.option('-p, --port [port]', 'POW API Port #', 14265)
.option('-H, --iri-host [host]', 'IRI API Hostname', 'http://localhost')
.option('-P, --iri-port [port]', 'IRI API Port #', 14265)
.parse(process.argv)
var testTime = {}
var tests = []
var running = 0
var address = ''
// in case they differ, we instantiate two different versions of API
const iota = new IOTA({host: program.iriHost, port: program.iriPort})
const pow = new IOTA({host: program.host, port: program.port})
const limiter = semaphore(program.concurrency)
// console.log(program)
runTests()
async function runTests(){
let seed = generate()
address = await getAddress(seed, 1)
let root = await getTransactionsToApprove(3)
testTime.startTime = performance.now()
for(var i = 1; i <= program.maxBundleSize; i++){
if(program.format == 'human') console.log('generating '+ program.depth + ' bundles of '+ i +' transactions')
for(var k = 0; k < program.depth; k++) {
let transfers = createTransfer(address, i)
running++
prepareAndAttach(seed, transfers, root)
}
}
setTimeout(checkForFinished, 1000)
}
async function checkForFinished(){
if(running > 0){
if(program.format == 'human') console.log(running + ' remaining')
setTimeout(checkForFinished, 1000)
} else {
testTime.endTime = performance.now()
runReport()
}
}
function runReport(){
if(program.format == 'human') console.log('Totally done!')
let bundleSummary = []
let summary = {
POWServer: program.host+':'+program.port,
depth: program.depth,
concurrency: program.concurrency,
maxBundleSize: program.maxBundleSize,
publicTxns: program.broadcast ? 'yes' : 'no',
estTPS: 0,
minTime: 999999999999,
maxTime: 0,
totalTxns: 0,
totalTime: 0,
testTime: testTime.endTime - testTime.startTime,
errors: 0
}
if(program.broadcast) summary.address = address
let times = []
for(var i = 0; i < tests.length; i++){
let test = tests[i]
test.totalTime = test.endTime - test.startTime
test.avgTime = test.totalTime / test.bundleSize
for(var k = 0; k < test.bundleSize; k++) {
times.push(Math.floor(test.avgTime))
}
if(test.error){
summary.errors++
} else {
summary.maxTime = Math.max(summary.maxTime, test.avgTime)
summary.minTime = Math.min(summary.minTime, test.avgTime)
summary.totalTxns += test.bundleSize
summary.totalTime += test.totalTime
}
tests[i] = test
}
times.sort((a, b) => a - b)
summary.medianTime = times[Math.floor(times.length/2)]
summary.avgTime = summary.totalTime / summary.totalTxns
summary.estTPS = summary.totalTxns/ (summary.testTime/ 1000)
switch(program.format){
case 'human':
let testColumns = columnify(tests, {
columns: ['bundleSize', 'totalTime', 'avgTime']
})
console.log(testColumns + '\n')
let summaryColumns = columnify(summary)
console.log(summaryColumns)
break
case 'JSON':
console.log(JSON.stringify({tests: tests, summary: summary}))
break
}
}
async function prepareAndAttach(seed, transfer, root) {
iota.api.prepareTransfers(seed, transfer, function(error, trytes){
limiter.take(() => {
let test = {
startTime: performance.now(),
bundleSize: transfer.length
}
pow.api.attachToTangle(root.trunk, root.branch, program.mwm, trytes, (error, success) => {
test.endTime = performance.now()
if(error){
if(program.format == 'human') console.log(error)
test.error = error
}
console.log('Success!')
if(program.broadcast && success){
let attached = iota.utils.transactionObject(success[0])
iota.api.broadcastTransactions(success, (error, success) => {
if(success){
if(program.format == 'human') console.log('POW successful and valid ' + attached.hash);
test.hash = attached.hash
} else {
test.error = error
console.log(error)
}
finalize(test)
})
} else {
finalize(test)
}
})
})
})
}
function finalize(test) {
limiter.leave()
tests.push(test) // commit test to storage
running-- //decrement running
}
/**
* Quick promise function to grab a single address by index
*/
function getAddress(seed, index) {
return new Promise((resolve, reject) => {
iota.api.getNewAddress(seed, {index : index}, (error, success)=>{
if(error){
reject(error)
} else {
resolve(success)
}
})
})
}
function getTransactionsToApprove(depth) {
return new Promise((resolve, reject) => {
iota.api.getTransactionsToApprove(depth, null, function(error, toApprove){
if(error){
reject(error)
} else {
resolve({trunk: toApprove.trunkTransaction, branch: toApprove.branchTransaction})
}
})
})
}
/**
* Create a transfer to send POW on
*/
function createTransfer(address, size) {
let transfers = []
for (var i=0; i < size; i++){
let payload = {benchmark: 'https://github.com/gagathos/iota-pow-benchmark', seq: i, rand: Math.random()}
let transfer = {
address: address,
value: 0,
message: iota.utils.toTrytes(JSON.stringify(payload)),
tag: 'BENCHMARK'
}
transfers.push(transfer)
}
return transfers
}