This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench.js
197 lines (169 loc) · 4.79 KB
/
bench.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
const autocannon = require('@homura/autocannon');
const ora = require('ora');
const Table = require('cli-table3');
const logger = require('./logger');
const { utils } = require('@mutadev/muta-sdk');
const randomBytes = require('randombytes');
function round(x) {
return parseFloat(Math.round(x * 100) / 100).toFixed(2);
}
const query = `mutation ( $inputRaw: InputRawTransaction! $inputEncryption: InputTransactionEncryption! ) { sendTransaction(inputRaw: $inputRaw, inputEncryption: $inputEncryption) }`;
async function runMain(assetBenchProducer, workers, options) {
let errorCount = 0;
function getBody() {
return assetBenchProducer.produceRequestBody();
}
await assetBenchProducer.start();
return new Promise((resolve, reject) => {
const instance = autocannon(
{
...options,
setupClient(client) {
client.setBody(getBody());
},
},
finishedBench
);
autocannon.track(instance);
instance.on('response', function (
client,
statusCode,
returnBytes,
responseTime
) {
const res = client.parser.chunk.toString();
const isError = res.includes('error');
if (isError) {
logger.error(res);
errorCount++;
}
client.setBody(getBody());
});
instance.on('done', async function ({ start, duration }) {
for (const worker of workers) {
worker.process.kill();
}
const spin = ora('TPS is calculating ').start();
const {
blockUsage,
transferProcessed,
blocks,
} = await assetBenchProducer.end();
spin.stop();
const txCount = transferProcessed;
const blockCount = blockUsage;
const balanceTable = new Table({ head: ['', 'balance', 'block height'] });
balanceTable.push(
{
init: [
assetBenchProducer.startBalance,
assetBenchProducer.startBlock,
],
},
{ done: [assetBenchProducer.endBalance, assetBenchProducer.endBlock] }
);
console.log('block_id \t\t count \t\t\t round');
Object.entries(blocks)
.sort((l, r) => Number(l[0]) - Number(r[0]))
.forEach(([id, info]) => {
console.log(
`${id} \t\t\t ${info.transactionsCount} \t\t\t ${info.round}`
);
});
console.log('TPS:');
console.log(balanceTable.toString());
console.log(`${round(txCount / blockCount)} tx/block`);
console.log(`${round(duration / blockCount)} sec/block`);
console.log(`${round(txCount / duration)} tx/sec`);
if (options.json) {
const benchDetail = {
blocks: [],
tx_block: round(txCount / blockCount),
sec_block: round(duration / blockCount),
tx_sec: round(txCount / duration),
};
Object.entries(blocks)
.sort((l, r) => Number(l[0]) - Number(r[0]))
.forEach(([id, info]) => {
benchDetail.blocks.push([
parseInt(id),
info.transactionsCount,
info.round,
]);
});
console.log(JSON.stringify(benchDetail));
}
});
function finishedBench(err) {
if (err) reject(err);
else resolve({ errorCount });
}
});
}
function runWorker() {
let errorCount = 0;
const workerData = JSON.parse(process.env.WORKER_DATA);
const options = JSON.parse(process.env.OPTIONS);
const payload = JSON.stringify({
asset_id: workerData.assetId,
to: workerData.to,
value: 1,
});
function getBody() {
const signed = utils.signTransaction(
{
serviceName: 'asset',
method: 'transfer',
payload,
timeout: workerData.timeout,
nonce: `0x${randomBytes(32).toString('hex')}`,
chainId: `${workerData.chainId}`,
cyclesPrice: '0x01',
cyclesLimit: '0x5208',
sender: workerData.sender,
},
Buffer.from(workerData.privateKey, 'hex')
);
const variables = {
inputRaw: utils.separateOutRawTransaction(signed),
inputEncryption: utils.separateOutEncryption(signed),
};
const tx = JSON.stringify({
query,
variables,
});
return tx;
}
const instance = autocannon(
{
...options,
setupClient(client) {
client.setBody(getBody());
},
},
finishedBench
);
autocannon.track(instance);
instance.on('response', function (
client,
statusCode,
returnBytes,
responseTime
) {
const res = client.parser.chunk.toString();
const isError = res.includes('error');
if (isError) {
logger.error(res);
errorCount++;
}
client.setBody(getBody());
});
function finishedBench(err) {
if (err) {
process.exit(1);
}
process.exit(0);
}
}
exports.runWorker = runWorker;
exports.runMain = runMain;