-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_comment.js
160 lines (146 loc) · 3.55 KB
/
generate_comment.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
import "https://deno.land/std/dotenv/load.ts";
const repo = Deno.args[0] || "denoland/deno";
const pullNumber = Deno.args[1];
const artifactID = Deno.args[2];
const benchmarkType = Deno.args[3];
const artifactName = `deno-${pullNumber}`;
const token = Deno.env.get("GITHUB_TOKEN");
const equinixToken = Deno.env.get("EQUINIX_TOKEN");
const osDir = Deno.build.os === "linux" ? "linux64" : "mac";
const hyperfineBin =
`equinix-metal-test/third_party/prebuilt/${osDir}/hyperfine`;
export function svgChart(means) {
const body =
`![](https://quickchart.io/chart?c={type:%27bar%27,data:{labels:${
JSON.stringify(Object.keys(means))
},datasets:[{label:%27Units%27,data:${
JSON.stringify(Object.values(means))
}}]}})
`;
return body;
}
export async function generateComment(body, pullNumber) {
const comment = {
body,
};
const response = await fetch(
`https://api.github.com/repos/${repo}/issues/${pullNumber}/comments`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `token ${token}`,
},
body: JSON.stringify(comment),
},
);
return response.json();
}
async function downloadArtifact() {
const redirected = await fetch(
`https://api.github.com/repos/denoland/deno/actions/artifacts/${artifactID}/zip`,
{
headers: {
"Authorization": `token ${token}`,
},
},
);
const location = redirected.url;
// curl
const result = await Deno.run({
cmd: [
"curl",
"-L",
"-H",
`Authorization: token ${token}`,
"-o",
"artifact.zip",
location,
],
});
await result.status();
result.close();
// unzip
const unzip = await Deno.run({
cmd: ["unzip", "artifact.zip"],
});
await unzip.status();
unzip.close();
// chmod
const chmod = await Deno.run({
cmd: ["chmod", "+x", "./deno"],
});
await chmod.status();
chmod.close();
}
async function getInstanceMetadata() {
const resp = await fetch(
`https://metadata.platformequinix.com/metadata`,
);
return resp.json();
}
async function terminateInstance() {
const { id } = await getInstanceMetadata();
const resp = await fetch(
`https://api.equinix.com/metal/v1/devices/${id}?force_delete=true`,
{
method: "DELETE",
headers: {
"X-Auth-Token": equinixToken,
},
},
);
return resp.text();
}
async function runHyperfine() {
const result = await Deno.run({
cmd: [
hyperfineBin,
"--warmup",
"5",
"--show-output",
"--export-json",
"hyperfine.json",
"deno run equinix-metal-test/nop.js",
`./deno run equinix-metal-test/nop.js`,
],
});
await result.status();
}
async function hyperfine() {
await runHyperfine();
const { results } = JSON.parse(await Deno.readTextFile("hyperfine.json"));
const means = { "deno-main": results[0].mean, "deno-pr": results[1].mean };
console.log(await generateComment(svgChart(means), pullNumber));
}
async function wrk() {
const result = await Deno.run({
cmd: [
"wrk",
"-t",
2,
"-d",
30,
"-c",
256,
`http://127.0.0.1:8080/`,
],
});
await result.status();
}
const benchmarkTypes = {
hyperfine,
wrk,
};
if (import.meta.main) {
try {
await downloadArtifact();
const run = benchmarkTypes[benchmarkType];
if (run) run();
else await generateComment(`benchmark type invalid: ${benchmarkType}`);
} catch (e) {
await generateComment(e.toString(), pullNumber);
} finally {
console.log(await terminateInstance());
}
}