From 6b463cdfc1d18d09e7fd57a46dce5aebb25d29c9 Mon Sep 17 00:00:00 2001 From: Tom French Date: Fri, 15 Nov 2024 12:33:38 +0000 Subject: [PATCH] feat: add example benchmark workflow --- .github/workflows/benchmark.yml | 49 +++++++++++++++++++++++++++++++++ .gitignore | 4 ++- scripts/build-gates-report.sh | 35 +++++++++++++++++++++++ src/lib.nr | 10 +++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/benchmark.yml create mode 100755 scripts/build-gates-report.sh diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 0000000..1fc192b --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,49 @@ +name: Benchmarks + +on: + push: + branches: + - master + pull_request: + +jobs: + test: + name: Benchmark library + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v4 + + - name: Install Nargo + uses: noir-lang/noirup@v0.1.3 + with: + toolchain: 0.34.0 + + - name: Install bb + run: | + npm install -g bbup + bbup -nv 0.34.0 + ls ~/.bb/ + + - name: Build Noir benchmark programs + run: nargo export + + - name: Generate gates report + run: ./scripts/build-gates-report.sh + env: + BACKEND: .bb/bb + + - name: Compare gates reports + id: gates_diff + uses: noir-lang/noir-gates-diff@1931aaaa848a1a009363d6115293f7b7fc72bb87 + with: + report: gates_report.json + summaryQuantile: 0.9 # only display the 10% most significant circuit size diffs in the summary (defaults to 20%) + + - name: Add gates diff to sticky comment + if: github.event_name == 'pull_request' || github.event_name == 'pull_request_target' + uses: marocchino/sticky-pull-request-comment@v2 + with: + # delete the comment in case changes no longer impact circuit sizes + delete: ${{ !steps.gates_diff.outputs.markdown }} + message: ${{ steps.gates_diff.outputs.markdown }} diff --git a/.gitignore b/.gitignore index 1de5659..4aacd17 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -target \ No newline at end of file +target +export +gates_report.json diff --git a/scripts/build-gates-report.sh b/scripts/build-gates-report.sh new file mode 100755 index 0000000..7e4b531 --- /dev/null +++ b/scripts/build-gates-report.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -e + +BACKEND=${BACKEND:-bb} + +cd $(dirname "$0")/../ + +artifacts_path="./export" +artifacts=$(ls $artifacts_path) + +echo "{\"programs\": [" > gates_report.json + +# Bound for checking where to place last parentheses +NUM_ARTIFACTS=$(ls -1q "$artifacts_path" | wc -l) + +ITER="1" +for artifact in $artifacts; do + ARTIFACT_NAME=$(basename "$artifact") + + GATES_INFO=$($BACKEND gates -b "$artifacts_path/$artifact") + MAIN_FUNCTION_INFO=$(echo $GATES_INFO | jq -r '.functions[0] | .name = "main"') + echo "{\"package_name\": \"$ARTIFACT_NAME\", \"functions\": [$MAIN_FUNCTION_INFO]" >> gates_report.json + + if (($ITER == $NUM_ARTIFACTS)); then + echo "}" >> gates_report.json + else + echo "}, " >> gates_report.json + fi + + ITER=$(( $ITER + 1 )) +done + +echo "]}" >> gates_report.json + + diff --git a/src/lib.nr b/src/lib.nr index 5b65ad6..80b968c 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -3,3 +3,13 @@ fn smoke_test() { assert(true); } + +// This is an example benchmark. +// Changes to the number of constraints generated by this function will show in PRs. +#[export] +fn bench_test(mut x: Field) -> Field { + for _ in 0..100 { + x *= x; + } + x +}