Skip to content

Commit 0da1b35

Browse files
authored
Added memory benchmarks to CI (#864)
* Added memory benchmarks to CI * Fixed linting issues * Fixed benchmark working directory * Load benchmarks from directory * Run benchmarks on master and commit Run benchmarks on master and commit Run benchmarks on master and commit * Changed find -d parameter to be unix compliant * Strip leading './' in readDir find command * Improved local development workflow and performed additional cleanup * Changed unit from kb to mb Some more cleanup * Addressed Review Fixed popen behaviour Improved error handling Fixed tslint issues * Fixed readFile returning incorrect result Reverted lua-types dev version back to jit * Consistent variable names * Reduced error handling boilerplate & Added some becnhmarks * Track amount of garbage collected * Typos * Addressed final review Changed error handling to use throw instead of monads Added eslint and fixed linting issues * Added missing function to readme * Removed tslint comments * Updated readme regarding non garbage results * Improved readabilty * Fixed typos
1 parent 00735b7 commit 0da1b35

File tree

19 files changed

+945
-1
lines changed

19 files changed

+945
-1
lines changed

.eslintrc.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ const typescriptBase = {
6767

6868
module.exports = {
6969
extends: ["plugin:jest/recommended", "plugin:jest/style"],
70-
parserOptions: { sourceType: "module", project: ["test/tsconfig.json", "src/lualib/tsconfig.json"] },
70+
parserOptions: {
71+
sourceType: "module",
72+
project: ["test/tsconfig.json", "src/lualib/tsconfig.json", "benchmark/tsconfig.json"],
73+
},
7174
env: { es6: true, node: true },
7275
plugins: ["import"],
7376
rules: {
@@ -186,5 +189,11 @@ module.exports = {
186189
"@typescript-eslint/prefer-optional-chain": "off",
187190
},
188191
},
192+
{
193+
files: "benchmark/src/memory_benchmarks/**/*.ts",
194+
rules: {
195+
"import/no-default-export": "off",
196+
},
197+
},
189198
],
190199
};

.github/workflows/ci.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,94 @@ jobs:
3838
CI: true
3939
- if: matrix.os == 'ubuntu-latest'
4040
uses: codecov/codecov-action@v1
41+
42+
benchmark:
43+
name: Benchmark
44+
runs-on: ubuntu-latest
45+
steps:
46+
- name: Lua Install
47+
run: sudo apt-get install lua5.3 luajit
48+
# Checkout master & commit
49+
- name: Checkout master
50+
uses: actions/checkout@v2
51+
with:
52+
ref: master
53+
path: master
54+
- name: Checkout commit
55+
uses: actions/checkout@v2
56+
with:
57+
path: commit
58+
- name: Use Node.js 12.13.1
59+
uses: actions/setup-node@v1
60+
with:
61+
node-version: 12.13.1
62+
# NPM
63+
- name: NPM master
64+
# TODO Lua types is only added manually to test the benchmark PR this can be removed again once the PR is merged
65+
run: npm ci && npm run build && npm install -D lua-types
66+
working-directory: master
67+
- name: NPM commit
68+
run: npm ci && npm run build
69+
working-directory: commit
70+
# Benchmark directory setup
71+
- name: Ensure benchmark data dir exists
72+
run: mkdir -p ./benchmark/data
73+
working-directory: commit
74+
- name: Copy commit benchmark to master
75+
run: rm -rf ./master/benchmark && cp -rf ./commit/benchmark ./master/benchmark
76+
# Run master benchmark first and output to commit benchmark data
77+
- name: Build benchmark Lua 5.3 master
78+
run: node ../dist/tstl.js -p tsconfig.53.json
79+
working-directory: master/benchmark
80+
- name: Run benchmark Lua 5.3 master
81+
id: benchmark-lua-master
82+
run: lua5.3 -- run.lua ../../../commit/benchmark/data/benchmark_master_53.json
83+
working-directory: master/benchmark/dist
84+
- name: Build benchmark LuaJIT master
85+
run: node ../dist/tstl.js -p tsconfig.jit.json
86+
working-directory: master/benchmark
87+
- name: Run benchmark LuaJIT master
88+
id: benchmark-jit-master
89+
run: luajit -- run.lua ../../../commit/benchmark/data/benchmark_master_jit.json
90+
working-directory: master/benchmark/dist
91+
# Run commit benchmark and compare with master
92+
- name: Build benchmark Lua 5.3 commit
93+
run: node ../dist/tstl.js -p tsconfig.53.json
94+
working-directory: commit/benchmark
95+
- name: Run benchmark Lua 5.3 commit
96+
id: benchmark-lua-commit
97+
run: echo ::set-output name=info::`lua5.3 -- run.lua ../data/benchmark_commit_53.json ../data/benchmark_master_53.json`
98+
working-directory: commit/benchmark/dist
99+
- name: Build benchmark LuaJIT commit
100+
run: node ../dist/tstl.js -p tsconfig.jit.json
101+
working-directory: commit/benchmark
102+
- name: Run benchmark LuaJIT commit
103+
id: benchmark-jit-commit
104+
run: echo ::set-output name=info::`luajit -- run.lua ../data/benchmark_commit_jit.json ../data/benchmark_master_jit.json`
105+
working-directory: commit/benchmark/dist
106+
- name: Create benchmark check
107+
uses: actions/github-script@0.9.0
108+
with:
109+
benchmark-info-lua: ${{steps.benchmark-lua-commit.outputs.info}}
110+
benchmark-info-jit: ${{steps.benchmark-jit-commit.outputs.info}}
111+
script: |
112+
const benchmarkInfoLua = JSON.parse(core.getInput('benchmark-info-lua', { required: true }));
113+
const benchmarkInfoJIT = JSON.parse(core.getInput('benchmark-info-jit', { required: true }));
114+
115+
const summary = `### Lua5.3\n${benchmarkInfoLua.summary}\n### LuaJIT\n${benchmarkInfoJIT.summary}`;
116+
117+
const text = `### Lua5.3\n${benchmarkInfoLua.text}\n### LuaJIT\n${benchmarkInfoJIT.text}`;
118+
119+
github.checks.create({
120+
owner: context.repo.owner,
121+
repo: context.repo.repo,
122+
name: "Benchmark results",
123+
head_sha: context.sha,
124+
status: "completed",
125+
conclusion: "neutral",
126+
output: {
127+
title: "Benchmark results",
128+
summary: summary,
129+
text: text
130+
}
131+
});

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ yarn.lock
66
.vscode
77
.idea
88
.DS_Store
9+
10+
benchmark/data/*
11+
benchmark/dist/*
12+
!benchmark/dist/json.lua

benchmark/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## TSTL Benchmarks
2+
3+
These benchmarks are written in typescript and transpiled to lua by using tstl.
4+
5+
### Currently only memory benchmarks are supported
6+
7+
To add a new benchmark add a new file to `memory_benchmarks`
8+
and **default** export a function with the following type: `() => void`.
9+
To prevent the benchmark from reporting "useful" results of your benchmark function as garbage, simply return the result.
10+
The memory used by the returned result wont count towards the total garbage amount.
11+
12+
For example (memory_benchmarks/my_benchmark.ts):
13+
14+
```ts
15+
export default function myBenchmark() {
16+
const n = 123;
17+
const result = [];
18+
for (let i = 0; i < n; i++) {
19+
// Do something memory instensive
20+
}
21+
return result; // Return results so they wont be counted as garbage
22+
}
23+
```
24+
25+
**Goal**
26+
27+
The goal of memory benchmarks is to track how much (memory) `"garbage"` is created by tstl.
28+
For that reason garabage collection is disabled in the benchmarks.
29+
30+
You can force the creation of `"garbage"` by creating a lot of anonymous functions or temporary tables (see [lua-users.org](http://lua-users.org/wiki/OptimisingGarbageCollection) for more information).
31+
32+
To avoid crashes in the CI your benchmark should not use more than 500MB of memory.
33+
34+
**Running locally**
35+
36+
1. Create a benchmark baseline called "benchmark_baseline.json":
37+
`tstl -p tsconfig.53.json && cd dist && lua -- run.lua benchmark_baseline.json`
38+
2. Make some changes to tstl.
39+
3. Create an updated benchmark and compare with the baseline:
40+
`tstl -p tsconfig.53.json && cd dist && lua -- run.lua benchmark_updated.json benchmark_baseline.json`
41+
4. The above command will output comparison data as json to stdout.
42+
If you provide a path as third argument the comparison data will be written to that path instead.
43+
`tstl -p tsconfig.53.json && cd dist && lua -- run.lua benchmark_updated.json benchmark_baseline.json result.md`

0 commit comments

Comments
 (0)