FoolDev Claude Opus 4.7 commited on
Commit
32d9533
·
1 Parent(s): d344201

bench.sh: simplify per code-review findings

Browse files

- Drop NPROMPTS=5 from the usage docstring — the variable was never
read; the example was a lie.
- Extract the duplicated 'jq tok/s math' expression into a tok_per_s()
helper. Two callers, identical formula — one place to fix if the
rounding ever needs to change.
- Route red() error output to stderr instead of stdout. Callers parsing
the table off stdout shouldn't see error noise mixed in.
- Cache the /api/tags response into TAGS once and reuse for both the
reachability check and the model-present jq filter, instead of
hitting the endpoint twice back-to-back.

Behavior unchanged on the happy path; failure messages now redirect
to stderr (verified with MODEL=nonexistent-model).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

Files changed (1) hide show
  1. scripts/bench.sh +12 -6
scripts/bench.sh CHANGED
@@ -8,7 +8,7 @@
8
  #
9
  # Usage:
10
  # ./scripts/bench.sh # uses MODEL=janus-27b
11
- # MODEL=janus-27b NPROMPTS=5 ./scripts/bench.sh
12
  # HOST=http://localhost:11434 ./scripts/bench.sh
13
  #
14
  # Requires: curl, jq, a running Ollama daemon with the model created.
@@ -17,21 +17,27 @@ set -euo pipefail
17
  MODEL="${MODEL:-janus-27b}"
18
  HOST="${HOST:-http://localhost:11434}"
19
 
20
- red() { printf "\033[31m%s\033[0m\n" "$*"; }
21
  green() { printf "\033[32m%s\033[0m\n" "$*"; }
22
  blue() { printf "\033[34m%s\033[0m\n" "$*"; }
23
 
 
 
 
 
 
24
  for dep in curl jq; do
25
  if ! command -v "$dep" >/dev/null 2>&1; then
26
  red "[!] missing dependency: $dep"; exit 1
27
  fi
28
  done
29
 
30
- if ! curl -fsS "${HOST}/api/tags" >/dev/null; then
 
31
  red "[!] Ollama not reachable at ${HOST}"
32
  exit 1
33
  fi
34
- if ! curl -fsS "${HOST}/api/tags" | jq -e --arg m "${MODEL}" '.models[] | select(.name | startswith($m))' >/dev/null; then
35
  red "[!] Model '${MODEL}' not found. Build it first: ./scripts/build.sh"
36
  exit 1
37
  fi
@@ -86,7 +92,7 @@ for i in "${!PROMPTS[@]}"; do
86
  fi
87
 
88
  eval_ms=$(( eval_ns / 1000000 ))
89
- toks_per_s="$(jq -n --argjson c "$eval_count" --argjson n "$eval_ns" '($c / ($n / 1000000000)) | . * 100 | floor / 100')"
90
  printf "%-4s %8s %12s %8s\n" "$((i+1))" "$eval_count" "$eval_ms" "$toks_per_s"
91
 
92
  TOTAL_TOKENS=$(( TOTAL_TOKENS + eval_count ))
@@ -94,5 +100,5 @@ for i in "${!PROMPTS[@]}"; do
94
  done
95
 
96
  echo
97
- avg="$(jq -n --argjson c "$TOTAL_TOKENS" --argjson n "$TOTAL_NS" '($c / ($n / 1000000000)) | . * 100 | floor / 100')"
98
  green "[+] aggregate: ${TOTAL_TOKENS} tokens / $(( TOTAL_NS / 1000000 )) ms = ${avg} tok/s"
 
8
  #
9
  # Usage:
10
  # ./scripts/bench.sh # uses MODEL=janus-27b
11
+ # MODEL=janus-27b ./scripts/bench.sh
12
  # HOST=http://localhost:11434 ./scripts/bench.sh
13
  #
14
  # Requires: curl, jq, a running Ollama daemon with the model created.
 
17
  MODEL="${MODEL:-janus-27b}"
18
  HOST="${HOST:-http://localhost:11434}"
19
 
20
+ red() { printf "\033[31m%s\033[0m\n" "$*" >&2; }
21
  green() { printf "\033[32m%s\033[0m\n" "$*"; }
22
  blue() { printf "\033[34m%s\033[0m\n" "$*"; }
23
 
24
+ # tok_per_s <eval_count> <eval_duration_ns> -> "X.YZ" (2 dp, floor).
25
+ tok_per_s() {
26
+ jq -n --argjson c "$1" --argjson n "$2" '($c / ($n / 1e9)) | . * 100 | floor / 100'
27
+ }
28
+
29
  for dep in curl jq; do
30
  if ! command -v "$dep" >/dev/null 2>&1; then
31
  red "[!] missing dependency: $dep"; exit 1
32
  fi
33
  done
34
 
35
+ # Single /api/tags fetch covers both checks below.
36
+ if ! TAGS="$(curl -fsS "${HOST}/api/tags")"; then
37
  red "[!] Ollama not reachable at ${HOST}"
38
  exit 1
39
  fi
40
+ if ! jq -e --arg m "${MODEL}" '.models[] | select(.name | startswith($m))' >/dev/null <<<"${TAGS}"; then
41
  red "[!] Model '${MODEL}' not found. Build it first: ./scripts/build.sh"
42
  exit 1
43
  fi
 
92
  fi
93
 
94
  eval_ms=$(( eval_ns / 1000000 ))
95
+ toks_per_s="$(tok_per_s "$eval_count" "$eval_ns")"
96
  printf "%-4s %8s %12s %8s\n" "$((i+1))" "$eval_count" "$eval_ms" "$toks_per_s"
97
 
98
  TOTAL_TOKENS=$(( TOTAL_TOKENS + eval_count ))
 
100
  done
101
 
102
  echo
103
+ avg="$(tok_per_s "$TOTAL_TOKENS" "$TOTAL_NS")"
104
  green "[+] aggregate: ${TOTAL_TOKENS} tokens / $(( TOTAL_NS / 1000000 )) ms = ${avg} tok/s"