2024-04-23 12:18:22 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2023-12-19 12:04:02 +03:00
|
|
|
# Benchmark script to compare the benchmarks caused by the changes of the
|
|
|
|
# current PR against the main branch
|
|
|
|
|
2024-04-23 12:18:22 +03:00
|
|
|
set -e
|
|
|
|
set -u
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
set -x
|
|
|
|
|
|
|
|
# Build without needing to read the Git context
|
|
|
|
export RELEASE_VERSION='bench'
|
|
|
|
|
2023-12-19 12:04:02 +03:00
|
|
|
# Run the benchmarks on the current PR and save it in a JSON file
|
|
|
|
#
|
|
|
|
# TODO: naveen, Benchmark seperate targets and merge their JSON. Currently,
|
|
|
|
# `--save-baseline` only supports saving benchmarks for a single target. For
|
|
|
|
# now, we'll benchmark only the `execute` target
|
|
|
|
# See: https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options
|
|
|
|
#
|
2024-04-23 12:18:22 +03:00
|
|
|
# cargo bench --bench execute --bench generate_ir --bench validation --bench parser -- --save-baseline current-benchmarks
|
|
|
|
cargo bench --bench execute -- --save-baseline current-benchmarks
|
|
|
|
critcmp --export current-benchmarks > current-benchmarks.json
|
2023-12-19 12:04:02 +03:00
|
|
|
|
|
|
|
# Clone into the main branch of v3-engine
|
|
|
|
# https://$2@github.com/hasura/v3-engine.git
|
2024-04-23 12:18:22 +03:00
|
|
|
git clone --branch main "https://${2}@github.com/hasura/v3-engine.git" main-copy
|
|
|
|
pushd main-copy
|
2023-12-19 12:04:02 +03:00
|
|
|
# Run the benchmarks on the main branch and save it in a JSON file
|
2024-04-23 12:18:22 +03:00
|
|
|
cargo bench --bench execute -- --save-baseline main-benchmarks
|
|
|
|
critcmp --export main-benchmarks > /app/main-benchmarks.json
|
2023-12-19 12:04:02 +03:00
|
|
|
popd
|
|
|
|
|
|
|
|
# Compare between the benchmarks of main and the benchmarks generated by the PR
|
2024-04-23 12:18:22 +03:00
|
|
|
critcmp main-benchmarks.json current-benchmarks.json > diff.txt
|
2023-12-19 12:04:02 +03:00
|
|
|
|
2024-04-23 12:18:22 +03:00
|
|
|
# Format the result of benchmark into a Code comment, so that when we
|
2023-12-19 12:04:02 +03:00
|
|
|
# comment on the PR, the benchmark results are displayed in a nice format
|
2024-04-23 12:18:22 +03:00
|
|
|
sed -i '1s/^/Benchmark report\n ```\n/' diff.txt
|
|
|
|
echo '```' >> diff.txt
|
2023-12-19 12:04:02 +03:00
|
|
|
|
|
|
|
# Format the results into a json format
|
|
|
|
# { "body": "<BENCHMARK AS STRING>""}
|
2024-04-23 12:18:22 +03:00
|
|
|
jq -n --rawfile diff diff.txt '.body=$diff' > bench_comment.json
|
2023-12-19 12:04:02 +03:00
|
|
|
|
|
|
|
curl -s -H "Authorization: token $2" \
|
|
|
|
-X GET "https://api.github.com/repos/hasura/v3-engine/issues/$1/comments" > /tmp/comments.json
|
|
|
|
comment_id=$(jq 'map(select(.body | contains ("Benchmark report"))) | last | .id' /tmp/comments.json)
|
2024-04-23 12:18:22 +03:00
|
|
|
echo "Comment ID: ${comment_id}"
|
|
|
|
|
2023-12-19 12:04:02 +03:00
|
|
|
# Post the benchmark as comment on PR or update existing one
|
2024-04-23 12:18:22 +03:00
|
|
|
if [ "$comment_id" = 'null' ]; then
|
2023-12-19 12:04:02 +03:00
|
|
|
curl -L \
|
|
|
|
-X POST \
|
|
|
|
-H "Accept: application/vnd.github+json" \
|
|
|
|
-H "Authorization: Bearer $2" \
|
|
|
|
-H "X-GitHub-Api-Version: 2022-11-28" \
|
|
|
|
"https://api.github.com/repos/hasura/v3-engine/issues/$1/comments" \
|
|
|
|
-d @bench_comment.json
|
|
|
|
else
|
|
|
|
curl -L \
|
|
|
|
-X PATCH \
|
|
|
|
-H "Accept: application/vnd.github+json" \
|
|
|
|
-H "Authorization: Bearer $2" \
|
|
|
|
-H "X-GitHub-Api-Version: 2022-11-28" \
|
|
|
|
"https://api.github.com/repos/hasura/v3-engine/issues/comments/$comment_id" \
|
|
|
|
-d @bench_comment.json
|
|
|
|
fi
|