Jenkins

Diese Anleitung erklaert, wie Sie den LoadFocus JMeter API Client mit Jenkins fuer automatisierte Leistungstests integrieren.

Einrichtungsschritte

1. Anmeldedaten in Jenkins speichern

Speichern Sie zunaechst Ihre LoadFocus API-Zugangsdaten sicher in Jenkins:

  1. Navigieren Sie zu Jenkins Dashboard > Manage Jenkins > Manage Credentials
  2. Waehlen Sie die entsprechende Credential-Domain (z. B. global)
  3. Klicken Sie auf "Add Credentials"
  4. Fuegen Sie folgende Credentials hinzu:
    • Kind: Secret text
    • Scope: Global
    • Secret: Ihr LoadFocus API-Schluessel
    • ID: loadfocus-api-key
    • Description: LoadFocus API Key
  5. Wiederholen Sie dies fuer Ihre Team-ID mit ID: loadfocus-team-id

2. Jenkins-Pipeline erstellen

Erstellen Sie eine Jenkinsfile in Ihrem Repository:

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-Job konfigurieren

  1. Erstellen Sie einen neuen Pipeline-Job in Jenkins
  2. Konfigurieren Sie die Pipeline zur Verwendung Ihrer Jenkinsfile
  3. Richten Sie die entsprechende SCM-Konfiguration ein, um Ihr Repository abzurufen

Erweiterte Konfiguration

Deklarative Pipeline mit parallelen Tests

Mehrere Leistungstests parallel ausfuehren:

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...
}
}

Tipps fuer die Jenkins-Integration

  1. Timeout-Behandlung: Timeouts fuer lang laufende Leistungstests festlegen:

    stage('Performance Test') {
    options {
    timeout(time: 60, unit: 'MINUTES')
    }
    steps {
    // Performance test steps
    }
    }
  2. Bedingte Ausfuehrung: Leistungstests nur auf bestimmten Branches ausfuehren:

    stage('Performance Test') {
    when {
    anyOf {
    branch 'main'
    branch 'develop'
    tag pattern: "v\\d+\\.\\d+\\.\\d+", comparator: "REGEXP"
    }
    }
    steps {
    // Performance test steps
    }
    }
  3. Geplante Tests: Leistungstests nach Zeitplan ausfuehren:

    pipeline {
    agent any
    triggers {
    cron('0 0 * * *') // Run at midnight every day
    }
    stages {
    // Pipeline stages
    }
    }
  4. Parametrisierte Tests: Anpassung von Testparametern ermoeglichen:

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

Weitere Informationen finden Sie in der Jenkins-Dokumentation und der LoadFocus API Client-Dokumentation.