#!/bin/bash # Mid Range MT Benchmark - Optimal Configuration # # Parameters discovered through performance tuning: # - threads=4: Optimal for quad-core systems # - cycles=60000: Sufficient iterations for stable results # - ws=256: Working set that fits in L3 cache (4MB) # - taskset -c 0-3: Pin to cores 0-3 for consistency # # Performance target: 95-99 M ops/sec # vs System allocator: ~1.87x faster set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" # Default parameters (optimized for cache efficiency) THREADS=${1:-4} CYCLES=${2:-60000} WORKING_SET=${3:-256} SEED=${4:-1} RUNS=${5:-5} # CPU affinity (use cores 0-3) TASKSET="taskset -c 0-3" echo "======================================" echo "Mid Range MT Benchmark (8-32KB)" echo "======================================" echo "Configuration:" echo " Threads: $THREADS" echo " Cycles: $CYCLES" echo " Working Set: $WORKING_SET" echo " Seed: $SEED" echo " Runs: $RUNS" echo " CPU Affinity: cores 0-3" echo "" echo "Working Set Analysis:" WS_SIZE=$((WORKING_SET * 16)) # Average 16KB per allocation echo " Memory: ~${WS_SIZE} KB per thread" echo " Total: ~$((WS_SIZE * THREADS / 1024)) MB" echo "" cd "$PROJECT_ROOT" # Check if benchmark exists if [ ! -f "./bench_mid_large_mt_hakx" ]; then echo "ERROR: bench_mid_large_mt_hakx not found!" echo "Building benchmark..." make bench_mid_large_mt_hakx echo "" fi # Run benchmark multiple times and collect results echo "Running benchmark $RUNS times..." echo "" RESULTS=() for i in $(seq 1 $RUNS); do echo "Run $i/$RUNS:" OUTPUT=$($TASKSET ./bench_mid_large_mt_hakx $THREADS $CYCLES $WORKING_SET $SEED 2>&1) echo "$OUTPUT" # Extract throughput MOPS=$(echo "$OUTPUT" | grep "Throughput:" | awk '{print $2}') RESULTS+=($MOPS) echo "" done # Calculate statistics echo "======================================" echo "Summary Statistics" echo "======================================" # Sort results for median calculation IFS=$'\n' SORTED=($(sort -n <<<"${RESULTS[*]}")) unset IFS # Calculate average SUM=0 for val in "${RESULTS[@]}"; do SUM=$(echo "$SUM + $val" | bc) done AVG=$(echo "scale=2; $SUM / ${#RESULTS[@]}" | bc) # Get median MID=$((${#RESULTS[@]} / 2)) if [ $((${#RESULTS[@]} % 2)) -eq 0 ]; then MEDIAN=$(echo "scale=2; (${SORTED[$MID-1]} + ${SORTED[$MID]}) / 2" | bc) else MEDIAN=${SORTED[$MID]} fi # Get min/max MIN=${SORTED[0]} MAX=${SORTED[-1]} echo "Results (M ops/sec):" for i in "${!RESULTS[@]}"; do printf " Run %d: %s\n" $((i+1)) "${RESULTS[$i]}" done echo "" echo "Statistics:" printf " Average: %.2f M ops/sec\n" $AVG printf " Median: %.2f M ops/sec\n" $MEDIAN printf " Min: %.2f M ops/sec\n" $MIN printf " Max: %.2f M ops/sec\n" $MAX printf " Range: %.2f - %.2f M\n" $MIN $MAX echo "" # Performance vs target TARGET_MIN=95 TARGET_MAX=120 if (( $(echo "$MEDIAN >= $TARGET_MIN" | bc -l) )); then PCT=$(echo "scale=1; $MEDIAN / $TARGET_MAX * 100" | bc) echo "Target Achievement: ${PCT}% of 120M target ✅" else echo "Target Achievement: Below 95M target ❌" fi echo "" echo "Benchmark completed successfully!"