Run a function repeatedly in order to gauge its performance. Compare the results against other functions.
Add to your bpm.ini
file the following development dependency. It is unlikely that you will rely on this library for non-development systems.
[devDependencies]
bench=*
Run bpm install
to add the library. Finally, use it in your testing scripts.
#!/usr/bin/env bash
. bpm
bpm::include bench
# Add tests here
bench::test::a-descriptive-name-here() {
# Do something
ls some-file &> /dev/null
}
# Trigger all of the bench test functions that have been loaded.
bench::auto
Automatically benchmark functions that start with bench::test::*
and writes the results to stdout.
Examples
bench::test::echo-nothing() {
echo -n ""
}
bench::auto
Returns nothing, even if there are no functions to test.
Benchmark shell functions or commands, running them repeatedly until an accurate measurement of their execution time is available.
- $1 - Name of variable to get the results. This is an array with the amount of seconds (floating point) as the first value and the operations per second (another floating point number) as the second element in the array.
- $2-@ - The command to run. This can be an alias, function, or another shell command.
Examples
# Determining which one is faster.
method1() {
if [[ -f some-file ]]; then
return 1
fi
return 0
}
method2() {
[[ ! -f some-file ]]
}
bench::test result method1
echo "Method 1: ${result[0]} seconds, ${result[1]} op/sec"
bench::test result method2
echo "Method 2: ${result[0]} seconds, ${result[1]} op/sec"
Returns nothing. Even if the function returns errors, this continues to trudge on and will ignore the return code of the function being called.
This project is placed under an MIT License.