benchmmark.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env bash
  2. current_branch=$(git rev-parse --abbrev-ref HEAD)
  3. if [ "$current_branch" == "HEAD" ]; then
  4. current_branch=$(git rev-parse HEAD)
  5. fi
  6. restore_branch() {
  7. echo "Restoring original branch/commit: $current_branch"
  8. git checkout "$current_branch"
  9. }
  10. trap restore_branch EXIT
  11. # Check if there are uncommitted changes
  12. if ! git diff --quiet || ! git diff --cached --quiet; then
  13. echo "There are uncommitted changes. Please commit or stash them before running this script."
  14. exit 1
  15. fi
  16. # Ensure that at least one commit argument is passed
  17. if [ "$#" -lt 1 ]; then
  18. echo "Usage: $0 <commit1> <commit2> ... <commitN>"
  19. exit 1
  20. fi
  21. commits=("$@")
  22. benchmarks_dir=benchmarks
  23. if ! mkdir -p "${benchmarks_dir}"; then
  24. echo "Unable to create dir for benchmarks data"
  25. exit 1
  26. fi
  27. # Benchmark results
  28. bench_files=()
  29. # Run benchmark for each listed commit
  30. for i in "${!commits[@]}"; do
  31. commit="${commits[i]}"
  32. git checkout "$commit" || {
  33. echo "Failed to checkout $commit"
  34. exit 1
  35. }
  36. # Sanitized commmit message
  37. commit_message=$(git log -1 --pretty=format:"%s" | tr -c '[:alnum:]-_' '_')
  38. # Benchmark data will go there
  39. bench_file="${benchmarks_dir}/${i}_${commit_message}.bench"
  40. if ! go test -bench=. -count=10 >"$bench_file"; then
  41. echo "Benchmarking failed for commit $commit"
  42. exit 1
  43. fi
  44. bench_files+=("$bench_file")
  45. done
  46. # go install golang.org/x/perf/cmd/benchstat[@latest]
  47. benchstat "${bench_files[@]}"