Zum Inhalt springen

Diese Anleitung zeigt, wie Sie einen LoadFocus-JMeter-Test aus einem GitHub-Actions-Workflow starten, auf sein Ende warten und den Workflow fehlschlagen lassen, wenn ein Schwellenwert verfehlt wird. Sie nutzt den LoadFocus API Client (loadfocus-api, über npm installiert). Legen Sie den Test zuerst in LoadFocus an und speichern Sie ihn, siehe So führen Sie einen neuen Apache JMeter-Lasttest durch; der Workflow startet diesen gespeicherten Test über seinen Namen.

Speichern Sie zunächst Ihre LoadFocus API-Zugangsdaten als GitHub-Repository-Secrets:

  1. Gehen Sie zu Ihrem GitHub-Repository
  2. Navigieren Sie zu Settings > Secrets and variables > Actions
  3. Fügen Sie folgende Repository-Secrets hinzu:
    • LOADFOCUS_API_KEY: Ihr LoadFocus API-Schlüssel
    • LOADFOCUS_TEAM_ID: Ihre LoadFocus Team-ID

Beides finden Sie auf der Seite API-Schlüssel in den Einstellungen Ihres LoadFocus-Kontos.

Erstellen Sie eine neue Datei in Ihrem Repository unter .github/workflows/performance-test.yml:

name: Performance Tests
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
# Optional: Run on a schedule
schedule:
- cron: '0 0 * * 1' # Run at midnight every Monday
jobs:
performance-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install LoadFocus JMeter API Client
run: npm install -g @loadfocus/loadfocus-api-client
- name: Configure LoadFocus API Client
run: |
loadfocus-api config set --apikey ${{ secrets.LOADFOCUS_API_KEY }} --teamid ${{ secrets.LOADFOCUS_TEAM_ID }}
- name: Run Performance Tests
run: |
# --name ist der exakte Name eines in LoadFocus gespeicherten Tests
loadfocus-api jmeter run-test \
--name "My_JMeter_Test" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
- name: Upload Performance Test Results
if: always()
uses: actions/upload-artifact@v4
with:
name: performance-test-results
path: performance_results.json

run-test startet den gespeicherten Test, wartet auf sein Ende und prüft die Schwellenwerte. Wird ein Schwellenwert verfehlt oder schlägt der Lauf fehl, beendet sich der Befehl mit Code 1 und der Schritt schlägt fehl. Schwellenwerte werden für jedes Request-Label einzeln geprüft, ein langsamer Request genügt also, damit der Lauf fehlschlägt. errors ist eine Anzahl fehlgeschlagener Requests, keine Prozentzahl. Ersetzen Sie in den folgenden Beispielen die Testnamen durch den exakten Namen Ihres gespeicherten Tests.

Die Ausgabedatei beginnt mit einigen Fortschrittszeilen vor dem JSON-Objekt. Für eine saubere JSON-Datei behalten Sie nur die Zeilen ab der ersten Zeile, die nur { enthält: sed -n '/^{$/,$p' performance_results.json > results.json.

3. Leistungstests zum Deployment-Workflow hinzufügen

Abschnitt betitelt „3. Leistungstests zum Deployment-Workflow hinzufügen“

Um das Deployment von den Leistungstestergebnissen abhängig zu machen:

name: Build, Test, and Deploy
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
# Your build steps...
performance-test:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install LoadFocus JMeter API Client
run: npm install -g @loadfocus/loadfocus-api-client
- name: Configure LoadFocus API Client
run: |
loadfocus-api config set --apikey ${{ secrets.LOADFOCUS_API_KEY }} --teamid ${{ secrets.LOADFOCUS_TEAM_ID }}
- name: Run Performance Tests
run: |
loadfocus-api jmeter run-test \
--name "My_JMeter_Test" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
- name: Upload Performance Test Results
if: always()
uses: actions/upload-artifact@v4
with:
name: performance-test-results
path: performance_results.json
deploy:
needs: performance-test
runs-on: ubuntu-latest
steps:
# Your deployment steps...

Tests gegen mehrere Umgebungen oder Konfigurationen ausführen:

jobs:
performance-test:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [dev, staging, production]
test-type: [api, frontend]
steps:
# Setup steps...
- name: Run Performance Tests
run: |
TEST_NAME="${{ matrix.test-type }}_test_${{ matrix.environment }}"
# Adjust thresholds based on environment
if [ "${{ matrix.environment }}" == "production" ]; then
THRESHOLDS="avgresponse<=150,errors==0,p95<=200"
else
THRESHOLDS="avgresponse<=300,errors==0,p95<=500"
fi
loadfocus-api jmeter run-test \
--name "$TEST_NAME" \
--thresholds "$THRESHOLDS" \
--format json > "performance_results_${{ matrix.environment }}_${{ matrix.test-type }}.json"

Leistungstestergebnisse als Kommentar zu Pull Requests hinzufügen:

- name: Comment PR
uses: actions/github-script@v6
if: always() && github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const raw = fs.readFileSync('performance_results.json', 'utf8');
// the file starts with progress lines; the JSON begins at the first line that is only {
const results = JSON.parse(raw.slice(raw.search(/^\{$/m)));
let comment = '## Performance Test Results\n\n';
comment += `**Overall Result:** ${results.overallResult}\n\n`;
comment += '### Results by Label\n\n';
for (const label of results.labels) {
comment += `#### ${label.label}\n`;
comment += `- **Result:** ${label.result}\n`;
comment += `- **Samples:** ${label.metrics.samples}\n`;
comment += `- **Avg Response:** ${label.metrics.avgresponse}ms\n`;
comment += `- **Error Rate:** ${label.metrics.errors}\n\n`;
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});

Erstellen Sie einen wiederverwendbaren Workflow für Leistungstests:

.github/workflows/reusable-performance-test.yml
name: Reusable Performance Test
on:
workflow_call:
inputs:
test-name:
required: true
type: string
thresholds:
required: false
type: string
default: "avgresponse<=200,errors==0,p95<=250"
wait-timeout:
required: false
type: number
default: 1800
secrets:
LOADFOCUS_API_KEY:
required: true
LOADFOCUS_TEAM_ID:
required: true
jobs:
performance-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install LoadFocus JMeter API Client
run: npm install -g @loadfocus/loadfocus-api-client
- name: Configure LoadFocus API Client
run: |
loadfocus-api config set --apikey ${{ secrets.LOADFOCUS_API_KEY }} --teamid ${{ secrets.LOADFOCUS_TEAM_ID }}
- name: Run Performance Tests
run: |
loadfocus-api jmeter run-test \
--name "${{ inputs.test-name }}" \
--thresholds "${{ inputs.thresholds }}" \
--wait-timeout ${{ inputs.wait-timeout }} \
--format json > performance_results.json
- name: Upload Performance Test Results
if: always()
uses: actions/upload-artifact@v4
with:
name: performance-test-results
path: performance_results.json

Dann aus einem anderen Workflow aufrufen:

.github/workflows/main.yml
jobs:
call-performance-test:
uses: ./.github/workflows/reusable-performance-test.yml
with:
test-name: "API_Performance_Test"
thresholds: "avgresponse<=150,errors==0"
secrets:
LOADFOCUS_API_KEY: ${{ secrets.LOADFOCUS_API_KEY }}
LOADFOCUS_TEAM_ID: ${{ secrets.LOADFOCUS_TEAM_ID }}
  1. Caching: npm-Abhängigkeiten cachen, um Workflow-Läufe zu beschleunigen:

    - name: Cache Node modules
    uses: actions/cache@v3
    with:
    path: ~/.npm
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
    ${{ runner.os }}-node-
  2. Parallelitätskontrolle: Gleichzeitige Leistungstests begrenzen:

    concurrency:
    group: performance-test-${{ github.ref }}
    cancel-in-progress: false
  3. Umgebungsspezifische Tests: GitHub-Environments für verschiedene Testkonfigurationen verwenden:

    jobs:
    performance-test:
    runs-on: ubuntu-latest
    environment: staging
    # Environment-specific variables are available here
  4. Bedingte Tests: Leistungstests nur ausführen, wenn bestimmte Dateien geändert werden:

    jobs:
    performance-test:
    if: |
    contains(github.event.pull_request.labels.*.name, 'performance-test') ||
    github.event_name == 'schedule' ||
    contains(github.event.head_commit.message, '[perf-test]')

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