-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (119 loc) · 3.57 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
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
const path = require("path");
const fs = require("fs");
const arg0 = process.argv[0];
const arg1 = process.argv[1];
const arg2 = process.argv[2];
const arg3 = process.argv[3];
if (!arg2 || !arg3 || arg2 === "-h" || arg2 === "--help") {
console.log(
`USAGE: ${path.basename(arg0)} ${path.basename(
arg1
)} <DAY> <PART> [<OPTIONS...>]
Supported options:
--example Run with example input only.
--input Run with input only.`
);
process.exit(1);
}
const options = new Set();
let i = 4;
while (process.argv[i] != null) {
options.add(process.argv[i]);
i++;
}
const day = parseInt(arg2, 10);
const part = parseInt(arg3, 10);
const dir = `${__dirname}/day${arg2.padStart(2, "0")}`;
function print_i32(num) {
console.log(num);
return num;
}
async function run(
inputFile,
wasmModule,
json,
isExample,
wasmSize,
iterations = 100
) {
const memory = new WebAssembly.Memory(json.memory);
const importObject = isExample
? json.example.importObject
: json.input.importObject;
importObject.env.memory = memory;
importObject.env.print_i32 = print_i32;
importObject.env.print = (start, length) => {
console.log(
new TextDecoder().decode(new Uint8Array(memory.buffer, start, length))
);
};
const wasm = await WebAssembly.instantiate(wasmModule, importObject);
new Uint8Array(memory.buffer).set(inputFile, json.dataOffset || 0);
const { solution } = wasm.exports;
const expected = isExample ? json.example.expected : json.input.expected;
const actual = solution();
let emoji = "❓";
let message = "";
if (expected != null) {
if (expected == actual) {
emoji = "✅";
} else {
emoji = "❌";
message = `(expected ${expected})`;
}
}
console.log(
`${emoji} ${isExample ? "EXAMPLE" : "INPUT "} ${actual} ${message}`
);
if (!isExample && expected == actual) {
// Start performance benchmarks
// Warm up the function
for (let i = 0; i < 10; i++) {
solution();
}
const results = [];
for (let i = 0; i < iterations; i++) {
const start = process.hrtime.bigint();
solution();
const end = process.hrtime.bigint();
results.push(Number(end - start));
}
const total = results.reduce((sum, timeNs) => sum + timeNs, 0);
const avgNs = total / iterations;
const bestNs = Math.min(...results);
const memoryUsage = json.input.memoryUsage;
console.log(`
Avg. runtime ${avgNs}ns (${avgNs / 1000}µs)
Best runtime ${bestNs}ns (${bestNs / 1000}µs)
WASM mem usage ${memoryUsage != null ? `${memoryUsage} bytes` : "Unknown"}
WASM file size ${wasmSize} bytes`);
}
}
(async () => {
let wasm, wasmModule, example, input, data, partData;
try {
wasm = fs.readFileSync(`${dir}/part${part}.wasm`);
example = options.has("--input")
? null
: fs.readFileSync(`${dir}/example.txt`);
input = options.has("--example")
? null
: fs.readFileSync(`${dir}/input.txt`);
data = await require(`${dir}/index.json`);
} catch (err) {
console.error(`Cannot find solution for day ${day} part ${part}.`);
process.exit(2);
}
wasmModule = new WebAssembly.Module(new Uint8Array(wasm));
partData = part == 1 ? data.part1 : data.part2;
const title = `Day ${day} Part ${part == 1 ? "One" : "Two"}`;
console.log(title);
console.log("=".repeat(title.length));
console.log();
if (example && partData.example) {
await run(example, wasmModule, partData, true);
}
if (input && partData.input) {
await run(input, wasmModule, partData, false, wasm.byteLength);
}
})();