Skip to content

Instantly share code, notes, and snippets.

@johnsonjh
Last active September 5, 2023 04:28
Show Gist options
  • Save johnsonjh/d7791f7dbcffb0d49dd9a92770b303a1 to your computer and use it in GitHub Desktop.
Save johnsonjh/d7791f7dbcffb0d49dd9a92770b303a1 to your computer and use it in GitHub Desktop.
hy-compare
#!/usr/bin/env bash
# shellcheck disable=SC2312
declare -a on_exit_items
TR="$(command -v gtr 2> /dev/null || \
command -v tr 2> /dev/null)"
export TR
AWK="$(command -v gawk 2> /dev/null || \
command -v awk 2> /dev/null)"
export AWK
TAIL="$(command -v gtail 2> /dev/null || \
command -v tail 2> /dev/null)"
export TAIL
"$(command -v jq)" --version 2>&1 | \
grep -q 'jq' ||
{
printf '%s\n' "Error: No jq in PATH."
exit 1
}
"$(command -v hyperfine)" --version 2>&1 | \
grep -q 'hyperfine' ||
{
printf '%s\n' "Error: No hyperfine in PATH."
exit 1
}
"$(command -v ministat)" --version 2>&1 | \
grep -q 'ministat' ||
{
printf '%s\n' "Error: No ministat in PATH."
exit 1
}
"$(command -v avg)" --version 2>&1 | \
grep -q 'misc-scripts' || \
{
printf '%s\n' "Warning: no misc-scripts avg; some stats omitted."
AVG="true"
export AVG
NO_AVG=1
export NO_AVG
}
# shellcheck disable=SC2065
test "${NO_AVG:-0}" -ne 1 > /dev/null 2>&1 && \
{
AVG="$(command -v avg)"
export AVG
}
function on_exit()
{
for i in "${on_exit_items[@]}"
do
# shellcheck disable=SC2086
eval ${i}
done
}
function add_on_exit()
{
local n="${#on_exit_items[*]}"
on_exit_items[n]="${*}"
if [[ "${n}" -eq 0 ]]
then
trap on_exit EXIT
fi
}
_tmpfile=$(mktemp) || \
{
printf '%s\n' ""
exit 1
}
add_on_exit "rm -f ${_tmpfile:?} > /dev/null 2>&1"
# shellcheck disable=SC2065
test ${#} -ne 3 > /dev/null 2>&1 && \
{
printf '%s\n' "Invalid number of arguments (${#})"
exit 1
}
# shellcheck disable=SC2065
test "${1:-0}" -lt 3 > /dev/null 2>&1 && \
{
printf '%s\n' "Invalid number of iterations."
exit 1
}
printf '%s\n' ""
"$(command -v hyperfine)" \
--export-json "${_tmpfile:?}" \
--runs "${1:?}" "${2:?}" "${3:?}"
ret=${?}
# shellcheck disable=SC2065
test "${ret:-0}" -ne 0 > /dev/null 2>&1 && \
{
printf '%s\n' "Hyperfine error (${ret:?})"
exit "${ret:?}"
}
printf '%s\n' ""
add_on_exit "rm -f ./*.hysplit > /dev/null 2>&1"
"$(command -v jq)" -r '.results[] .times' < "${_tmpfile:?}" | \
${TR:-tr} -d ',][ ' | \
${AWK:-awk} -v RS= '{print > ("out" NR ".hysplit")}'
"$(command -v ministat)" \
-w "$(( "$(tput cols 2> /dev/null || printf '%s\n' 80)" - 6 ))" \
./*.hysplit | ${TAIL:-tail} -n +3
test "${NO_AVG:-0}" -ne 1 &&
{
n=0
for inputs in ./*.hysplit
do
n="$(( n + 1 ))"
printf '\n%s %d:\n' "Set" "${n:?}"
"${AVG:?}" -a -m -g -h -s -x "${inputs:?}"
done
}
printf '%s\n' ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment