Skip to content

Commit 8db43fc

Browse files
committed
Add a GitHub action for continuous benchmarking
We've put in a lot of effort into making Dex faster in the recent months and it would be a waste to have it all slip away. So, to prevent that, I've set up a simple system for continuous performance tracking. This includes three parts: 1. I created an orphan branch named `performance-data`, which we'll use as a simple CSV-based database of performance measurements. I ran a few benchmarks using the new `benchmarks/continuous.py` script to pre-populate it. 2. On the `gh-pages` branch, there's a new `performance.html` file, now accessible via [this URL](https://google-research.github.io/dex-lang/performance.html). It pulls the data from the `performance-data` branch and displays it as a series of plots showing changes in total allocation and end-to-end execution times of a few of our examples. 3. This commit checks in the file used to perform benchmarks, and adds a GitHub action that will run it every time we push to `main`. While total allocation numbers are very stable, at this point you might be worried that any time estimates we get from the free VMs that execute GitHub actions will be too noisy to provide any good signal. And if we did it naively, you wouldn't be wrong! To make the drift between different machines lower we use the techniques outlined in [this post](https://labs.quansight.org/blog/2021/08/github-actions-benchmarks/). The idea is to report _relative change_ in execution time compared to baseline. Most importantly, any time we want to evaluate a new commit, _we rebenchmark the baseline_ to adjust for the differences between machines. To minimize noise on shorter time scales, we interleave the evaluations of baseline compiler with those using newer versions.
1 parent 282e1e8 commit 8db43fc

File tree

3 files changed

+175
-7
lines changed

3 files changed

+175
-7
lines changed

.github/workflows/bench.yaml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Continuous benchmarking
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
matrix:
13+
os: [ubuntu-20.04]
14+
include:
15+
- os: ubuntu-20.04
16+
install_deps: sudo apt-get install llvm-12-tools llvm-12-dev pkg-config
17+
path_extension: /usr/lib/llvm-12/bin
18+
19+
steps:
20+
- name: Checkout the repository
21+
uses: actions/checkout@v2
22+
with:
23+
fetch-depth: 0
24+
25+
- name: Install system dependencies
26+
run: |
27+
${{ matrix.install_deps }}
28+
echo "${{ matrix.path_extension }}" >> $GITHUB_PATH
29+
30+
- name: Cache
31+
uses: actions/cache@v2
32+
with:
33+
path: |
34+
~/.stack
35+
36+
key: ${{ runner.os }}-bench-v1-${{ hashFiles('**/*.cabal', 'stack*.yaml') }}
37+
restore-keys: ${{ runner.os }}-bench-v1
38+
39+
- name: Benchmark
40+
run: python3 benchmarks/continuous.py /tmp/new-perf-data.csv /tmp/new-commits.csv ${GITHUB_SHA}
41+
42+
- name: Switch to the data branch
43+
uses: actions/checkout@v2
44+
with:
45+
ref: performance-data
46+
47+
- name: Append new data points
48+
run: |
49+
cat /tmp/new-perf-data.csv >>performance.csv
50+
cat /tmp/new-commits.csv >>commits.csv
51+
52+
- name: Commit new data points
53+
run: |
54+
git config --global user.name 'Dex CI'
55+
git config --global user.email '[email protected]'
56+
git add performance.csv commits.csv
57+
git commit -m "Add measurements for ${GITHUB_SHA}"
58+
git push

.github/workflows/docs.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ jobs:
2020
- name: Checkout the repository
2121
uses: actions/checkout@v2
2222

23-
- name: Setup Haskell Stack
24-
uses: actions/setup-haskell@v1
25-
with:
26-
enable-stack: true
27-
stack-no-global: true
28-
stack-version: 'latest'
29-
3023
- name: Install system dependencies
3124
run: |
3225
${{ matrix.install_deps }}

benchmarks/continuous.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import re
2+
import os
3+
import sys
4+
import csv
5+
import subprocess
6+
import tempfile
7+
from functools import partial
8+
from dataclasses import dataclass
9+
from pathlib import Path
10+
from typing import Union, Sequence
11+
12+
13+
BASELINE = '8dd1aa8539060a511d0f85779ae2c8019162f567'
14+
BENCH_EXAMPLES = [('kernelregression', 10), ('psd', 10), ('fluidsim', 10), ('regression', 10)]
15+
16+
17+
def run(*args, capture=False, env=None):
18+
print('> ' + ' '.join(map(str, args)))
19+
return subprocess.run(args, check=True, text=True, capture_output=capture, env=env)
20+
21+
22+
def read(*args, **kwargs):
23+
return run(*args, capture=True, **kwargs).stdout
24+
25+
26+
def read_stderr(*args, **kwargs):
27+
return run(*args, capture=True, **kwargs).stderr
28+
29+
30+
def build(commit):
31+
if os.path.exists(commit):
32+
print(f'Skipping the build of {commit}')
33+
else:
34+
run('git', 'checkout', commit)
35+
run('make', 'install', env=dict(os.environ, PREFIX=commit))
36+
dex_bin = Path.cwd() / commit / 'dex'
37+
return dex_bin
38+
39+
40+
def benchmark(baseline_bin, latest_bin):
41+
with tempfile.TemporaryDirectory() as tmp:
42+
def clean(bin, uniq):
43+
run(bin, 'clean', env={'XDG_CACHE_HOME': Path(tmp) / uniq})
44+
def bench(bin, uniq, bench_name, path):
45+
return parse_result(
46+
read_stderr(bin, 'script', path, '+RTS', '-s',
47+
env={'XDG_CACHE_HOME': Path(tmp) / uniq}))
48+
baseline_clean = partial(clean, baseline_bin, 'baseline')
49+
baseline_bench = partial(bench, baseline_bin, 'baseline')
50+
latest_clean = partial(clean, latest_bin, 'latest')
51+
latest_bench = partial(bench, latest_bin, 'latest')
52+
results = []
53+
for example, repeats in BENCH_EXAMPLES:
54+
path = Path('examples') / (example + '.dx')
55+
# warm-up the caches
56+
baseline_clean()
57+
baseline_bench(example, path)
58+
latest_clean()
59+
latest_bench(example, path)
60+
for i in range(repeats):
61+
print(f'Iteration {i}')
62+
baseline_alloc, baseline_time = baseline_bench(example, path)
63+
latest_alloc, latest_time = latest_bench(example, path)
64+
print(baseline_alloc, '->', latest_alloc)
65+
print(baseline_time, '->', latest_time)
66+
results.append(Result(example, 'alloc', latest_alloc))
67+
results.append(Result(example, 'time_rel', latest_time / baseline_time))
68+
return results
69+
70+
71+
@dataclass
72+
class Result:
73+
benchmark: str
74+
measure: str
75+
value: Union[int, float]
76+
77+
78+
ALLOC_PATTERN = re.compile(r"^\s*([0-9,]+) bytes allocated in the heap", re.M)
79+
TIME_PATTERN = re.compile(r"^\s*Total\s*time\s*([0-9.]+)s", re.M)
80+
def parse_result(output):
81+
alloc_line = ALLOC_PATTERN.search(output)
82+
if alloc_line is None:
83+
raise RuntimeError("Couldn't extract total allocations")
84+
total_alloc = int(alloc_line.group(1).replace(',', ''))
85+
time_line = TIME_PATTERN.search(output)
86+
if time_line is None:
87+
raise RuntimeError("Couldn't extract total time")
88+
total_time = float(time_line.group(1))
89+
return total_alloc, total_time
90+
91+
92+
def save(commit, results: Sequence[Result], datapath, commitpath):
93+
with open(datapath, 'a', newline='') as datafile:
94+
writer = csv.writer(datafile, delimiter=',', quotechar='"', dialect='unix')
95+
for r in results:
96+
writer.writerow((commit, r.benchmark, r.measure, r.value))
97+
with open(commitpath, 'a', newline='') as commitfile:
98+
writer = csv.writer(commitfile, delimiter=',', quotechar='"', dialect='unix')
99+
date = read('git', 'show', '-s', '--format=%ct', commit, '--').strip()
100+
writer.writerow([commit, date])
101+
102+
103+
def main(argv):
104+
if len(argv) != 3:
105+
raise ValueError("Expected three arguments!")
106+
datapath, commitpath, commit = argv
107+
print('Building baseline: {BASELINE}')
108+
baseline_bin = build(BASELINE)
109+
print(f'Building latest: {commit}')
110+
latest_bin = build(commit)
111+
results = benchmark(baseline_bin, latest_bin)
112+
save(commit, results, datapath, commitpath)
113+
print('DONE!')
114+
115+
116+
if __name__ == '__main__':
117+
main(sys.argv[1:])

0 commit comments

Comments
 (0)