Skip to content

Commit 267ba40

Browse files
authored
feat: add benchmarks (#23)
*feat : benchmarks for packing with hermit.com. *feat : benchmarks for binaries produced by hermit.com.
1 parent 19ab242 commit 267ba40

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ or
120120

121121
`echo aeiou | ./build/count_vowels.hermit.com`
122122

123+
## Benchmarks
124+
125+
- Hermit-cli : `./benchmarks/bench-cli.sh` benchmark hermit cli, for more details check [docs](benchmarks/README.md).
126+
- Cli binaries produced by Hermit-cli : `./benchmarks/bench-artifacts.sh` benchmark produced binaries, for more details check [docs](benchmarks/README.md).
127+
123128
## Community
124129

125130
Hermit shares the [Extism Discord](https://discord.gg/cx3usBCWnc). Join `#hermit` to discuss working with or building Hermit.

benchmarks/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.json
2+
*.com
3+
benchmarks/bench-artifacts/*
4+
benchmarks/bench-cli/*

benchmarks/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## Benchmarks
2+
3+
The purpose of this tool is to benchmark the performance of the `hermit` CLI.This will let us track performance changes from one release to another.
4+
5+
Example of current benchmarks with a VM(4xCPU : Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz, RAM 6GB) :
6+
7+
- Debug mode
8+
```log
9+
Benching with cat.
10+
Benchmark 1: hermit.com with cat
11+
Time (mean ± σ): 1737.8 ms ± 103.2 ms [User: 1725.2 ms, System: 12.1 ms]
12+
Range (min … max): 1595.7 ms … 1915.7 ms 10 runs
13+
14+
Benching with count_vowels.
15+
Benchmark 1: hermit.com with count_vowels
16+
Time (mean ± σ): 50272.0 ms ± 2673.0 ms [User: 50248.9 ms, System: 19.0 ms]
17+
Range (min … max): 47441.7 ms … 54378.3 ms 10 runs
18+
19+
Benching with cowsay.
20+
Benchmark 1: hermit.com with cowsay
21+
Time (mean ± σ): 30065.3 ms ± 1294.7 ms [User: 30047.6 ms, System: 15.0 ms]
22+
Range (min … max): 28296.4 ms … 32019.9 ms 10 runs
23+
```
24+
25+
- Release mode
26+
```log
27+
Benching with cat.
28+
Benchmark 1: hermit.com with cat
29+
Time (mean ± σ): 46.0 ms ± 4.6 ms [User: 44.2 ms, System: 1.8 ms]
30+
Range (min … max): 40.0 ms … 60.3 ms 67 runs
31+
32+
Benching with count_vowels.
33+
Benchmark 1: hermit.com with count_vowels
34+
Time (mean ± σ): 795.7 ms ± 33.1 ms [User: 789.6 ms, System: 6.0 ms]
35+
Range (min … max): 734.3 ms … 841.0 ms 10 runs
36+
37+
Benching with cowsay.
38+
Benchmark 1: hermit.com with cowsay
39+
Time (mean ± σ): 472.0 ms ± 18.6 ms [User: 471.0 ms, System: 1.0 ms]
40+
Range (min … max): 447.8 ms … 508.2 ms 10 runs
41+
```
42+
43+
## Requirements
44+
45+
- Install [Hyperfine](https://github.com/sharkdp/hyperfine) : `cargo install --locked hyperfine`
46+
47+
More info can be found in [installation docs](https://github.com/sharkdp/hyperfine#installation).
48+
49+
- Build Hermit, preferably in release mode.
50+
51+
## Usage
52+
53+
### Hermit-cli
54+
55+
Run `./benchmarks/bench-cli.sh`, this will run benchmarks using hermit-cli with available samples.
56+
57+
Default samples used during the bench :
58+
- [cat](/src/cat/)
59+
- [count_vowels](/src/count_vowels/)
60+
- [cowsay](/src/cowsay/)
61+
62+
You can provide your own samples to benchmark against it :
63+
- Create a folder under `benchmarks/bench-cli/custom/` with the '$name' of your sample (ex.: benchmarks/custom/my_cli)
64+
- Create a HermitFile under `benchmarks/bench-cli/custom/$name/` (ex.: benchmarks/custom/my_cli/HermitFile)
65+
- Run `./benchmarks/bench-cli.sh --only-custom`
66+
67+
### Cli binaires produced by Hermit-cli
68+
69+
Run `./benchmarks/bench-artifacts.sh`, this will run benchmarks using cli binaries produced by hermit-cli.
70+
71+
Default samples used during the bench :
72+
- [cat](/src/cat/)
73+
- [count_vowels](/src/count_vowels/)
74+
- [cowsay](/src/cowsay/)
75+
76+
You can benchmark your own samples, follow instructions provided by: `./benchmarks/bench-artifacts.sh --only-custom`

benchmarks/bench-artifacts.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
#set -x
3+
4+
only_custom=false
5+
remove_artifacts_after_bench=false
6+
script_folder_name=$(basename "$(dirname "$(readlink -f "$0")")")/bench-artifacts
7+
mkdir -p $script_folder_name
8+
#uncomment line below if you wan't to remove
9+
#stored bench stats from previous runs
10+
#rm -rf $script_folder_name/*.json
11+
12+
# Iterate through the command-line arguments
13+
for arg in "$@"; do
14+
if [ "$arg" == "--only-custom" ]; then
15+
only_custom=true
16+
break
17+
fi
18+
done
19+
20+
run_hyperfine(){
21+
#https://github.com/sharkdp/hyperfine/issues/94#issuecomment-432708897
22+
#samples are small, silence the warning msg.
23+
export_file="benchmarks/bench-artifacts/benchmark_$1_$(date +%s%3N).json"
24+
hyperfine \
25+
--export-json="$export_file" \
26+
-N \
27+
--min-runs 10 \
28+
--warmup=3 \
29+
--time-unit=millisecond \
30+
--command-name="bench of $1" "$2" 2> /dev/null
31+
}
32+
33+
# Check if --only-custom was passed
34+
if [ "$only_custom" == false ]; then
35+
# Benchmark build/cat.hermit.com, build/count_vowels.hermit.com and build/cowsay.hermit.com examples.
36+
run_hyperfine "Cat" "build/cat.hermit.com src/cat/cat.c"
37+
run_hyperfine "Count_vowels" "echo eeeUIaoopaskjdfhiiioozzmmmwze | build/count_vowels.hermit.com"
38+
run_hyperfine "Cowsay" "build/cowsay.hermit.com Hermooooooooot"
39+
fi
40+
41+
if [ "$only_custom" == true ]; then
42+
cat <<EOF
43+
# to benchmark using custom artifact
44+
# place your cli binary in "./benchmarks/bench-artifacts/custom/"
45+
# set values of "example_name" and "cmd"
46+
# run commands below
47+
export example_name="my_cli"
48+
export cmd="path/to/my_cli.com params of my_cli.com"
49+
export stats_file="benchmarks/bench-artifacts/benchmark_\$example_name_\$(date +%s%3N).json"
50+
hyperfine \\
51+
--export-json="\$stats_file" \\
52+
--warmup=3 \\
53+
--time-unit=millisecond \\
54+
--command-name="bench of \$example_name" "\$cmd"
55+
EOF
56+
fi

benchmarks/bench-cli.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
#set -x
3+
4+
only_custom=false
5+
remove_artifacts_after_bench=false
6+
script_folder_name=$(basename "$(dirname "$(readlink -f "$0")")")/bench-cli
7+
mkdir -p $script_folder_name
8+
rm -rf $script_folder_name/*.com
9+
#uncomment line below if you wan't to remove
10+
#stored bench stats from previous runs
11+
#rm -rf $script_folder_name/*.json
12+
13+
# Iterate through the command-line arguments
14+
for arg in "$@"; do
15+
if [ "$arg" == "--only-custom" ]; then
16+
only_custom=true
17+
break
18+
fi
19+
done
20+
21+
# Check if --only-custom was passed
22+
if [ "$only_custom" == false ]; then
23+
# Benchmark build/hermit.com using `cat`, `count_vowels` and `cowsay` examples.
24+
examples_paths=("src/cat" "src/count_vowels" "src/cowsay")
25+
for example_path in "${examples_paths[@]}"; do
26+
example_name=$(basename "$example_path")
27+
printf "Benching with ${example_name} ${example_path}.\n"
28+
29+
export_file="${script_folder_name}/benchmark_${example_name}_$(date +%s%3N).json"
30+
hyperfine \
31+
--export-json="$export_file" \
32+
--warmup=3 \
33+
--time-unit=millisecond \
34+
--command-name="hermit.com with ${example_name}" "build/hermit.com -f ${example_path}/Hermitfile -o ${script_folder_name}/${example_name}.com" \
35+
--cleanup "(${remove_artifacts_after_bench} && rm -rf ${script_folder_name}/${example_name}.com) || true"
36+
done
37+
38+
fi
39+
40+
# Benchmark build/hermit.com using custom examples.
41+
if [ "$only_custom" == true ]; then
42+
custom_examples_paths=($(ls -d $script_folder_name/custom/* 2>/dev/null))
43+
for example_path in "${custom_examples_paths[@]}"; do
44+
example_name=$(basename "$example_path")
45+
printf "Benching with ${example_name}.\n"
46+
47+
export_file="${example_path}/benchmark_${example_name}_$(date +%s%3N).json"
48+
hyperfine \
49+
--export-json="$export_file" \
50+
--warmup=3 \
51+
--time-unit=millisecond \
52+
--command-name="hermit.com with ${example_name}" "build/hermit.com -f ${example_path}/Hermitfile -o ${example_path}/${example_name}.com" \
53+
--cleanup "(${remove_artifacts_after_bench} && rm -rf ${example_path}/${example_name}.com) || true"
54+
done
55+
fi

0 commit comments

Comments
 (0)