Integrazione CircleCI
Questa guida spiega come integrare il Client API JMeter di LoadFocus con CircleCI per test di prestazione automatizzati.
Passaggi di Configurazione
1. Aggiungere le Variabili d'Ambiente
Memorizzate le vostre credenziali API LoadFocus come variabili d'ambiente in CircleCI:
- Navigate al vostro progetto in CircleCI
- Andate su Project Settings > Environment Variables
- Aggiungete le seguenti variabili:
LOADFOCUS_API_KEY: La vostra chiave API LoadFocusLOADFOCUS_TEAM_ID: Il vostro ID team LoadFocus
2. Configurare il Workflow CircleCI
Create o aggiornate il vostro file .circleci/config.yml per includere i test di prestazione:
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. Bloccare la Build in Caso di Problemi di Prestazione
Per far fallire la vostra pipeline quando le soglie di prestazione non vengono rispettate, modificate il passaggio "Run Performance Tests":
- 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
Configurazione Avanzata
Test Paralleli
Per eseguire piu test di prestazione in parallelo:
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
Suggerimenti per l'Integrazione con CircleCI
Allocazione delle Risorse: Assicuratevi che il vostro piano CircleCI disponga di risorse sufficienti per eseguire test di prestazione, specialmente se sono di lunga durata.
Impostazioni di Timeout: Per test piu lunghi, regolate il timeout del job in CircleCI:
- run:name: Run Performance Testscommand: loadfocus-api jmeter run-test --name "Test_Name" --waitTimeout 3600no_output_timeout: 60mTest Condizionali: Eseguite i test di prestazione solo su branch specifici o per commit specifici:
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 >>Notifiche: Configurate le notifiche per i fallimenti dei test di prestazione:
- 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
Per maggiori informazioni, consultate la documentazione di CircleCI e la documentazione del Client API LoadFocus.