-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
run.ts
87 lines (75 loc) · 2.02 KB
/
run.ts
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
import { delay } from "https://deno.land/std@0.147.0/async/mod.ts";
import { markdownTable } from "https://esm.sh/markdown-table@3.0.2";
async function oha(url?: string) {
const u = url ?? "http://localhost:9000";
const args = [
"-j",
"--no-tui",
"-z",
"10sec",
u,
];
console.log(`oha ${args.join().replaceAll(",", " ")}`);
const command = new Deno.Command("oha", {
args,
});
const { code, stdout, stderr } = await command.output();
const err = new TextDecoder().decode(stderr);
if (!err && code === 0) {
const output = new TextDecoder().decode(stdout);
return JSON.parse(output);
}
}
async function killServer() {
const l = Deno.run({
cmd: [`lsof`, `-t`, `-i:9000`],
stdout: "piped",
stderr: "piped",
stdin: "null",
});
const pid = JSON.parse(new TextDecoder().decode(await l.output()));
const c = Deno.run({ cmd: ["kill", "-9", `${pid}`] });
await c.status();
}
async function bench(server: string) {
Deno.run({
cmd: ["deno", "task", `${server}`],
});
await delay(1000);
let res;
if (server === "params") {
res = await oha("http://localhost:9000/agus?title=lead");
} else {
res = await oha();
}
await killServer();
return {
"module": server,
"requestsPerSec": <number> res.summary.requestsPerSec,
};
}
const server: string[] = [];
for await (const f of Deno.readDir("./examples")) {
const [n] = f.name.split(".");
server.push(n);
}
const res = [];
for (const file of server) {
const r = await bench(file);
res.push(r);
}
const table = res.sort((a, b) => b.requestsPerSec - a.requestsPerSec);
const max = table[0];
const t = table.map((v) => {
const relative = (v.requestsPerSec / max.requestsPerSec) * 100;
return [v.module, v.requestsPerSec.toFixed(2), relative.toFixed(0) + "%"];
});
let markdown = "";
markdown += `\n${
markdownTable([
["module", "rps", "relative"],
...t,
])
}`;
await Deno.writeTextFile("bench/result.json", JSON.stringify(table));
await Deno.writeTextFile("bench/result.md", markdown);