-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
85 lines (76 loc) · 2.2 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
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
import * as Scenarios from "./scenarios/index.js";
import Benchmark from "benchmark"; // This is technically a no-op in the web bundle, but included for cross-compatibility
var suite = new Benchmark.Suite();
/**
* getStartMemory is an isomorphic way to get the memory used, in bytes,
* either from the browser or from Node.
*/
const getStartMemory = () => {
if (typeof performance.memory !== "undefined") {
return performance.memory.usedJSHeapSize;
} else {
const used = process.memoryUsage();
return used.heapUsed;
}
};
/**
* getEndMemory is an isomorphic way to get the memory used, in bytes,
* either from the browser or from Node.
*/
const getEndMemory = () => {
if (typeof performance.memory !== "undefined") {
return performance.memory.usedJSHeapSize;
} else {
const used = process.memoryUsage();
return used.heapUsed;
}
};
/**
* trackMaxMemory maintains a map of memory usage for each scenario.
*
* It uses keys that correspond to the title of each scenario, and the
* value is the maximum memory used for that scenario.
*/
const memoryUsage = {};
const trackMaxMemory = (title, memoryUsed) => {
if (!memoryUsage[title]) {
memoryUsage[title] = memoryUsed;
} else {
memoryUsage[title] = Math.max(memoryUsage[title], memoryUsed);
}
};
suite.on("complete", function () {
const headers = [
"scenario",
"ops_per_sec",
"margin_of_error",
"runs",
"max_memory_used_kb",
];
const results = suite.filter("successful").map((benchmark) => {
return {
scenario: benchmark.name,
opsSec: benchmark.hz,
plusMinus: benchmark.stats.rme,
runs: benchmark.stats.sample.length,
maxMemory: memoryUsage[benchmark.name],
};
});
console.log(headers.join(","));
results.forEach((result) =>
console.log(
`${result.scenario},${result.opsSec},${result.plusMinus},${result.runs},${result.maxMemory}`
)
);
});
for (const scenario in Scenarios) {
const { title, run } = Scenarios[scenario];
suite.add(title, () => {
const startMemory = getStartMemory();
run();
const endMemory = getEndMemory();
const memoryUsed = endMemory - startMemory;
trackMaxMemory(title, memoryUsed);
});
}
suite.run();