|
| 1 | +#!/bin/bash |
| 2 | +# Fine-grained binary search for optimal smallQueueRatio |
| 3 | +set -e |
| 4 | + |
| 5 | +S3FIFO="$(dirname "$0")/../s3fifo.go" |
| 6 | +SIZE="${1:-16}" |
| 7 | +RESULTS_FILE="/tmp/small_ratio_${SIZE}k.txt" |
| 8 | + |
| 9 | +# Clear previous results |
| 10 | +> "$RESULTS_FILE" |
| 11 | + |
| 12 | +run_benchmark() { |
| 13 | + local ratio=$1 |
| 14 | + |
| 15 | + # Skip if already tested |
| 16 | + if grep -q "^$ratio " "$RESULTS_FILE" 2>/dev/null; then |
| 17 | + return 0 |
| 18 | + fi |
| 19 | + |
| 20 | + # Update smallQueueRatio in s3fifo.go |
| 21 | + sed -i '' "s/smallQueueRatio = [0-9]*/smallQueueRatio = $ratio/" "$S3FIFO" |
| 22 | + |
| 23 | + # Run benchmark - capture output |
| 24 | + output=$(SIZES=$SIZE SUITES=hitrate make benchmark 2>&1) || true |
| 25 | + |
| 26 | + # Extract the average from Category Summary line for multicache |
| 27 | + avg=$(echo "$output" | grep -E '^\s*\| multicache' | tail -1 | awk -F'|' '{print $3}' | grep -o '[0-9.]*') |
| 28 | + |
| 29 | + if [ -z "$avg" ]; then |
| 30 | + echo "ratio=$ratio: FAILED" |
| 31 | + return 1 |
| 32 | + fi |
| 33 | + |
| 34 | + echo "ratio=$ratio ($(echo "scale=1; $ratio/10" | bc)%): hitrate=$avg%" |
| 35 | + echo "$ratio $avg" >> "$RESULTS_FILE" |
| 36 | +} |
| 37 | + |
| 38 | +echo "=== Fine-tuning smallQueueRatio for ${SIZE}K cache ===" |
| 39 | +echo "" |
| 40 | + |
| 41 | +# Binary search between bounds |
| 42 | +low=100 |
| 43 | +high=160 |
| 44 | +iterations=0 |
| 45 | +max_iterations=20 |
| 46 | + |
| 47 | +while [ $((high - low)) -gt 2 ] && [ $iterations -lt $max_iterations ]; do |
| 48 | + mid=$(( (low + high) / 2 )) |
| 49 | + left=$(( (low + mid) / 2 )) |
| 50 | + right=$(( (mid + high) / 2 )) |
| 51 | + |
| 52 | + echo "--- Iteration $((iterations+1)): testing $left, $mid, $right (range: $low-$high) ---" |
| 53 | + |
| 54 | + run_benchmark $left |
| 55 | + run_benchmark $mid |
| 56 | + run_benchmark $right |
| 57 | + |
| 58 | + # Get results |
| 59 | + left_val=$(grep "^$left " "$RESULTS_FILE" | awk '{print $2}') |
| 60 | + mid_val=$(grep "^$mid " "$RESULTS_FILE" | awk '{print $2}') |
| 61 | + right_val=$(grep "^$right " "$RESULTS_FILE" | awk '{print $2}') |
| 62 | + |
| 63 | + # Find best and narrow range |
| 64 | + best_val=$left_val |
| 65 | + best_pos="left" |
| 66 | + |
| 67 | + if [ "$(echo "$mid_val > $best_val" | bc -l)" -eq 1 ]; then |
| 68 | + best_val=$mid_val |
| 69 | + best_pos="mid" |
| 70 | + fi |
| 71 | + if [ "$(echo "$right_val > $best_val" | bc -l)" -eq 1 ]; then |
| 72 | + best_val=$right_val |
| 73 | + best_pos="right" |
| 74 | + fi |
| 75 | + |
| 76 | + case $best_pos in |
| 77 | + left) high=$mid ;; |
| 78 | + mid) low=$left; high=$right ;; |
| 79 | + right) low=$mid ;; |
| 80 | + esac |
| 81 | + |
| 82 | + iterations=$((iterations + 1)) |
| 83 | +done |
| 84 | + |
| 85 | +# Final sweep of remaining range |
| 86 | +echo "" |
| 87 | +echo "--- Final sweep: $low to $high ---" |
| 88 | +for ratio in $(seq $low $high); do |
| 89 | + run_benchmark $ratio |
| 90 | +done |
| 91 | + |
| 92 | +echo "" |
| 93 | +echo "=== Results for ${SIZE}K (sorted by hitrate) ===" |
| 94 | +sort -k2 -rn "$RESULTS_FILE" | head -10 |
| 95 | + |
| 96 | +# Find the best |
| 97 | +best_line=$(sort -k2 -rn "$RESULTS_FILE" | head -1) |
| 98 | +best_ratio=$(echo "$best_line" | awk '{print $1}') |
| 99 | +best_hitrate=$(echo "$best_line" | awk '{print $2}') |
| 100 | + |
| 101 | +echo "" |
| 102 | +echo "=== OPTIMAL for ${SIZE}K ===" |
| 103 | +echo "smallQueueRatio = $best_ratio ($(echo "scale=1; $best_ratio/10" | bc)%) with hitrate=$best_hitrate%" |
| 104 | + |
| 105 | +# Restore original value |
| 106 | +sed -i '' "s/smallQueueRatio = [0-9]*/smallQueueRatio = 137/" "$S3FIFO" |
| 107 | +echo "" |
| 108 | +echo "Restored smallQueueRatio to 137" |
0 commit comments