forked from MCJack123/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_benchmark.ts
More file actions
71 lines (53 loc) · 2.42 KB
/
memory_benchmark.ts
File metadata and controls
71 lines (53 loc) · 2.42 KB
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
import { BenchmarkKind, MemoryBenchmarkResult, ComparisonInfo, MemoryBenchmarkCategory } from "./benchmark_types";
import { compareNumericBenchmarks } from "./benchmark_util";
import { toFixed, json } from "./util";
export function runMemoryBenchmark(benchmarkFunction: () => void): MemoryBenchmarkResult {
const result: MemoryBenchmarkResult = {
kind: BenchmarkKind.Memory,
benchmarkName: "NO_NAME",
categories: {
[MemoryBenchmarkCategory.Garbage]: 0,
[MemoryBenchmarkCategory.TotalMemory]: 0,
},
};
// collect before running benchmark
collectgarbage("collect");
// stop automatic gc
collectgarbage("stop");
const preExecMemoryUsage = collectgarbage("count");
// store return value this allows benchmark functions
// to prevent "useful" result data from being garbage collected
let temp = benchmarkFunction();
const postExecMemoryUsage = collectgarbage("count");
collectgarbage("restart");
collectgarbage("collect");
// get the amount of garbage collected
result.categories[MemoryBenchmarkCategory.Garbage] = postExecMemoryUsage - collectgarbage("count");
// make sure result isn't garbage collected until now and supress unused var warning
temp = temp;
result.benchmarkName = debug.getinfo(benchmarkFunction).short_src;
result.categories[MemoryBenchmarkCategory.TotalMemory] = postExecMemoryUsage - preExecMemoryUsage;
return result;
}
const formatMemory = (memInKB: number) => toFixed(memInKB / 1024, 3);
export function compareMemoryBenchmarks(
oldResults: MemoryBenchmarkResult[],
newResults: MemoryBenchmarkResult[]
): ComparisonInfo {
// Can not use Object.values because we want a fixed order.
const categories = [MemoryBenchmarkCategory.TotalMemory, MemoryBenchmarkCategory.Garbage];
const summary = categories
.map(category => `### ${category}\n\n${compareCategory(newResults, oldResults, category)}`)
.join("\n");
const text = `### master:\n\`\`\`json\n${json.encode(oldResults)}\n\`\`\`\n### commit:\n\`\`\`json\n${json.encode(
newResults
)}\n\`\`\``;
return { summary, text };
}
function compareCategory(
newResults: MemoryBenchmarkResult[],
oldResults: MemoryBenchmarkResult[],
category: MemoryBenchmarkCategory
): string {
return compareNumericBenchmarks(newResults, oldResults, "mb", result => result.categories[category], formatMemory);
}