Skip to content

Commit edc37d8

Browse files
committed
Load benchmarks from directory
1 parent d333822 commit edc37d8

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

benchmark/benchmark_types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export interface BenchmarkResult {
66
kind: BenchmarkKind;
77
}
88

9+
export type BenchmarkFunction = () => void;
10+
911
export interface MemoryBenchmarkResult extends BenchmarkResult {
1012
kind: BenchmarkKind.Memory;
1113
benchmarkName: string;

benchmark/run.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { runMemoryBenchmark, compareMemoryBenchmarks } from "./memory_benchmark";
2-
import { isMemoryBenchmarkResult, BenchmarkResult } from "./benchmark_types";
3-
import detectCyleBenchmark from "./memory_benchmarks/graph_cylce";
4-
import { json } from "./util";
2+
import { isMemoryBenchmarkResult, BenchmarkResult, BenchmarkFunction } from "./benchmark_types";
3+
import { json, readAll, readDir, loadBenchmarksFromDirectory } from "./util";
54

65
// CLI arguments
76
// arg[0]: path to baseline benchmark data (required because this is also the output path)
@@ -13,8 +12,7 @@ function benchmark() {
1312
// even if there was no previous one
1413

1514
// Memory tests
16-
const memoryBenchmarkInput: (() => void)[] = [detectCyleBenchmark];
17-
15+
const memoryBenchmarkInput = loadBenchmarksFromDirectory("memory_benchmarks");
1816
const memoryUpdatedResults = memoryBenchmarkInput.map(runMemoryBenchmark);
1917

2018
// run future benchmarks types here
@@ -52,19 +50,11 @@ function loadMasterBenchmarkData(): string | undefined {
5250

5351
if (masterFileOpen && masterFileOpen[0]) {
5452
const masterFile = masterFileOpen[0];
55-
let masterContent: (string | undefined)[];
56-
if (_VERSION == "Lua 5.3") {
57-
// @ts-ignore
58-
masterContent = masterFile.read("a");
59-
} else {
60-
// JIT
61-
// @ts-ignore
62-
masterContent = masterFile.read("*a");
63-
}
53+
let masterContent = readAll(masterFile);
6454
masterFile.close();
6555

66-
if (masterContent[0]) {
67-
return masterContent[0];
56+
if (masterContent) {
57+
return masterContent;
6858
}
6959
}
7060
}

benchmark/util.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { BenchmarkFunction } from "./benchmark_types";
2+
13
export function round(num: number, decimalPlaces: number = 0) {
24
return tonumber(string.format(`%.${decimalPlaces}f`, num));
35
}
@@ -6,3 +8,64 @@ export const json: {
68
decode: (this: void, str: string) => {};
79
encode: (this: void, val: any) => string;
810
} = require("json");
11+
12+
export function readAll(file: LuaFile): string | undefined {
13+
let content: (string | undefined)[];
14+
if (_VERSION == "Lua 5.3") {
15+
// @ts-ignore
16+
content = file.read("a");
17+
} else {
18+
// JIT
19+
// @ts-ignore
20+
content = file.read("*a");
21+
}
22+
23+
if (content && content[0]) {
24+
return content[0];
25+
}
26+
}
27+
28+
export function readDir(dir = ""): string[] | undefined {
29+
let isWindows = false;
30+
let [success, findHandle] = pcall(() => io.popen(`find ${dir} -type f -d 1`));
31+
32+
if (!success) {
33+
[success, findHandle] = pcall(() => io.popen(`dir /A-D /B ${dir}`));
34+
isWindows = true;
35+
}
36+
37+
if (success) {
38+
// appereantly TS can't infer this on it's own
39+
findHandle = findHandle as LuaFile;
40+
41+
const findResult = readAll(findHandle);
42+
findHandle.close();
43+
44+
if (findResult) {
45+
let files = findResult.split("\n");
46+
if (isWindows) {
47+
// on windows we need to append the directory path
48+
// on unix this is done by find automatically
49+
files = files.map(f => `${dir}/${f}`);
50+
}
51+
return findResult.split("\n").filter(p => p !== "");
52+
}
53+
}
54+
}
55+
56+
export function loadBenchmarksFromDirectory(dir = ""): BenchmarkFunction[] {
57+
// Memory tests
58+
const benchmarkPaths = readDir(dir);
59+
60+
if (!benchmarkPaths) {
61+
return [];
62+
}
63+
64+
return benchmarkPaths.map(f => {
65+
// replace slashes with dots
66+
let dotPath = string.gsub(f, "%/", ".")[0];
67+
// remove extension
68+
dotPath = string.gsub(dotPath, ".lua", "")[0];
69+
return require(dotPath).default as BenchmarkFunction;
70+
});
71+
}

0 commit comments

Comments
 (0)