-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_save.js
72 lines (70 loc) · 1.97 KB
/
benchmark_save.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
/** @param {NS} ns */
export async function main(ns) {
ns.disableLog("run");
ns.disableLog("kill");
const orig = React.createElement;
let resolve;
// Pull the save function from the button on the character menu
React.createElement = function (...args) {
const props = args[1];
if (props && props.save && props.killScripts) {
React.createElement = orig;
resolve(props.save);
}
return orig.call(this, ...args);
};
const resultP = Promise.race([
new Promise((res) => (resolve = res)),
ns.asleep(1000),
]).finally(() => {
React.createElement = orig;
});
// Force a rerender
ns.ui.setTheme(ns.ui.getTheme());
const saveFunc = await resultP;
if (!saveFunc) {
ns.tprint("ERROR: Couldn't find save function!");
return;
}
const COPIES = 10000;
ns.write(
"benchmark_save_helper.js",
`
/** @param {NS} ns */
export function main(ns) {
ns.writePort(ns.args[0], "");
return ns.asleep(300000);
}`.trim(),
"w"
);
ns.tprintf("Launching %d copies...", COPIES);
const beforeLaunch = performance.now();
ns.atExit(() => {
const host = ns.getHostname();
for (let i = 0; i < COPIES; ++i) {
ns.kill("benchmark_save_helper.js", host, ns.pid, i);
}
});
const promise = ns.getPortHandle(ns.pid).nextWrite();
for (let i = 0; i < COPIES; ++i) {
const pid = ns.run("benchmark_save_helper.js", 1, ns.pid, i);
if (pid < 1) {
ns.tprintf("Failed to launch copy %d, benchmark failed", i);
return;
}
}
const afterLaunch = performance.now();
ns.tprintf("Launched in %.3f seconds", 0.001 * (afterLaunch - beforeLaunch));
await promise;
const afterStart = performance.now();
ns.tprintf(
"Scripts started in %.3f seconds. Saving game...",
0.001 * (afterStart - afterLaunch)
);
await ns.asleep(0);
const before = performance.now();
saveFunc();
await ns.asleep(0);
const after = performance.now();
ns.tprintf("Saving took %.3f seconds", 0.001 * (after - before));
}