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.
- Create a GitLab CI/CD file.
(ex:
.gitlab-ci.yml
) - Create a GitLab CI/CD job.
(ex:
benchmark_target_branch
) - Run
if
the pipeline was triggered by apush
to themain
branch. See the GitLab CI/CDrules
documentation and GitLab CI/CD predefined variables documentation for a full overview. (ex:rules: if: ...
) - Set the
image
the job will run in. See the GitLab CI/CDimage
documentation for a full overview. (ex:image: debian:bullseye
) - Install the Bencher CLI using the convenience script.
(ex:
before_script: ...
) - Use the
bencher run
CLI subcommand to run yourmain
branch benchmarks. See thebencher run
CLI subcommand for a full overview. (ex:bencher run
) - Set the
--project
option to the Project slug. See the--project
docs for more details. (ex:--project save-walter-white-1234abcd
) - Set the
--token
option to the maskedBENCHER_API_TOKEN
environment variable. See the--token
docs for more details. (ex:--token "$BENCHER_API_TOKEN"
) - Set the
--branch
option to the Branch name. See the--branch
docs for a full overview. (ex:--branch main
) - Set the
--testbed
option to the Testbed name. This should likely match the machine selected inimage
. See the--tested
docs for more details. (ex:--testbed debian:bullseye
) - Set the Threshold for the
main
Branch,debian:bullseye
Testbed, andlatency
Measure:- Set the
--threshold-measure
option to the built-inlatency
Measure that is generated bybencher mock
. See the--threshold-measure
docs for more details. (ex:--threshold-measure latency
) - Set the
--threshold-test
option to a Student’s t-test (t_test
). See the--threshold-test
docs for a full overview. (ex:--threshold-test t_test
) - Set the
--threshold-max-sample-size
option to the maximum sample size of64
. See the--threshold-max-sample-size
docs for more details. (ex:--threshold-max-sample-size 64
) - Set the
--threshold-upper-boundary
option to the Upper Boundary of0.99
. See the--threshold-upper-boundary
docs for more details. (ex:--threshold-upper-boundary 0.99
) - Set the
--thresholds-reset
flag so that only the specified Threshold is active. See the--thresholds-reset
docs for a full overview. (ex:--thresholds-reset
)
- Set the
- Set the
--err
flag to fail the command if an Alert is generated. See the--err
docs for a full overview. (ex:--err
) - Set the
--adapter
option 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.
- Update the GitLab CI/CD file.
(ex:
.gitlab-ci.yml
) - Create a GitLab CI/CD job.
(ex:
benchmark_mr_branch
) - Run
if
the pipeline was triggered by amerge_request_event
. See the GitLab CI/CDrules
documentation and GitLab CI/CD predefined variables documentation for a full overview. (ex:rules: if: ...
) - Set the
image
the job will run in. See the GitLab CI/CDimage
documentation for a full overview. (ex:image: debian:bullseye
) - Install the Bencher CLI using the convenience script.
(ex:
before_script: ...
) - Use the
bencher run
CLI subcommand to run your merge request branch benchmarks. See thebencher run
CLI subcommand for a full overview. (ex:bencher run
) - Set the
--project
option to the Project slug. See the--project
docs for more details. (ex:--project save-walter-white-1234abcd
) - Set the
--token
option to the maskedBENCHER_API_TOKEN
environment variable. See the--token
docs for more details. (ex:--token "$BENCHER_API_TOKEN"
) - Set the
--branch
option to the MR branch name using a GitLab CI/CD predefined variable. See the--branch
docs for a full overview. (ex:--branch "$CI_COMMIT_REF_NAME"
) - Set the Start Point for the MR Branch:
- Set the
--start-point
option to the MR Branch start point using a GitLab CI/CD predefined variable. See the--start-point
docs for a full overview. (ex:--start-point "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
) - Set the
--start-point-hash
option to the MR Branch start pointgit
hash using a GitLab CI/CD predefined variable. See the--start-point-hash
docs for a full overview. (ex:--start-point-hash "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA"
) - Set the
--start-point-clone-thresholds
flag to clone the Thresholds from the start point. See the--start-point-clone-thresholds
docs for a full overview. (ex:--start-point-clone-thresholds
) - Set the
--start-point-reset
flag to always reset the MR Branch to the start point. This will prevent benchmark data drift. See the--start-point-reset
docs for a full overview. (ex:--start-point-reset
)
- Set the
- Set the
--testbed
option to the Testbed name. This should likely match the machine selected inimage
. See the--tested
docs for more details. (ex:--testbed debian:bullseye
) - Set the
--err
flag to fail the command if an Alert is generated. See the--err
docs for a full overview. (ex:--err
) - Set the
--adapter
option 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.
- Update the GitLab CI/CD file.
(ex:
.gitlab-ci.yml
) - Create a GitLab CI/CD job.
(ex:
archive_mr_branch
) - Run
if
the pipeline was triggered by amerge_request_event
. See the GitLab CI/CDrules
documentation and GitLab CI/CD predefined variables documentation for a full overview. (ex:rules: if: ...
) - Set the
image
the job will run in. See the GitLab CI/CDimage
documentation 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 archive
CLI subcommand to archive the MR branch if the MR state isclosed
. (ex:bencher archive
) - Set the
--project
option to the Project slug. See the--project
docs for more details. (ex:--project save-walter-white-1234abcd
) - Set the
--token
option to the maskedBENCHER_API_TOKEN
environment variable. See the--token
docs for more details. (ex:--token "$BENCHER_API_TOKEN"
) - Set the
--branch
option 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! 🎉