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:

  1. Przejdz do Jenkins Dashboard > Manage Jenkins > Manage Credentials
  2. Wybierz odpowiednia domene poswiadczen (np. global)
  3. Kliknij "Add Credentials"
  4. Dodaj nastepujace poswiadczenia:
    • Kind: Secret text
    • Scope: Global
    • Secret: Twoj klucz API LoadFocus
    • ID: loadfocus-api-key
    • Description: LoadFocus API Key
  5. 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 budowania
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
steps {
// Twoje kroki testowania
sh 'npm test'
}
}
stage('Performance Test') {
steps {
// Zainstaluj klienta API JMeter LoadFocus
sh 'npm install -g @loadfocus/loadfocus-api-client'
// Skonfiguruj klienta API LoadFocus
sh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'
sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'
// Uruchom testy wydajnosci
sh '''
loadfocus-api jmeter run-test \
--name "Jenkins_${JOB_NAME}_${BUILD_NUMBER}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
'''
// Zarchiwizuj wyniki
archiveArtifacts artifacts: 'performance_results.json', fingerprint: true
}
}
stage('Deploy') {
when {
expression {
return currentBuild.resultIsBetterOrEqualTo('SUCCESS')
}
}
steps {
// Twoje kroki wdrozenia
echo 'Deploying...'
}
}
}
post {
always {
// Wyczysc przestrzen robocza
cleanWs()
}
}
}

3. Konfiguracja zadania Jenkins

  1. Utworz nowe zadanie Pipeline w Jenkins
  2. Skonfiguruj Pipeline do uzywania Twojego Jenkinsfile
  3. Skonfiguruj odpowiednie ustawienia SCM do pobierania repozytorium

Zaawansowana konfiguracja

Deklaratywny potok z testowaniem rownolegym

Uruchom wiele testow wydajnosci rownolegle:

pipeline {
agent any
environment {
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.groovy
def 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 ?: 1800
def 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 testu
def testResults = readJSON file: resultsFile
return testResults
}
}
}

Wskazowki dotyczace integracji z Jenkins

  1. Obsluga timeout: Ustaw timeout dla dlugotrwalych testow wydajnosci:

    stage('Performance Test') {
    options {
    timeout(time: 60, unit: 'MINUTES')
    }
    steps {
    // Kroki testu wydajnosci
    }
    }
  2. 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
    }
    }
  3. Zaplanowane testowanie: Uruchamiaj testy wydajnosci wedlug harmonogramu:

    pipeline {
    agent any
    triggers {
    cron('0 0 * * *') // Uruchom o polnocy kazdego dnia
    }
    stages {
    // Etapy potoku
    }
    }
  4. Parametryzowane testy: Umozliw dostosowanie parametrow testow:

    pipeline {
    agent any
    parameters {
    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 parametrami
    sh """
    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.