Skip to content

Commit

Permalink
Merge pull request #326 from YJDoc2/add-bench-ci
Browse files Browse the repository at this point in the history
Add CI for running benchmarks
  • Loading branch information
kivikakk committed Jul 11, 2023
2 parents 91e5240 + e72e9a7 commit 216848f
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 2 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: benchmarks

on:
pull_request:
types:
- opened
- reopened
issue_comment:
types:
- created

jobs:
run_benchmarks:
runs-on: ubuntu-latest
permissions:
pull-requests: write
# run either when pull request is opened or when comment body (only on pr) is /run-bench
if: (github.event_name == 'pull_request') || ((github.event.issue.pull_request != null) && github.event.comment.body == '/run-bench')
steps:
- uses: actions/checkout@v3
with:
submodules: true
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Install hyperfine
run: cargo install hyperfine
- name: Install cmake
run: sudo apt-get update && sudo apt-get install cmake -y
- name: Build Binaries
run: make binaries
- name: Run Benchmarks
run: make bench-all
- name: Post result comment
uses: mshick/add-pr-comment@v2
with:
message-path: bench-output.md
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ target
comrak-*
.vscode
.idea
vendor/comrak
vendor/progit
benches/cmark-gfm
benches/comrak-*
benches/pulldown-cmark
benches/markdown-it
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[submodule "vendor/cmark-gfm"]
path = vendor/cmark-gfm
url = https://github.com/kivikakk/cmark-gfm.git
[submodule "vendor/pulldown-cmark"]
path = vendor/pulldown-cmark
url = https://github.com/raphlinus/pulldown-cmark.git
[submodule "vendor/markdown-it"]
path = vendor/markdown-it
url = https://github.com/rlidwka/markdown-it.rs.git
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
ROOT:=$(shell git rev-parse --show-toplevel)
COMMIT:=$(shell git rev-parse --short HEAD)
MIN_RUNS:=25

src/scanners.rs: src/scanners.re
re2rust -W -Werror -i --no-generation-date -o $@ $<
cargo fmt

bench:
cargo build --release
(cd vendor/cmark-gfm/; make bench PROG=../../target/release/comrak)

binaries: build-comrak-branch build-comrak-master build-cmark-gfm build-pulldown-cmark build-markdown-it

build-comrak-branch:
cargo build --release
cp ${ROOT}/target/release/comrak ${ROOT}/benches/comrak-${COMMIT}

build-comrak-master:
git clone https://github.com/kivikakk/comrak.git --depth 1 --single-branch ${ROOT}/vendor/comrak || true
cd ${ROOT}/vendor/comrak && \
cargo build --release && \
cp ./target/release/comrak ${ROOT}/benches/comrak-main

build-cmark-gfm:
cd ${ROOT}/vendor/cmark-gfm && \
make && \
cp build/src/cmark-gfm ${ROOT}/benches/cmark-gfm

build-markdown-it:
cd ${ROOT}/vendor/markdown-it && \
cargo build --release && \
cp target/release/markdown-it ${ROOT}/benches/markdown-it

build-pulldown-cmark:
cd ${ROOT}/vendor/pulldown-cmark && \
cargo build --release && \
cp target/release/pulldown-cmark ${ROOT}/benches/pulldown-cmark

bench-comrak: build-comrak-branch
git clone https://github.com/progit/progit.git ${ROOT}/vendor/progit || true > /dev/null
cd benches && \
hyperfine --warmup 3 --min-runs ${MIN_RUNS} -L binary comrak-${COMMIT} './bench.sh ./{binary}'

bench-all: binaries
git clone https://github.com/progit/progit.git ${ROOT}/vendor/progit || true > /dev/null
cd benches && \
hyperfine --warmup 10 --min-runs ${MIN_RUNS} -L binary comrak-${COMMIT},comrak-main,pulldown-cmark,cmark-gfm,markdown-it './bench.sh ./{binary}' --export-markdown ${ROOT}/bench-output.md &&\
echo "\n\nRun on" `date -u` >> ${ROOT}/bench-output.md

27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ assert_eq!(
</ol>\n");
```

## Benchmarking

For running benchmarks, you will need to [install hyperfine](https://github.com/sharkdp/hyperfine#installation) and optionally cmake.

If you want to just run the benchmark for `comrak`, with the current state of the repo, you can simply run
```bash
make bench-comrak
```

This will build comrak in release mode, and run benchmark on it. You will see the time measurements as reported by hyperfine in the console.

Makefile also provides a way to run benchmarks for `comrak` current state (with your changes), `comrak` main branch, [`cmark-gfm`](https://github.com/github/cmark-gfm), [`pulldown-cmark`](https://github.com/raphlinus/pulldown-cmark) and [`markdown-it.rs`](https://github.com/rlidwka/markdown-it.rs). For this you will need to install `cmake`. After that make sure that you have set-up the git submodules. In case you have not installed submodules when cloning, you can do it by running
```bash
git submodule update --init
```

After this is done, you can run
```bash
make bench-all
```

which will run benchmarks across all, and report the time take by each as well as relative time.

Apart from this, CI is also setup for running benchmarks when a pull request is first opened. It will add a comment with the results on the pull request in a tabular format comparing the 5 versions. After that you can manually trigger this CI by commenting `/run-bench` on the PR, this will update the existing comment with new results. Note benchmarks won't be automatically run on each push.



## Security

As with [`cmark`](https://github.com/commonmark/cmark) and [`cmark-gfm`](https://github.com/github/cmark-gfm#security),
Expand Down
8 changes: 8 additions & 0 deletions benches/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/bash

PROG=$1
ROOTDIR=$(git rev-parse --show-toplevel)

for lang in ar az be ca cs de en eo es es-ni fa fi fr hi hu id it ja ko mk nl no-nb pl pt-br ro ru sr th tr uk vi zh zh-tw; do \
cat $ROOTDIR/vendor/progit/$lang/*/*.markdown | $PROG > /dev/null
done
1 change: 1 addition & 0 deletions vendor/markdown-it
Submodule markdown-it added at c2919d
1 change: 1 addition & 0 deletions vendor/pulldown-cmark
Submodule pulldown-cmark added at 34c2bb

0 comments on commit 216848f

Please sign in to comment.