A benchmark utility for POSIX shell comparison
Usage: shellbench [options] files... -s, --shell SHELL[,SHELL...] The shell(s) to run the benchmark. [default: sh] -t, --time SECONDS Benchmark execution time. (SECONDS > 0) [default: 3] -w, --warmup SECONDS Benchmark preparation time. (SECONDS > 0) [default: 1] -c, --correct Enable correction mode to eliminate loop overhead. -e, --error Display error details. -h, --help You're looking at it.
$./shellbench -s sh,bash,ksh,mksh,posh,zsh sample/count.sh sample/output.sh------------------------------------------------------------------------------------------------name sh bash ksh mksh posh zsh------------------------------------------------------------------------------------------------count.sh: posix 1,034,369 248,929 282,537 364,627 411,116 577,090count.sh: typeset -i error 237,421 288,133 341,660 error 593,124count.sh: increment error 272,415 443,765 350,265 error 835,077output.sh: echo 279,335 121,104 375,175 179,903 201,718 59,138output.sh: printf 277,989 118,461 209,123 180 179 63,644output.sh: print error error 281,775 182,388 error 63,006------------------------------------------------------------------------------------------------* count: number of executions per second
file:sample/count.sh
#!/bin/shsetup() { i=1; }cleanup() {:; }#bench "posix"@begini=$((i+1))@end#bench "typeset -i"typeset -i i@begini=$((i+1))@end#bench "increment"typeset -i i@begin((i++))@end
file:sample/output.sh
#!/bin/sh#bench "echo"@beginecho"test"@end#bench "printf"@beginprintf"test\n"@end#bench "print"@beginprint"test"@end
Define new benchmark
Repeatedly execute between@begin
to@end
, and count the number of executions.
Invoked before each benchmark.
Invoked after each benchmark.
name | description | default |
---|
SHELLBENCH_SHELLS | The shell(s) to run the benchmark | sh |
SHELLBENCH_BENCHMARK_TIME | Benchmark execution time | 3 |
SHELLBENCH_WARMUP_TIME | Benchmark preparation time | 1 |
SHELLBENCH_NAME_WIDTH | Display width of benchmark name | 30 |
SHELLBENCH_COUNT_WIDTH | Display width of benchamrk count | 10 |
SHELLBENCH_NULLLOOP_COUNT | null loop measurement | |
ShellBench translates@begin
and@end
to loop as follows:
# From@beginecho"test"@end# Translate towhile __count=$(($__count+1));doecho"test"done
That is, during the benchmark time, not onlyecho
butwhile
,__ count=$(($__count+1))
, count the number of times it is executed.
This loop will be killed by another process after benchmark time. Therefore, after@end
is not executed.
Calculate the benchmark measurement results more strictly.This mode is suitable when the cost impact of the loop cannot be ignored.
Difference between default and modification mode
- Default mode: Measure
while
,__count=$(($__count+1))
and target code. - Correction mode: Measure only target code
Correction mode first measures the null loop and then eliminatesthe null loop measurement from the benchmark measurement.
# Null loopwhile __count=$(($__count+1));do__CORRECTION_MODE=done
And translates@begin
and@end
in correction mode as follows:
# From@beginecho"test"@end# Translate towhile __count=$(($__count+1));do__CORRECTION_MODE=echo"test"done
Compute a corrected value from the null loop execution count and the benchmark count.
Corrected count:B / ( 1.0 - ( B * ( 1.0 / A ) ) )
- A: null loop executed count
- B: benchmark count