| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Plot stats for regtest/aports
|
| 4 | #
|
| 5 | # This file was generated with Claude Code.
|
| 6 | #
|
| 7 | # I wanted to try out gnuplot. TODO: I think moving back to R is better! I
|
| 8 | # can't read gnuplot!
|
| 9 | #
|
| 10 | # Usage:
|
| 11 | # regtest/plot.sh <function name>
|
| 12 |
|
| 13 | : ${LIB_OSH=stdlib/osh}
|
| 14 | source $LIB_OSH/bash-strict.sh
|
| 15 | source $LIB_OSH/task-five.sh
|
| 16 |
|
| 17 | deps() {
|
| 18 | sudo apt-get install gnuplot
|
| 19 | }
|
| 20 |
|
| 21 | make-cpu-tsv() {
|
| 22 | local input_file=${1:-_tmp/aports-report/2025-11-13-main-full/proc-log/stat.txt}
|
| 23 |
|
| 24 | # Extract only lines that start with timestamp followed by "cpu " (not cpu0, cpu1, etc.)
|
| 25 | # Field 5 is the idle time (0-indexed: timestamp=0, cpu=1, user=2, nice=3, system=4, idle=5)
|
| 26 | awk '/^[0-9]+ cpu / { print $1 "\t" $5 }' "$input_file"
|
| 27 | }
|
| 28 |
|
| 29 | plot() {
|
| 30 | local format=${1:-png}
|
| 31 | local tsv_file=${2:-_tmp/cpu_idle.tsv}
|
| 32 | local output_file=${3:-cpu_idle.$format}
|
| 33 |
|
| 34 | echo "Generating $tsv_file..."
|
| 35 | make-cpu-tsv > "$tsv_file"
|
| 36 |
|
| 37 | # Create gnuplot script
|
| 38 | local terminal_cmd
|
| 39 | case "$format" in
|
| 40 | png)
|
| 41 | terminal_cmd="set terminal pngcairo size 1200,800 enhanced font 'Arial,12'"
|
| 42 | ;;
|
| 43 | svg)
|
| 44 | terminal_cmd="set terminal svg size 1200,800 enhanced font 'Arial,12'"
|
| 45 | ;;
|
| 46 | *)
|
| 47 | echo "Error: Unsupported format '$format'. Use 'png' or 'svg'."
|
| 48 | return 1
|
| 49 | ;;
|
| 50 | esac
|
| 51 |
|
| 52 | gnuplot <<EOF
|
| 53 | $terminal_cmd
|
| 54 | set output '$output_file'
|
| 55 |
|
| 56 | set title "CPU Idle Time Over Time"
|
| 57 | set xlabel "Timestamp (Unix time)"
|
| 58 | set ylabel "CPU Idle Time (jiffies)"
|
| 59 | set grid
|
| 60 |
|
| 61 | plot '$tsv_file' using 1:2 with lines title 'CPU Idle'
|
| 62 | EOF
|
| 63 |
|
| 64 | echo "Created $output_file"
|
| 65 | }
|
| 66 |
|
| 67 | task-five "$@"
|