Jenkins
Ten przewodnik wyjasnia, jak zintegrowac klienta API JMeter LoadFocus z Jenkins w celu automatycznego testowania wydajnosci.
Kroki konfiguracji
1. Przechowywanie danych uwierzytelniajacych w Jenkins
Najpierw przechowuj dane uwierzytelniajace API LoadFocus w bezpieczny sposob w Jenkins:
- Przejdz do Jenkins Dashboard > Manage Jenkins > Manage Credentials
- Wybierz odpowiednia domene poswiadczen (np. global)
- Kliknij "Add Credentials"
- Dodaj nastepujace poswiadczenia:
- Kind: Secret text
- Scope: Global
- Secret: Twoj klucz API LoadFocus
- ID: loadfocus-api-key
- Description: LoadFocus API Key
- Powtorz dla ID zespolu z ID: loadfocus-team-id
2. Utworz potok Jenkins
Utworz plik Jenkinsfile w swoim repozytorium:
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 {// Twoje kroki budowaniash 'npm install'sh 'npm run build'}}stage('Test') {steps {// Twoje kroki testowaniash 'npm test'}}stage('Performance Test') {steps {// Zainstaluj klienta API JMeter LoadFocussh 'npm install -g @loadfocus/loadfocus-api-client'// Skonfiguruj klienta API LoadFocussh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'// Uruchom testy wydajnoscish '''loadfocus-api jmeter run-test \--name "Jenkins_${JOB_NAME}_${BUILD_NUMBER}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json'''// Zarchiwizuj wynikiarchiveArtifacts artifacts: 'performance_results.json', fingerprint: true}}stage('Deploy') {when {expression {return currentBuild.resultIsBetterOrEqualTo('SUCCESS')}}steps {// Twoje kroki wdrozeniaecho 'Deploying...'}}}post {always {// Wyczysc przestrzen roboczacleanWs()}}}
3. Konfiguracja zadania Jenkins
- Utworz nowe zadanie Pipeline w Jenkins
- Skonfiguruj Pipeline do uzywania Twojego Jenkinsfile
- Skonfiguruj odpowiednie ustawienia SCM do pobierania repozytorium
Zaawansowana konfiguracja
Deklaratywny potok z testowaniem rownolegym
Uruchom wiele testow wydajnosci rownolegle:
pipeline {agent anyenvironment {LOADFOCUS_API_KEY = credentials('loadfocus-api-key')LOADFOCUS_TEAM_ID = credentials('loadfocus-team-id')}stages {// Poprzednie etapy...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}}}}// Nastepne etapy...}}
Wspoldzielona biblioteka
Utworz wspoldzielona biblioteke do wielokrotnego uzywania testow wydajnosci:
// 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// Zwroc wyniki testudef testResults = readJSON file: resultsFilereturn testResults}}}
Wskazowki dotyczace integracji z Jenkins
Obsluga timeout: Ustaw timeout dla dlugotrwalych testow wydajnosci:
stage('Performance Test') {options {timeout(time: 60, unit: 'MINUTES')}steps {// Kroki testu wydajnosci}}Warunkowe wykonanie: Uruchamiaj testy wydajnosci tylko na okreslonych galeziach:
stage('Performance Test') {when {anyOf {branch 'main'branch 'develop'tag pattern: "v\\d+\\.\\d+\\.\\d+", comparator: "REGEXP"}}steps {// Kroki testu wydajnosci}}Zaplanowane testowanie: Uruchamiaj testy wydajnosci wedlug harmonogramu:
pipeline {agent anytriggers {cron('0 0 * * *') // Uruchom o polnocy kazdego dnia}stages {// Etapy potoku}}Parametryzowane testy: Umozliw dostosowanie parametrow testow:
pipeline {agent anyparameters {string(name: 'TEST_NAME', defaultValue: 'API_Performance_Test', description: 'Nazwa testu LoadFocus do uruchomienia')string(name: 'THRESHOLDS', defaultValue: 'avgresponse<=200,errors==0', description: 'Progi wydajnosci')string(name: 'WAIT_TIMEOUT', defaultValue: '1800', description: 'Maksymalny czas oczekiwania w sekundach')}stages {stage('Performance Test') {steps {// Uruchom test z parametramish """loadfocus-api jmeter run-test \\--name "${params.TEST_NAME}" \\--thresholds "${params.THRESHOLDS}" \\--waitTimeout ${params.WAIT_TIMEOUT} \\--format json > performance_results.json"""}}}}
Aby uzyskac wiecej informacji, zapoznaj sie z dokumentacja Jenkins i dokumentacja klienta API LoadFocus.