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

151 lines, 50 significant
1#!/usr/bin/env bash
2#
3# Plot stats for regtest/aports
4#
5# This file was started 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
21readonly STAT_FILE=_tmp/aports-report/2025-11-13-main-full/proc-log/stat.txt
22
23make-cpu-tsv() {
24 local input_file=${1:-$STAT_FILE}
25
26 # Extract only lines that start with timestamp followed by "cpu " (not cpu0, cpu1, etc.)
27 # Field 5 is the idle time (0-indexed: timestamp=0, cpu=1, user=2, nice=3, system=4, idle=5)
28 awk '/^[0-9]+ cpu / { print $1 "\t" $6 }' "$input_file"
29}
30
31totals() {
32 local input_file=${1:-$STAT_FILE}
33
34 awk '
35 /^[0-9]+ cpu[ ]/ {
36 user += $3
37 sys += $5
38 idle += $6
39 }
40 END {
41 print user
42 print sys
43 print idle
44
45 print user/sys
46 # Hm, there is a lot of idle time
47 print user/idle
48 }
49 ' "$input_file"
50
51 echo
52 wc -l $input_file
53}
54
55col-diff() {
56 local input_file=${1:-$STAT_FILE}
57
58 awk '
59 /^[0-9]+ cpu[ ]/ {
60 if (NR == 1) {
61 prev_time = $1
62 prev_user = $3
63 prev_sys = $5
64 prev_idle = $6
65 } else {
66 time = $1
67 user = $3
68 sys = $5
69 idle = $6
70
71 #print time - prev_time
72 #print user - prev_user
73 #print sys - prev_sys
74 print idle - prev_idle
75
76 prev_time = time
77 prev_user = user
78 prev_sys = sys
79 prev_idle = idle
80 }
81 }
82 ' "$input_file"
83}
84
85col-diff-hist() {
86 col-diff | sort | uniq -c | sort -n
87}
88
89col-diff-plot() {
90 # Silly but convenient method of plotting!
91
92 col-diff | awk '
93 # Store values and find max
94 {
95 values[NR] = $1
96 if ($1 > max) max = $1
97 }
98
99 END {
100 # Scale and print
101 for (i = 1; i <= NR; i++) {
102 bar_length = int(values[i] / max * 50) # Scale to 50 chars
103 printf "%6d| ", values[i]
104 for (j = 0; j < bar_length; j++) {
105 printf "_"
106 }
107 printf "\n"
108 }
109 }
110 '
111}
112
113plot() {
114 local format=${1:-png}
115 local tsv_file=${2:-_tmp/cpu_idle.tsv}
116 local output_file=${3:-cpu_idle.$format}
117
118 echo "Generating $tsv_file..."
119 make-cpu-tsv > "$tsv_file"
120
121 # Create gnuplot script
122 local terminal_cmd
123 case "$format" in
124 png)
125 terminal_cmd="set terminal pngcairo size 1200,800 enhanced font 'Arial,12'"
126 ;;
127 svg)
128 terminal_cmd="set terminal svg size 1200,800 enhanced font 'Arial,12'"
129 ;;
130 *)
131 echo "Error: Unsupported format '$format'. Use 'png' or 'svg'."
132 return 1
133 ;;
134 esac
135
136 gnuplot <<EOF
137$terminal_cmd
138set output '$output_file'
139
140set title "CPU Idle Time Over Time"
141set xlabel "Timestamp (Unix time)"
142set ylabel "CPU Idle Time (jiffies)"
143set grid
144
145plot '$tsv_file' using 1:2 with lines title 'CPU Idle'
146EOF
147
148 echo "Created $output_file"
149}
150
151task-five "$@"