Jenkins
Tento průvodce vysvětluje, jak integrovat klienta LoadFocus JMeter API s Jenkins pro automatizované výkonnostní testování.
Kroky nastavení
1. Uložení přihlašovacích údajů v Jenkins
Nejprve bezpečně uložte vaše LoadFocus API přihlašovací údaje v Jenkins:
- Přejděte na Jenkins Dashboard > Manage Jenkins > Manage Credentials
- Vyberte příslušnou doménu přihlašovacích údajů (např. global)
- Klikněte na "Add Credentials"
- Přidejte následující přihlašovací údaje:
- Kind: Secret text
- Scope: Global
- Secret: Váš LoadFocus API klíč
- ID: loadfocus-api-key
- Description: LoadFocus API Key
- Opakujte pro vaše team ID s ID: loadfocus-team-id
2. Vytvoření Jenkins Pipeline
Vytvořte soubor Jenkinsfile ve vašem repozitáři:
pipeline {agent {docker {image 'node:16-alpine'}}environment {LOADFOCUS_API_KEY = credentials('loadfocus-api-key')LOADFOCUS_TEAM_ID = credentials('loadfocus-team-id')}stages {stage('Build') {steps {// Your build stepssh 'npm install'sh 'npm run build'}}stage('Test') {steps {// Your test stepssh 'npm test'}}stage('Performance Test') {steps {// Install LoadFocus JMeter API Clientsh 'npm install -g @loadfocus/loadfocus-api-client'// Configure LoadFocus API Clientsh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'// Run Performance Testssh '''loadfocus-api jmeter run-test \--name "Jenkins_${JOB_NAME}_${BUILD_NUMBER}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json'''// Archive the resultsarchiveArtifacts artifacts: 'performance_results.json', fingerprint: true}}stage('Deploy') {when {expression {return currentBuild.resultIsBetterOrEqualTo('SUCCESS')}}steps {// Your deployment stepsecho 'Deploying...'}}}post {always {// Clean up workspacecleanWs()}}}
3. Konfigurace Jenkins úlohy
- Vytvořte novou Pipeline úlohu v Jenkins
- Nakonfigurujte Pipeline pro použití vašeho Jenkinsfile
- Nastavte příslušnou SCM konfiguraci pro načtení vašeho repozitáře
Pokročilá konfigurace
Deklarativní Pipeline s paralelním testováním
Spusťte více výkonnostních testů paralelně:
pipeline {agent anyenvironment {LOADFOCUS_API_KEY = credentials('loadfocus-api-key')LOADFOCUS_TEAM_ID = credentials('loadfocus-team-id')}stages {// Previous stages...stage('Performance Tests') {parallel {stage('API Performance') {agent {docker {image 'node:16-alpine'}}steps {sh 'npm install -g @loadfocus/loadfocus-api-client'sh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'sh '''loadfocus-api jmeter run-test \--name "API_Performance_Test" \--thresholds "avgresponse<=150,errors==0" \--format json > api_performance_results.json'''archiveArtifacts artifacts: 'api_performance_results.json', fingerprint: true}}stage('UI Performance') {agent {docker {image 'node:16-alpine'}}steps {sh 'npm install -g @loadfocus/loadfocus-api-client'sh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'sh '''loadfocus-api jmeter run-test \--name "UI_Performance_Test" \--thresholds "avgresponse<=300,errors==0" \--format json > ui_performance_results.json'''archiveArtifacts artifacts: 'ui_performance_results.json', fingerprint: true}}}}// Next stages...}}
Skriptovaný Pipeline
Pro větší flexibilitu použijte skriptovaný pipeline:
node {def performanceTestPassed = falsestage('Checkout') {checkout scm}stage('Build & Test') {// Your build and test steps}stage('Performance Test') {docker.image('node:16-alpine').inside {withCredentials([string(credentialsId: 'loadfocus-api-key', variable: 'LOADFOCUS_API_KEY'),string(credentialsId: 'loadfocus-team-id', variable: 'LOADFOCUS_TEAM_ID')]) {sh 'npm install -g @loadfocus/loadfocus-api-client'sh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'try {sh '''loadfocus-api jmeter run-test \--name "Jenkins_${JOB_NAME}_${BUILD_NUMBER}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json'''// Check if test passed by examining the JSONdef testResults = readJSON file: 'performance_results.json'if (testResults.overallResult == 'PASSED') {performanceTestPassed = trueecho "Performance test passed!"} else {echo "Performance test failed to meet thresholds!"// Optional: Fail the build// error "Performance test failed"}} catch (Exception e) {echo "Error running performance test: ${e.message}"}archiveArtifacts artifacts: 'performance_results.json', fingerprint: true}}}stage('Deploy') {if (performanceTestPassed) {echo 'Deploying...'// Your deployment steps} else {echo 'Skipping deployment due to performance test failure'}}}
Sdílená knihovna
Vytvořte sdílenou knihovnu pro znovupoužitelné výkonnostní testování:
// vars/performanceTest.groovydef call(Map config = [:]) {def testName = config.testName ?: "Jenkins_${env.JOB_NAME}_${env.BUILD_NUMBER}"def thresholds = config.thresholds ?: "avgresponse<=200,errors==0,p95<=250"def waitTimeout = config.waitTimeout ?: 1800def resultsFile = config.resultsFile ?: "performance_results.json"docker.image('node:16-alpine').inside {withCredentials([string(credentialsId: 'loadfocus-api-key', variable: 'LOADFOCUS_API_KEY'),string(credentialsId: 'loadfocus-team-id', variable: 'LOADFOCUS_TEAM_ID')]) {sh 'npm install -g @loadfocus/loadfocus-api-client'sh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'sh """loadfocus-api jmeter run-test \\--name "${testName}" \\--thresholds "${thresholds}" \\--waitTimeout ${waitTimeout} \\--format json > ${resultsFile}"""archiveArtifacts artifacts: resultsFile, fingerprint: true// Return the test resultsdef testResults = readJSON file: resultsFilereturn testResults}}}
Poté ve vašem Jenkinsfile:
@Library('my-shared-library') _pipeline {agent anystages {stage('Performance Test') {steps {script {def results = performanceTest(testName: "API_Performance_Test",thresholds: "avgresponse<=150,errors==0")if (results.overallResult != 'PASSED') {error "Performance test failed"}}}}}}
Integrace s Jenkins pluginy
Performance Plugin
Použijte Jenkins Performance Plugin pro vizualizaci výsledků testů:
- Nainstalujte Performance Plugin v Jenkins
- Převeďte výsledky LoadFocus do formátu podporovaného pluginem (JMeter CSV nebo JUnit XML)
- Nakonfigurujte váš pipeline:
stage('Performance Test') {steps {// Run LoadFocus testsh '''loadfocus-api jmeter run-test \--name "Jenkins_${JOB_NAME}_${BUILD_NUMBER}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json# Convert to JMeter CSV format (using a custom script)node convert-to-jmeter.js performance_results.json performance_results.csv'''// Use Performance PluginperfReport sourceDataFiles: 'performance_results.csv',errorFailedThreshold: 0,errorUnstableThreshold: 0,errorUnstableResponseTimeThreshold: '200'}}
E-mailové notifikace
Odesílejte e-mailové notifikace s výsledky testů:
post {always {script {if (fileExists('performance_results.json')) {def results = readJSON file: 'performance_results.json'def resultStatus = results.overallResult == 'PASSED' ? 'SUCCESS' : 'FAILURE'def subject = "Performance Test ${resultStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}"// Create email bodydef body = """<h2>Performance Test Results</h2><p><strong>Overall Result:</strong> ${results.overallResult}</p><h3>Results by Label</h3><table border="1"><tr><th>Label</th><th>Result</th><th>Avg Response</th><th>Errors</th></tr>"""results.labels.each { label ->body += """<tr><td>${label.label}</td><td>${label.result}</td><td>${label.metrics.avgresponse}ms</td><td>${label.metrics.errors}</td></tr>"""}body += "</table>"emailext (subject: subject,body: body,to: 'team@example.com',attachmentsPattern: 'performance_results.json',mimeType: 'text/html')}}}}
Tipy pro integraci s Jenkins
Zpracování timeoutu: Nastavte timeouty pro dlouhotrvající výkonnostní testy:
stage('Performance Test') {options {timeout(time: 60, unit: 'MINUTES')}steps {// Performance test steps}}Podmíněné spuštění: Spouštějte výkonnostní testy pouze na konkrétních větvích:
stage('Performance Test') {when {anyOf {branch 'main'branch 'develop'tag pattern: "v\\d+\\.\\d+\\.\\d+", comparator: "REGEXP"}}steps {// Performance test steps}}Plánované testování: Spouštějte výkonnostní testy podle rozvrhu:
pipeline {agent anytriggers {cron('0 0 * * *') // Run at midnight every day}stages {// Pipeline stages}}Parametrizované testy: Umožněte přizpůsobení parametrů testu:
pipeline {agent anyparameters {string(name: 'TEST_NAME', defaultValue: 'API_Performance_Test', description: 'Name of the LoadFocus test to run')string(name: 'THRESHOLDS', defaultValue: 'avgresponse<=200,errors==0', description: 'Performance thresholds')string(name: 'WAIT_TIMEOUT', defaultValue: '1800', description: 'Maximum wait time in seconds')}stages {stage('Performance Test') {steps {// Run test with parameterssh """loadfocus-api jmeter run-test \\--name "${params.TEST_NAME}" \\--thresholds "${params.THRESHOLDS}" \\--waitTimeout ${params.WAIT_TIMEOUT} \\--format json > performance_results.json"""}}}}
Pro více informací se podívejte na dokumentaci Jenkins a dokumentaci LoadFocus API klienta.