CircleCI
Denne veiledningen forklarer hvordan du integrerer LoadFocus JMeter API-klienten med CircleCI for automatisert ytelsestesting.
Oppsettstrinn
1. Legg til miljøvariabler
Lagre LoadFocus API-legitimasjonen din som miljøvariabler i CircleCI:
- Naviger til prosjektet ditt i CircleCI
- Gå til Project Settings > Environment Variables
- Legg til følgende variabler:
LOADFOCUS_API_KEY: Din LoadFocus API-nøkkelLOADFOCUS_TEAM_ID: Din LoadFocus team-ID
2. Konfigurer CircleCI-arbeidsflyt
Opprett eller oppdater .circleci/config.yml-filen din for å inkludere ytelsestesting:
version: 2.1jobs:performance_test:docker:- image: cimg/node:16.13steps:- checkout- run:name: Install LoadFocus JMeter API Clientcommand: npm install -g @loadfocus/loadfocus-api-client- run:name: Configure LoadFocus API Clientcommand: |loadfocus-api config set apikey $LOADFOCUS_API_KEYloadfocus-api config set teamid $LOADFOCUS_TEAM_ID- run:name: Run Performance Testscommand: |loadfocus-api jmeter run-test \--name "CircleCI_${CIRCLE_PROJECT_REPONAME}_${CIRCLE_BRANCH}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json- store_artifacts:path: performance_results.jsondestination: performance-test-resultsworkflows:version: 2build_test_deploy:jobs:- build_and_test- performance_test:requires:- build_and_test- deploy:requires:- performance_testfilters:branches:only: main
3. Feile bygget ved ytelsesproblemer
For at pipelinen din skal feile når ytelsesterskelverdier ikke oppfylles, modifiser "Run Performance Tests"-steget:
- run:name: Run Performance Testscommand: |loadfocus-api jmeter run-test \--name "CircleCI_${CIRCLE_PROJECT_REPONAME}_${CIRCLE_BRANCH}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json# Check exit code - the command will exit with code 1 if thresholds are not metif [ $? -ne 0 ]; thenecho "Performance test failed to meet thresholds"exit 1fi
Avansert konfigurasjon
Parallell testing
For å kjøre flere ytelsestester parallelt:
performance_tests:docker:- image: cimg/node:16.13parallelism: 3steps:- checkout- run:name: Install LoadFocus JMeter API Clientcommand: npm install -g @loadfocus/loadfocus-api-client- run:name: Configure LoadFocus API Clientcommand: |loadfocus-api config set apikey $LOADFOCUS_API_KEYloadfocus-api config set teamid $LOADFOCUS_TEAM_ID- run:name: Run Performance Testscommand: |# Get test name based on indexTESTS=("API_Test" "Frontend_Test" "Database_Test")INDEX=$(( $CIRCLE_NODE_INDEX % 3 ))TEST_NAME=${TESTS[$INDEX]}echo "Running test: $TEST_NAME"loadfocus-api jmeter run-test \--name "$TEST_NAME" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > "performance_results_${TEST_NAME}.json"- store_artifacts:path: performance_results_*.jsondestination: performance-test-results
Tips for CircleCI-integrasjon
Ressurstildeling: Sørg for at CircleCI-planen din har tilstrekkelige ressurser for å kjøre ytelsestester, spesielt hvis de er langvarige.
Tidsavbruddsinnstillinger: For lengre tester, juster jobbens tidsavbrudd i CircleCI:
- run:name: Run Performance Testscommand: loadfocus-api jmeter run-test --name "Test_Name" --waitTimeout 3600no_output_timeout: 60mBetinget testing: Kjør ytelsestester kun på spesifikke grener eller for spesifikke commits:
performance_test:# Only run if commit message contains [PERF-TEST]when:condition:or:- equal: [ main, << pipeline.git.branch >> ]- matches:pattern: ".*\\[PERF-TEST\\].*"value: << pipeline.git.commit.message >>Varsler: Sett opp varsler for feil i ytelsestester:
- run:name: Notify on Performance Test Failurecommand: |if [ $? -ne 0 ]; thencurl -X POST -H "Content-Type: application/json" \-d '{"text":"Performance test failed for $CIRCLE_PROJECT_REPONAME"}' \$SLACK_WEBHOOK_URLfiwhen: on_fail
For mer informasjon, se CircleCI-dokumentasjonen og LoadFocus API-klientdokumentasjonen.