Time a tree of calls in a bash program.
Wrap what you want measured, giving it a label and the command to run:
build() {
BASHPROF_TIMETHIS compile cc_all
BASHPROF_TIMETHIS link ld_all
}
BASHPROF_TIMETHIS build buildbashprof run --into build.times -- bash build.bash
build 76588 µs (621 µs of its own) at [email protected]:6
compile 53139 µs (53139 µs of its own) at [email protected]:2
link 22828 µs (22828 µs of its own) at [email protected]:3
Each measurement carries the clock of the shell that sent it, the identity of the call it belongs to, and the frame it was written from. The tree is assembled from those, so calls made in a subshell nest under their parent, and a call whose shell died before it ended is reported as unended rather than given a duration. The figure in parentheses is the call's own time with its children's subtracted.
A call site makes bashprof a dependency of the script that says it. Outside a session the word is a command that does not exist — status 127, and the wrapped command does not run — which is deliberate, since a call site asked for a measurement and measuring into nowhere would be worse. A script that must also run unprofiled defines the word itself, in one line:
declare -F BASHPROF_TIMETHIS >/dev/null || BASHPROF_TIMETHIS() { shift; "$@"; }--output chooses how far the run is read: human for the tree above, tree
for the same as JSON, tree-with-err to include calls that never ended, and
raw for the messages themselves, one per line, before any of them are read
as calls.
run provisions BASH_ENV, and every non-interactive bash in the tree joins
as it starts. Under --reach by-hand the provisioned file only defines the
words, and a script joins where it says BASHPROF_INIT "$BASHPROF_SESSION";
this is the one to use when the program starts shells whose startup you do not
control.
A script can also start the profiler itself as a coprocess and keep the
reading. bashprof serve --help prints that recipe, and
__fixtures/joined/build.bash is the whole of it in one file.
Built on bash-interop. Reference:
docs/.
Licensed under the MIT licence.