-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.js
59 lines (54 loc) · 1.88 KB
/
runner.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
const fs = require("fs");
const { performance } = require("perf_hooks");
const { exec, spawn } = require("child_process");
// TODO: Perhaps we could trigger a transpile process from here
const [name, src = "js"] = process.argv.slice(2);
const testPath = `tests/${name}.in`;
const target = `${src}/${name}`;
const expectedScriptName = `${name}.js`;
// given the file name
if (name) {
fs.readFile(testPath, "utf-8", (err, tests) => {
if (err) return console.log("Test file not found", err);
// check if build/name is a thing
fs.readdir(`${src}/`, (err, files) => {
if (err) return console.log("Did you transpile?", err);
// check if the build has the expected script name
if (files.includes(expectedScriptName)) {
// setup spawn
const startTime = performance.now();
const child = spawn("node", [target]);
// prepare the encoding and pipe
child.stdin.setEncoding("utf-8");
child.stdout.pipe(process.stdout);
// write the actual data
child.stdin.write(tests);
// measure memory
memUsage(child);
// end process
child.stdin.end();
child.on("exit", code => {
const time = (performance.now() - startTime) / 1000;
console.log("Run Time:", time, "s");
console.log("Exit Code:", code);
});
} else {
// Exit, there was a problem with the script name
console.log(`Make sure the file script exists as ${name}.js`);
}
});
});
} else {
// Exit, no name was given
console.log("A name is required.");
}
function memUsage(child) {
exec("ps -p" + child.pid + " -o vsize=", function(err, stdout, stderr) {
const fail = err || stderr;
if (fail) {
return console.log("Failure while measuring memory");
}
const memory = parseInt(stdout, 10);
return console.log("Memory:", memory, "kilobytes");
});
}