main
fine
1#!/bin/sh
2# its like hyperfine but not written in rust and also worse but it has everything i need so
3#
4# usage:
5# fine 'command'
6# fine -t 20 'command'
7
8set -eu
9
10times=10
11
12usage() {
13 printf 'usage: %s [-t times] command\n' "${0##*/}" >&2
14 exit 2
15}
16
17while getopts 't:' opt; do
18 case $opt in
19 t)
20 times=$OPTARG
21 ;;
22 *)
23 usage
24 ;;
25 esac
26done
27shift $((OPTIND - 1))
28
29[ "$#" -eq 1 ] || usage
30
31case $times in
32 ''|*[!0-9]*)
33 usage
34 ;;
35esac
36
37[ "$times" -gt 0 ] 2>/dev/null || usage
38
39cmd=$1
40
41now() {
42 date +%s.%N
43}
44
45i=1
46while [ "$i" -le "$times" ]; do
47 printf 'run %d/%d\n' "$i" "$times" >&2
48 start=$(now)
49 sh -c "$cmd" >/dev/null 2>&1
50 end=$(now)
51 awk -v start="$start" -v end="$end" 'BEGIN { printf "%.6f\n", end - start }'
52 i=$((i + 1))
53done | awk '
54{
55 sum += $1
56 n++
57}
58{
59 printf "run %d: %.6fs\n", n, $1
60}
61END {
62 if (n == 0)
63 exit 1
64 printf "avg: %.6fs\n", sum / n
65}
66'