Files
homeai/homeai-llm/scripts/benchmark.sh
Aodhan Collins 7978eaea14 Add self-deploying setup scripts for all sub-projects (P1-P8)
- Root setup.sh orchestrator with per-phase dispatch (./setup.sh p1..p8 | all | status)
- Makefile convenience targets (make infra, make llm, make status, etc.)
- scripts/common.sh: shared bash library for OS detection, Docker helpers,
  service management (launchd/systemd), package install, env management
- .env.example + .gitignore: shared config template and secret exclusions

P1 (homeai-infra): full implementation
- docker-compose.yml: Uptime Kuma, code-server, n8n
- Note: Home Assistant, Portainer, Gitea are pre-existing instances
- setup.sh: Docker install, homeai network, container health checks

P2 (homeai-llm): full implementation
- Ollama native install with CUDA/ROCm/Metal auto-detection
- launchd plist (macOS) + systemd service (Linux) for auto-start
- scripts/pull-models.sh: idempotent model puller from manifest
- scripts/benchmark.sh: tokens/sec measurement per model
- Open WebUI on port 3030 (avoids Gitea :3000 conflict)

P3-P8: working stubs with prerequisite checks and TODO sections

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-04 21:10:53 +00:00

89 lines
2.2 KiB
Bash

#!/usr/bin/env bash
# scripts/benchmark.sh — Benchmark Ollama model inference speed
#
# Measures tokens/sec for each installed model.
# Results written to benchmark-results.md
#
# Usage:
# bash scripts/benchmark.sh
# bash scripts/benchmark.sh qwen2.5:7b # benchmark one model
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(cd "${SCRIPT_DIR}/../.." && pwd)"
source "${REPO_DIR}/scripts/common.sh"
RESULTS_FILE="${SCRIPT_DIR}/../benchmark-results.md"
PROMPT="Tell me a short story about a robot who loves cooking. Keep it to exactly 200 words."
if ! command_exists ollama; then
die "Ollama not found."
fi
if ! curl -sf http://localhost:11434 -o /dev/null; then
die "Ollama is not running."
fi
benchmark_model() {
local model="$1"
log_step "Benchmarking $model..."
local start end elapsed
start=$(date +%s%3N)
local response
response=$(ollama run "$model" "$PROMPT" 2>&1) || {
log_error "Model $model failed to run."
echo "| $model | ERROR | — |"
return
}
end=$(date +%s%3N)
elapsed=$(( (end - start) ))
local word_count
word_count=$(echo "$response" | wc -w)
local tokens_est=$(( word_count * 4 / 3 )) # rough estimate: 1 token ≈ 0.75 words
local elapsed_sec
elapsed_sec=$(echo "scale=1; $elapsed / 1000" | bc)
local tps
tps=$(echo "scale=1; $tokens_est / ($elapsed / 1000)" | bc 2>/dev/null || echo "?")
printf " %-30s %6s tok/s (%ss)\n" "$model" "$tps" "$elapsed_sec"
echo "| \`$model\` | ${tps} tok/s | ${elapsed_sec}s |"
}
log_section "Ollama Benchmark"
log_info "Prompt: '$PROMPT'"
echo ""
if [[ -n "${1:-}" ]]; then
models=("$@")
else
# Get list of installed models
mapfile -t models < <(ollama list 2>/dev/null | tail -n +2 | awk '{print $1}')
fi
if [[ ${#models[@]} -eq 0 ]]; then
die "No models installed. Run: bash scripts/pull-models.sh"
fi
{
echo "# Ollama Benchmark Results"
echo "> Generated: $(date)"
echo ""
echo "| Model | Speed | Time for ~200 tok |"
echo "|---|---|---|"
} > "$RESULTS_FILE"
for model in "${models[@]}"; do
benchmark_model "$model" | tee -a "$RESULTS_FILE"
done
echo "" >> "$RESULTS_FILE"
log_success "Results written to $RESULTS_FILE"
echo ""
cat "$RESULTS_FILE"