OILS / regtest / plot.sh View on Github | oils.pub

67 lines, 33 significant
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}
14source $LIB_OSH/bash-strict.sh
15source $LIB_OSH/task-five.sh
16
17deps() {
18 sudo apt-get install gnuplot
19}
20
21make-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
29plot() {
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
54set output '$output_file'
55
56set title "CPU Idle Time Over Time"
57set xlabel "Timestamp (Unix time)"
58set ylabel "CPU Idle Time (jiffies)"
59set grid
60
61plot '$tsv_file' using 1:2 with lines title 'CPU Idle'
62EOF
63
64 echo "Created $output_file"
65}
66
67task-five "$@"