Add timeline visualization and fix resource profiling

- Save timeline data for each run (time, memory, CPU)
- Create timelines directory structure
- Add Python visualization script for charts
- Fix integer comparison errors in profiling
- Collect samples throughout program lifetime
This commit is contained in:
Ein Anderssono
2026-04-23 01:00:42 +02:00
parent 83d3c1ba46
commit 8723db1033
40 changed files with 296 additions and 37 deletions
+30 -37
View File
@@ -123,33 +123,38 @@ run_program() {
local result
local peak_memory=0
local peak_cpu=0
local timeline_dir="timelines/$name"
# Create timeline directory
mkdir -p "$timeline_dir"
# Run 4 times, discard first run (warmup)
for i in 1 2 3 4; do
local mem_profile_file="/tmp/${name}_mem_$$_$i"
local cpu_profile_file="/tmp/${name}_cpu_$$_$i"
local timeline_file="$timeline_dir/run_$i.tsv"
local start=$(date +%s%N)
# Run program and capture PID for resource profiling
if [ "$i" -gt 1 ]; then
# For runs 2-4, profile memory and CPU
"$@" 2>/dev/null &
local pid=$!
"$@" 2>/dev/null &
local pid=$!
# Profile resources in background
local resources=$(profile_resources "$pid" "/dev/null" "/dev/null" "$timeline_file")
local peak_mem=$(echo "$resources" | awk '{print $1}')
local peak_cpu_val=$(echo "$resources" | awk '{print $2}')
# Wait for process to complete
wait "$pid" 2>/dev/null
local exit_code=$?
local end=$(date +%s%N)
local elapsed=$(( (end - start) / 1000000 ))
if [ $exit_code -eq 0 ]; then
# Get result for verification
result=$("$@" 2>/dev/null)
# Profile resources in background
local resources=$(profile_resources "$pid" "$mem_profile_file" "$cpu_profile_file")
local peak_mem=$(echo "$resources" | awk '{print $1}')
local peak_cpu_val=$(echo "$resources" | awk '{print $2}')
# Wait for process to complete
wait "$pid" 2>/dev/null
local exit_code=$?
if [ $exit_code -eq 0 ]; then
result=$("$@" 2>/dev/null)
local end=$(date +%s%N)
local elapsed=$(( (end - start) / 1000000 ))
if [ "$i" -gt 1 ]; then
# Only count runs 2-4 for averages
total_time=$((total_time + elapsed))
total_memory=$((total_memory + peak_mem))
total_cpu=$((total_cpu + peak_cpu_val))
@@ -164,23 +169,11 @@ run_program() {
if verify "$result" "$DECIMALS"; then
((success_count++))
fi
else
echo -e "${RED}ERROR${NC}"
results+=("999999 $name ERROR")
rm -f "$mem_profile_file" "$cpu_profile_file"
return
fi
rm -f "$mem_profile_file" "$cpu_profile_file"
else
# First run (warmup) - just execute without profiling
if result=$("$@" 2>/dev/null); then
:
else
echo -e "${RED}ERROR${NC}"
results+=("999999 $name ERROR")
return
fi
echo -e "${RED}ERROR${NC}"
results+=("999999 $name ERROR")
return
fi
done
@@ -194,10 +187,10 @@ run_program() {
local peak_memory_mb=$((peak_memory / 1024))
if [ $success_count -eq 3 ]; then
echo -e "${GREEN}SUCCESS${NC} $avg_time ms, ${BLUE}${avg_memory_mb}MB mem, ${YELLOW}${avg_cpu}% CPU avg, ${peak_cpu}% CPU peak${NC}"
echo -e "${GREEN}SUCCESS${NC} $avg_time ms, ${BLUE}${avg_memory_mb}MB avg / ${peak_memory_mb}MB peak, ${YELLOW}${avg_cpu}% CPU avg / ${peak_cpu}% CPU peak${NC}"
results+=("$avg_time $name SUCCESS $avg_memory $peak_memory $avg_cpu $peak_cpu")
else
echo -e "${RED}FAILED${NC} $avg_time ms, ${BLUE}${avg_memory_mb}MB mem, ${YELLOW}${avg_cpu}% CPU avg, ${peak_cpu}% CPU peak${NC}"
echo -e "${RED}FAILED${NC} $avg_time ms, ${BLUE}${avg_memory_mb}MB avg / ${peak_memory_mb}MB peak, ${YELLOW}${avg_cpu}% CPU avg / ${peak_cpu}% CPU peak${NC}"
results+=("$avg_time $name FAILED $avg_memory $peak_memory $avg_cpu $peak_cpu")
fi
}