Jenkins

Ez az útmutató bemutatja, hogyan integrálhatja a LoadFocus JMeter API klienst a Jenkinsszel az automatizált teljesítményteszteléshez.

Beállítási lépések

1. Hitelesítő adatok tárolása a Jenkinsben

Először tárolja LoadFocus API hitelesítő adatait biztonságosan a Jenkinsben:

  1. Navigáljon a Jenkins Dashboard > Manage Jenkins > Manage Credentials menüpontba
  2. Válassza ki a megfelelő hitelesítő adat tartományt (pl. global)
  3. Kattintson az "Add Credentials" gombra
  4. Adja hozzá a következő hitelesítő adatokat:
    • Kind: Secret text
    • Scope: Global
    • Secret: Az Ön LoadFocus API kulcsa
    • ID: loadfocus-api-key
    • Description: LoadFocus API Key
  5. Ismételje meg a csapat azonosítóhoz az ID: loadfocus-team-id értékkel

2. Jenkins Pipeline létrehozása

Hozzon létre egy Jenkinsfile-t a tárhelyén:

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 steps
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
steps {
// Your test steps
sh 'npm test'
}
}
stage('Performance Test') {
steps {
// Install LoadFocus JMeter API Client
sh 'npm install -g @loadfocus/loadfocus-api-client'
// Configure LoadFocus API Client
sh 'loadfocus-api config set apikey $LOADFOCUS_API_KEY'
sh 'loadfocus-api config set teamid $LOADFOCUS_TEAM_ID'
// Run Performance Tests
sh '''
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 results
archiveArtifacts artifacts: 'performance_results.json', fingerprint: true
}
}
stage('Deploy') {
when {
expression {
return currentBuild.resultIsBetterOrEqualTo('SUCCESS')
}
}
steps {
// Your deployment steps
echo 'Deploying...'
}
}
}
post {
always {
// Clean up workspace
cleanWs()
}
}
}

3. Jenkins feladat konfigurálása

  1. Hozzon létre egy új Pipeline feladatot a Jenkinsben
  2. Konfigurálja a Pipeline-t a Jenkinsfile használatához
  3. Állítsa be a megfelelő SCM konfigurációt a tárhely lekéréséhez

Haladó konfiguráció

Deklaratív Pipeline párhuzamos teszteléssel

Több teljesítményteszt párhuzamos futtatásához:

pipeline {
agent any
environment {
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...
}
}

Szkriptelt Pipeline

Nagyobb rugalmasságért használjon szkriptelt pipeline-t:

node {
def performanceTestPassed = false
stage('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 JSON
def testResults = readJSON file: 'performance_results.json'
if (testResults.overallResult == 'PASSED') {
performanceTestPassed = true
echo "Performance test passed!"
} else {
echo "Performance test failed to meet thresholds!"
}
} 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'
}
}
}

Megosztott könyvtár

Hozzon létre megosztott könyvtárat az újrafelhasználható teljesítményteszteléshez:

// 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
def testResults = readJSON file: resultsFile
return testResults
}
}
}

Ezután a Jenkinsfile-ban:

@Library('my-shared-library') _
pipeline {
agent any
stages {
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"
}
}
}
}
}
}

Tippek a Jenkins integrációhoz

  1. Időtúllépés kezelés: Állítson be időtúllépéseket a hosszan futó teljesítménytesztekhez:

    stage('Performance Test') {
    options {
    timeout(time: 60, unit: 'MINUTES')
    }
    steps {
    // Performance test steps
    }
    }
  2. Feltételes futtatás: Csak bizonyos ágakon futtasson teljesítményteszteket:

    stage('Performance Test') {
    when {
    anyOf {
    branch 'main'
    branch 'develop'
    tag pattern: "v\\d+\\.\\d+\\.\\d+", comparator: "REGEXP"
    }
    }
    steps {
    // Performance test steps
    }
    }
  3. Ütemezett tesztelés: Futtasson teljesítményteszteket ütemezetten:

    pipeline {
    agent any
    triggers {
    cron('0 0 * * *') // Run at midnight every day
    }
    stages {
    // Pipeline stages
    }
    }
  4. Paraméteres tesztek: Tegye lehetővé a teszt paraméterek testreszabását:

    pipeline {
    agent any
    parameters {
    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 {
    sh """
    loadfocus-api jmeter run-test \\
    --name "${params.TEST_NAME}" \\
    --thresholds "${params.THRESHOLDS}" \\
    --waitTimeout ${params.WAIT_TIMEOUT} \\
    --format json > performance_results.json
    """
    }
    }
    }
    }

További információkért tekintse meg a Jenkins dokumentációt és a LoadFocus API kliens dokumentációt.