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 toYour Repo -> Settings -> CI/CD -> Variables -> Expand -> Add variable
. The variable key should beBENCHER_API_TOKEN
and the variable value should be your API token. Check both theProtect variable
andMask 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 branch selection 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
--adapter
option to the desired benchmark harness adapter. See benchmark harness adapters for a full overview. (ex:--adapter json
) - Set the
--err
flag to fail the command if an Alert is generated. See Threshold & Alerts for a full overview. (ex:--err
) - 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.
- Create or update a 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 branch selection for a full overview. (ex:--branch "$CI_COMMIT_REF_NAME"
) - Set the
--branch-start-point
option to the MR target Branch start point using a GitLab CI/CD predefined variable. See branch selection for a full overview. (ex:--branch-start-point "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
) - Set the
--branch-start-point-hash
option to the MR target Branch start point hash using a GitLab CI/CD predefined variable. See branch selection for a full overview. (ex:--branch-start-point-hash "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA"
) - Set the
--branch-reset
flag to always reset the Branch to the start point. This will prevent benchmark data drift. See branch selection for a full overview. (ex:--branch-reset
) - 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
--adapter
option to the desired benchmark harness adapter. See benchmark harness adapters for a full overview. (ex:--adapter json
) - Set the
--err
flag to fail the command if an Alert is generated. See Threshold & Alerts for a full overview. (ex:--err
) - Specify the benchmark command arguments.
See benchmark command for a full overview.
(ex:
bencher mock
)
🐰 Congrats! You have learned how to use Bencher in GitLab CI/CD! 🎉