|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the Apache 2.0 License. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
| 8 | +ROOT_DIR=$( dirname "$SCRIPT_DIR" ) |
| 9 | + |
| 10 | +usage() { |
| 11 | + cat <<'EOF' |
| 12 | +Usage: scripts/bench-ab.sh [run|report|local] [options] |
| 13 | +
|
| 14 | +No command means local: restore main perf, run benchmarks, and write a report. |
| 15 | +
|
| 16 | +Options: |
| 17 | + --build-dir DIR Build directory. Default: build-bench-ab |
| 18 | + --results-dir DIR Benchmark JSON directory. Default: build/bench-ab |
| 19 | + --main-perf-dir DIR Main-branch perf JSON directory. Default: build/bench-ab/main |
| 20 | + --output FILE Markdown report path. Default: RESULTS_DIR/report.md |
| 21 | + --iterations N Benchmark repetitions. Default: 3 |
| 22 | + --label LABEL Report label. Default: run |
| 23 | + --summary Append report to GITHUB_STEP_SUMMARY |
| 24 | +EOF |
| 25 | +} |
| 26 | + |
| 27 | +abs_path() { |
| 28 | + if [[ "$1" == /* ]]; then |
| 29 | + echo "$1" |
| 30 | + else |
| 31 | + echo "$ROOT_DIR/$1" |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +cmd=local |
| 36 | +case "${1:-}" in |
| 37 | + -h|--help) |
| 38 | + usage |
| 39 | + exit 0 |
| 40 | + ;; |
| 41 | + run|report|local) |
| 42 | + cmd=$1 |
| 43 | + shift |
| 44 | + ;; |
| 45 | +esac |
| 46 | + |
| 47 | +build_dir=build-bench-ab |
| 48 | +results_dir=build/bench-ab |
| 49 | +main_perf_dir=build/bench-ab/main |
| 50 | +output_file= |
| 51 | +iterations=3 |
| 52 | +label=run |
| 53 | +summary=false |
| 54 | +repository=${GITHUB_REPOSITORY:-microsoft/CCF} |
| 55 | +main_artifact=perf-bench-virtual-main |
| 56 | + |
| 57 | +while [[ $# -gt 0 ]]; do |
| 58 | + opt=$1 |
| 59 | + case "$opt" in |
| 60 | + --build-dir) build_dir=${2:?$opt requires a value}; shift 2 ;; |
| 61 | + --results-dir) results_dir=${2:?$opt requires a value}; shift 2 ;; |
| 62 | + --main-perf-dir) main_perf_dir=${2:?$opt requires a value}; shift 2 ;; |
| 63 | + --output) output_file=${2:?$opt requires a value}; shift 2 ;; |
| 64 | + --iterations) iterations=${2:?$opt requires a value}; shift 2 ;; |
| 65 | + --label) label=${2:?$opt requires a value}; shift 2 ;; |
| 66 | + --summary) summary=true; shift ;; |
| 67 | + *) echo "Unknown argument: $opt" >&2; usage >&2; exit 1 ;; |
| 68 | + esac |
| 69 | +done |
| 70 | + |
| 71 | +if ! [[ "$iterations" =~ ^[1-9][0-9]*$ ]]; then |
| 72 | + echo "--iterations must be a positive integer" >&2 |
| 73 | + exit 1 |
| 74 | +fi |
| 75 | + |
| 76 | +output_file=${output_file:-"$results_dir/report.md"} |
| 77 | + |
| 78 | +build_dir=$(abs_path "$build_dir") |
| 79 | +results_dir=$(abs_path "$results_dir") |
| 80 | +main_perf_dir=$(abs_path "$main_perf_dir") |
| 81 | +output_file=$(abs_path "$output_file") |
| 82 | + |
| 83 | +restore_main_perf() { |
| 84 | + mkdir -p "$main_perf_dir" |
| 85 | + tmp_dir=$(mktemp -d) |
| 86 | + restored=false |
| 87 | + |
| 88 | + echo "Restoring latest main perf results..." |
| 89 | + if command -v gh >/dev/null 2>&1; then |
| 90 | + run_ids=$(gh api "repos/$repository/actions/artifacts?name=$main_artifact&per_page=100" \ |
| 91 | + --jq '[.artifacts[] | select(.expired == false)] | sort_by(.created_at) | reverse | .[0:10] | .[].workflow_run.id') || run_ids= |
| 92 | + |
| 93 | + for run_id in $run_ids; do |
| 94 | + download_dir="$tmp_dir/$run_id" |
| 95 | + mkdir -p "$download_dir" |
| 96 | + if gh run download "$run_id" --repo "$repository" --name "$main_artifact" --dir "$download_dir" >/dev/null 2>&1; then |
| 97 | + [[ -n "$(find "$download_dir" -name '*.json' -print -quit)" ]] && restored=true |
| 98 | + fi |
| 99 | + done |
| 100 | + elif command -v curl >/dev/null 2>&1 && command -v unzip >/dev/null 2>&1; then |
| 101 | + artifacts_json="$tmp_dir/artifacts.json" |
| 102 | + if curl -fsSL \ |
| 103 | + -H "Accept: application/vnd.github+json" \ |
| 104 | + "https://api.github.com/repos/$repository/actions/artifacts?name=$main_artifact&per_page=100" \ |
| 105 | + -o "$artifacts_json"; then |
| 106 | + python3 - "$artifacts_json" > "$tmp_dir/urls" <<'PY' |
| 107 | +import json |
| 108 | +import sys |
| 109 | +
|
| 110 | +with open(sys.argv[1], encoding="utf-8") as f: |
| 111 | + data = json.load(f) |
| 112 | +
|
| 113 | +artifacts = [ |
| 114 | + artifact |
| 115 | + for artifact in data.get("artifacts", []) |
| 116 | + if not artifact.get("expired") |
| 117 | +] |
| 118 | +for artifact in sorted( |
| 119 | + artifacts, key=lambda artifact: artifact.get("created_at", ""), reverse=True |
| 120 | +)[:10]: |
| 121 | + print(artifact["archive_download_url"]) |
| 122 | +PY |
| 123 | + |
| 124 | + index=0 |
| 125 | + while IFS= read -r url; do |
| 126 | + index=$((index + 1)) |
| 127 | + archive="$tmp_dir/$index.zip" |
| 128 | + download_dir="$tmp_dir/$index" |
| 129 | + mkdir -p "$download_dir" |
| 130 | + if curl -fsSL -L "$url" -o "$archive" && |
| 131 | + unzip -q "$archive" -d "$download_dir"; then |
| 132 | + [[ -n "$(find "$download_dir" -name '*.json' -print -quit)" ]] && restored=true |
| 133 | + fi |
| 134 | + done < "$tmp_dir/urls" |
| 135 | + fi |
| 136 | + fi |
| 137 | + |
| 138 | + if [[ "$restored" == "true" ]]; then |
| 139 | + rm -f "$main_perf_dir"/*.json |
| 140 | + find "$tmp_dir" -name '*.json' -exec cp {} "$main_perf_dir"/ \; |
| 141 | + echo "Main perf results saved in $main_perf_dir" |
| 142 | + else |
| 143 | + echo "Could not restore main perf results; using $main_perf_dir" >&2 |
| 144 | + fi |
| 145 | + rm -rf "$tmp_dir" |
| 146 | +} |
| 147 | + |
| 148 | +run_benchmarks() { |
| 149 | + mkdir -p "$build_dir" "$results_dir" |
| 150 | + rm -f "$results_dir"/bencher-pr-*.json |
| 151 | + log_dir="$results_dir/logs" |
| 152 | + mkdir -p "$log_dir" |
| 153 | + rm -f "$log_dir"/*.log |
| 154 | + |
| 155 | + build_log="$log_dir/build.log" |
| 156 | + echo "Building..." |
| 157 | + { |
| 158 | + cmake -S "$ROOT_DIR" -B "$build_dir" -GNinja -DWORKER_THREADS=2 && |
| 159 | + cmake --build "$build_dir" |
| 160 | + } > "$build_log" 2>&1 || { |
| 161 | + echo "Build failed. See $build_log" >&2 |
| 162 | + exit 1 |
| 163 | + } |
| 164 | + |
| 165 | + pushd "$build_dir" >/dev/null |
| 166 | + for ((i = 1; i <= iterations; i++)); do |
| 167 | + echo "Running $i/$iterations..." |
| 168 | + run_log="$log_dir/iteration-${i}.log" |
| 169 | + rm -f bencher.json |
| 170 | + { |
| 171 | + ./tests.sh -VV -L benchmark && |
| 172 | + ./tests.sh -VV -L perf -C perf && |
| 173 | + PYTHONPATH="$ROOT_DIR/tests" env/bin/python convert_pico_to_bencher.py |
| 174 | + } > "$run_log" 2>&1 || { |
| 175 | + echo "Iteration $i failed. See $run_log" >&2 |
| 176 | + exit 1 |
| 177 | + } |
| 178 | + result="$results_dir/bencher-pr-${i}.json" |
| 179 | + mv bencher.json "$result" |
| 180 | + done |
| 181 | + popd >/dev/null |
| 182 | + |
| 183 | + echo "Benchmark results saved in $results_dir" |
| 184 | + echo "Logs saved in $log_dir" |
| 185 | +} |
| 186 | + |
| 187 | +write_report() { |
| 188 | + mkdir -p "$( dirname "$output_file" )" |
| 189 | + python3 "$ROOT_DIR/scripts/compare_bencher_ab.py" \ |
| 190 | + "$main_perf_dir" \ |
| 191 | + "$results_dir" \ |
| 192 | + --label2 "$label" > "$output_file" |
| 193 | + |
| 194 | + if [[ "$summary" == "true" ]]; then |
| 195 | + if [[ -z "${GITHUB_STEP_SUMMARY:-}" ]]; then |
| 196 | + echo "GITHUB_STEP_SUMMARY is not set" >&2 |
| 197 | + exit 1 |
| 198 | + fi |
| 199 | + cat "$output_file" >> "$GITHUB_STEP_SUMMARY" |
| 200 | + fi |
| 201 | + |
| 202 | + echo "Report saved in $output_file" |
| 203 | +} |
| 204 | + |
| 205 | +case "$cmd" in |
| 206 | + run) run_benchmarks ;; |
| 207 | + report) write_report ;; |
| 208 | + local) restore_main_perf; run_benchmarks; write_report; echo "Done." ;; |
| 209 | +esac |
0 commit comments