-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathbenchmark.js
More file actions
104 lines (83 loc) · 2.91 KB
/
benchmark.js
File metadata and controls
104 lines (83 loc) · 2.91 KB
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
const { spawn } = require("child_process");
const { setTimeout } = require("timers/promises");
const { promisify } = require("util");
const exec = promisify(require("child_process").exec);
// Accepted percentage of performance decrease
const AcceptedDecrease = 40; // %
function generateWrkCommandForUrl(url) {
// Define the command with awk included
return `wrk -t12 -c400 -d15s --latency ${url} | grep 'Requests/sec' | awk '{print $2}'`;
}
async function run() {
console.log("Starting server with firewall...");
const withFirewallProc = spawn("node", ["app.js"], {
env: {
...process.env,
PORT: 5001,
NODE_OPTIONS: "--require @aikidosec/firewall",
AIKIDO_BLOCK: "true",
},
stdio: "inherit",
});
// Wait 2 seconds for the server to start and settle
await setTimeout(2000);
// Run the benchmarks
const withFirewallCommand = generateWrkCommandForUrl(
"http://localhost:5001/empty"
);
const resultWithFirewall = await exec(withFirewallCommand);
// Stop the server
withFirewallProc.kill();
console.log("Starting server without firewall...");
const withoutFirewallProc = spawn("node", ["app.js"], {
env: {
...process.env,
PORT: 5002,
},
stdio: "inherit",
});
// Wait 2 seconds for the server to start and settle
await setTimeout(2000);
const withoutFirewallCommand = generateWrkCommandForUrl(
"http://localhost:5002/empty"
);
const resultWithoutFirewall = await exec(withoutFirewallCommand);
// Stop the server
withoutFirewallProc.kill();
const withOpenTelemetryProc = spawn("node", ["app.js"], {
env: {
...process.env,
PORT: 5003,
NODE_OPTIONS:
"--require @opentelemetry/auto-instrumentations-node/register",
OTEL_TRACES_EXPORTER: "none",
OTEL_METRICS_EXPORTER: "none",
OTEL_LOGS_EXPORTER: "none",
},
stdio: "inherit",
});
// Wait 2 seconds for the server to start and settle
await setTimeout(2000);
const resultWithOpenTelemetry = await exec(
generateWrkCommandForUrl("http://localhost:5003/empty")
);
// Stop the server
withOpenTelemetryProc.unref();
withOpenTelemetryProc.kill();
const withFirewall = parseFloat(resultWithFirewall.stdout.trim());
const withoutFirewall = parseFloat(resultWithoutFirewall.stdout.trim());
const withOpenTelemetry = parseFloat(resultWithOpenTelemetry.stdout.trim());
console.log("--- Results ---");
console.log(`Without Zen: ${withoutFirewall} Requests/sec`);
console.log(`With Zen: ${withFirewall} Requests/sec`);
console.log(`With OpenTelemetry: ${withOpenTelemetry} Requests/sec`);
const increase = ((withoutFirewall - withFirewall) / withoutFirewall) * 100;
console.log(`Decrease with Zen for an empty route: ${increase.toFixed(2)}%`);
if (increase > AcceptedDecrease) {
console.error(
`Performance decrease is higher than the accepted ${AcceptedDecrease}%`
);
process.exit(1);
}
}
run();