How to use Bencher in GitLab CI/CD
Depending on your use case, you can set up Continuous Benchmarking in GitLab CI/CD for your:
Make sure you have created an API token
and set it as a masked variable named BENCHER_API_TOKEN before continuing on!
Navigate to Your Repo -> Settings -> CI/CD -> Variables -> Expand -> Add variable.
The variable key should be BENCHER_API_TOKEN and the variable value should be your API token.
Check both the Protect variable and Mask variable boxes.
Target Branch
A cornerstone of Statistical Continuous Benchmarking is having a historical baseline for your target branch. This historical baseline can then be used to detect performance regressions in Merge Requests.
benchmark_target_branch: rules: - if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push" when: always image: debian:bullseye before_script: - curl --proto '=https' --tlsv1.2 -sSfL https://bencher.dev/download/install-cli.sh | sh script: - | bencher run \ --project project-abc4567-wxyz123456789 \ --token "$BENCHER_API_TOKEN" \ --branch main \ --testbed debian:bullseye \ --threshold-measure latency \ --threshold-test t_test \ --threshold-max-sample-size 64 \ --threshold-upper-boundary 0.99 \ --thresholds-reset \ --err \ --adapter json \ bencher mock- Create a GitLab CI/CD file.
(ex:
.gitlab-ci.yml) - Create a GitLab CI/CD job.
(ex:
benchmark_target_branch) - Run
ifthe pipeline was triggered by apushto themainbranch. See the GitLab CI/CDrulesdocumentation and GitLab CI/CD predefined variables documentation for a full overview. (ex:rules: if: ...) - Set the
imagethe job will run in. See the GitLab CI/CDimagedocumentation for a full overview. (ex:image: debian:bullseye) - Install the Bencher CLI using the convenience script.
(ex:
before_script: ...) - Use the
bencher runCLI subcommand to run yourmainbranch benchmarks. See thebencher runCLI subcommand for a full overview. (ex:bencher run) - Set the
--projectoption to the Project slug. See the--projectdocs for more details. (ex:--project project-abc4567-wxyz123456789) - Set the
--tokenoption to the maskedBENCHER_API_TOKENenvironment variable. See the--tokendocs for more details. (ex:--token "$BENCHER_API_TOKEN") - Set the
--branchoption to the Branch name. See the--branchdocs for a full overview. (ex:--branch main) - Set the
--testbedoption to the Testbed name. This should likely match the machine selected inimage. See the--testeddocs for more details. (ex:--testbed debian:bullseye) - Set the Threshold for the
mainBranch,debian:bullseyeTestbed, andlatencyMeasure:- Set the
--threshold-measureoption to the built-inlatencyMeasure that is generated bybencher mock. See the--threshold-measuredocs for more details. (ex:--threshold-measure latency) - Set the
--threshold-testoption to a Student’s t-test (t_test). See the--threshold-testdocs for a full overview. (ex:--threshold-test t_test) - Set the
--threshold-max-sample-sizeoption to the maximum sample size of64. See the--threshold-max-sample-sizedocs for more details. (ex:--threshold-max-sample-size 64) - Set the
--threshold-upper-boundaryoption to the Upper Boundary of0.99. See the--threshold-upper-boundarydocs for more details. (ex:--threshold-upper-boundary 0.99) - Set the
--thresholds-resetflag so that only the specified Threshold is active. See the--thresholds-resetdocs for a full overview. (ex:--thresholds-reset)
- Set the
- Set the
--errflag to fail the command if an Alert is generated. See the--errdocs for a full overview. (ex:--err) - Set the
--adapteroption to Bencher Metric Format JSON (json) that is generated bybencher mock. See benchmark harness adapters for a full overview. (ex:--adapter json) - Specify the benchmark command arguments.
See benchmark command for a full overview.
(ex:
bencher mock)
Merge Requests
In order to catch performance regression in Merge Requests, you will need to run your benchmarks on MRs. The below example should only be used for branches within the same repository.
benchmark_mr_branch: rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" when: always image: debian:bullseye before_script: - curl --proto '=https' --tlsv1.2 -sSfL https://bencher.dev/download/install-cli.sh | sh script: - | bencher run \ --project project-abc4567-wxyz123456789 \ --token "$BENCHER_API_TOKEN" \ --branch "$CI_COMMIT_REF_NAME" \ --start-point "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" \ --start-point-hash "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA" \ --start-point-clone-thresholds \ --start-point-reset \ --testbed debian:bullseye \ --err \ --adapter json \ bencher mock- Update the GitLab CI/CD file.
(ex:
.gitlab-ci.yml) - Create a GitLab CI/CD job.
(ex:
benchmark_mr_branch) - Run
ifthe pipeline was triggered by amerge_request_event. See the GitLab CI/CDrulesdocumentation and GitLab CI/CD predefined variables documentation for a full overview. (ex:rules: if: ...) - Set the
imagethe job will run in. See the GitLab CI/CDimagedocumentation for a full overview. (ex:image: debian:bullseye) - Install the Bencher CLI using the convenience script.
(ex:
before_script: ...) - Use the
bencher runCLI subcommand to run your merge request branch benchmarks. See thebencher runCLI subcommand for a full overview. (ex:bencher run) - Set the
--projectoption to the Project slug. See the--projectdocs for more details. (ex:--project project-abc4567-wxyz123456789) - Set the
--tokenoption to the maskedBENCHER_API_TOKENenvironment variable. See the--tokendocs for more details. (ex:--token "$BENCHER_API_TOKEN") - Set the
--branchoption to the MR branch name using a GitLab CI/CD predefined variable. See the--branchdocs for a full overview. (ex:--branch "$CI_COMMIT_REF_NAME") - Set the Start Point for the MR Branch:
- Set the
--start-pointoption to the MR Branch start point using a GitLab CI/CD predefined variable. See the--start-pointdocs for a full overview. (ex:--start-point "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME") - Set the
--start-point-hashoption to the MR Branch start pointgithash using a GitLab CI/CD predefined variable. See the--start-point-hashdocs for a full overview. (ex:--start-point-hash "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA") - Set the
--start-point-clone-thresholdsflag to clone the Thresholds from the start point. See the--start-point-clone-thresholdsdocs for a full overview. (ex:--start-point-clone-thresholds) - Set the
--start-point-resetflag to always reset the MR Branch to the start point. This will prevent benchmark data drift. See the--start-point-resetdocs for a full overview. (ex:--start-point-reset)
- Set the
- Set the
--testbedoption to the Testbed name. This should likely match the machine selected inimage. See the--testeddocs for more details. (ex:--testbed debian:bullseye) - Set the
--errflag to fail the command if an Alert is generated. See the--errdocs for a full overview. (ex:--err) - Set the
--adapteroption to Bencher Metric Format JSON (json) that is generated bybencher mock. See benchmark harness adapters for a full overview. (ex:--adapter json) - Specify the benchmark command arguments.
See benchmark command for a full overview.
(ex:
bencher mock)
To clean up the MR branch after its MR is closed,
you can create a separate job that queries for the MR state using the GitLab API.
If the state is closed, this job will archive the MR branch using the bencher archive command.
archive_mr_branch: rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" when: always image: debian:bullseye before_script: - curl --proto '=https' --tlsv1.2 -sSfL https://bencher.dev/download/install-cli.sh | sh - | MR_STATE=$(curl --header "PRIVATE-TOKEN: $CI_JOB_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID" | jq -r .state) echo "Merge request state: $MR_STATE" script: - | if [ "$MR_STATE" = "closed" ]; then bencher archive \ --project project-abc4567-wxyz123456789 \ --token "$BENCHER_API_TOKEN" \ --branch "$CI_COMMIT_REF_NAME" else echo 'Merge request is not `closed`. Skipping archival.' fi- Update the GitLab CI/CD file.
(ex:
.gitlab-ci.yml) - Create a GitLab CI/CD job.
(ex:
archive_mr_branch) - Run
ifthe pipeline was triggered by amerge_request_event. See the GitLab CI/CDrulesdocumentation and GitLab CI/CD predefined variables documentation for a full overview. (ex:rules: if: ...) - Set the
imagethe job will run in. See the GitLab CI/CDimagedocumentation for a full overview. (ex:image: debian:bullseye) - Install the Bencher CLI using the convenience script.
(ex:
before_script: curl ...) - Check the MR state using the GitLab API.
(ex:
before_script: MR_STATE=$(...)) - Use the
bencher archiveCLI subcommand to archive the MR branch if the MR state isclosed. (ex:bencher archive) - Set the
--projectoption to the Project slug. See the--projectdocs for more details. (ex:--project project-abc4567-wxyz123456789) - Set the
--tokenoption to the maskedBENCHER_API_TOKENenvironment variable. See the--tokendocs for more details. (ex:--token "$BENCHER_API_TOKEN") - Set the
--branchoption to the MR branch name using a GitLab CI/CD predefined variable. (ex:--branch "$CI_COMMIT_REF_NAME")
🐰 Congrats! You have learned how to use Bencher in GitLab CI/CD! 🎉