-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (57 loc) · 1.89 KB
/
index.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
import fetch from "node-fetch";
import { performance } from "perf_hooks";
const rpcEndpoint = "https://your-rpc-endpoint-here.com/rpc"; // Replace with your RPC endpoint
const method = "eth_blockNumber";
const targetRPS = 100; // Adjust this value to set your desired RPS
const duration = 10; // How many seconds to run the benchmark for
class EthereumRPCBenchmark {
constructor(rpcEndpoint, targetRPS) {
this.rpcEndpoint = rpcEndpoint;
this.targetRPS = targetRPS;
this.responseTimes = [];
}
async sendRequest() {
const startTime = performance.now();
await fetch(this.rpcEndpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
method: method,
params: [],
id: 1,
}),
});
const endTime = performance.now();
this.responseTimes.push(endTime - startTime);
}
async runBenchmark(duration) {
const startTime = performance.now();
while (performance.now() - startTime < duration * 1000) {
const batchPromises = [];
for (let i = 0; i < this.targetRPS; i++) {
batchPromises.push(this.sendRequest());
}
await Promise.all(batchPromises);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
printResults() {
const avgResponseTime =
this.responseTimes.reduce((a, b) => a + b, 0) / this.responseTimes.length;
console.log(`Average response time: ${avgResponseTime.toFixed(2)} ms`);
console.log(`Total requests sent: ${this.responseTimes.length}`);
console.log(
`Actual RPS: ${(
this.responseTimes.length /
(this.responseTimes.length / this.targetRPS)
).toFixed(2)}`
);
}
}
async function main() {
const benchmark = new EthereumRPCBenchmark(rpcEndpoint, targetRPS);
await benchmark.runBenchmark(duration);
benchmark.printResults();
}
main().catch(console.error);