GitHub Actions

Denna guide förklarar hur du integrerar LoadFocus JMeter API-klienten med GitHub Actions för automatiserad prestandatestning.

Installationssteg

1. Lagra autentiseringsuppgifter som GitHub Secrets

Lagra först dina LoadFocus API-autentiseringsuppgifter som GitHub repository secrets:

  1. Gå till ditt GitHub-repository
  2. Navigera till Settings > Secrets and variables > Actions
  3. Lägg till följande repository secrets:
    • LOADFOCUS_API_KEY: Din LoadFocus API-nyckel
    • LOADFOCUS_TEAM_ID: Ditt LoadFocus team-ID

2. Skapa ett GitHub Actions-arbetsflöde

Skapa en ny fil i ditt repository på .github/workflows/performance-test.yml:

name: Performance Tests
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
# Valfritt: Kör enligt schema
schedule:
- cron: '0 0 * * 1' # Kör vid midnatt varje måndag
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 }}
loadfocus-api config set teamid ${{ secrets.LOADFOCUS_TEAM_ID }}
- name: Run Performance Tests
run: |
loadfocus-api jmeter run-test \
--name "GitHub_${{ github.repository_owner }}_${{ github.repository }}_${{ github.ref_name }}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
- name: Upload Performance Test Results
uses: actions/upload-artifact@v3
with:
name: performance-test-results
path: performance_results.json

3. Lägg till prestandatestning i ditt deployment-arbetsflöde

För att göra deployment beroende av prestandatestresultat:

name: Build, Test, and Deploy
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
# Dina byggsteg...
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 }}
loadfocus-api config set teamid ${{ secrets.LOADFOCUS_TEAM_ID }}
- name: Run Performance Tests
run: |
loadfocus-api jmeter run-test \
--name "GitHub_${{ github.repository }}_${{ github.ref_name }}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
- name: Upload Performance Test Results
uses: actions/upload-artifact@v3
with:
name: performance-test-results
path: performance_results.json
deploy:
needs: performance-test
runs-on: ubuntu-latest
steps:
# Dina deployment-steg...

Avancerad konfiguration

Matristestning för flera miljöer

Kör tester mot flera miljöer eller konfigurationer:

jobs:
performance-test:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [dev, staging, production]
test-type: [api, frontend]
steps:
# Installationssteg...
- name: Run Performance Tests
run: |
TEST_NAME="${{ matrix.test-type }}_test_${{ matrix.environment }}"
# Justera tröskelvärden baserat på miljö
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"

Skapa prestandatestrapporter

Generera HTML-rapporter från JSON-resultat:

- name: Generate HTML Report
run: |
# Installera rapportgenerator
npm install -g performance-report-generator
# Generera HTML-rapport
performance-report-generator \
--input performance_results.json \
--output performance_report.html
- name: Upload HTML Report
uses: actions/upload-artifact@v3
with:
name: performance-test-report
path: performance_report.html
# Valfritt: Publicera till GitHub Pages
- name: Publish to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./reports
destination_dir: performance-reports

Kommentera testresultat på PR:er

Lägg till prestandatestresultat som en kommentar på pull requests:

- name: Comment PR
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const results = JSON.parse(fs.readFileSync('performance_results.json', 'utf8'));
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
});

Återanvändbart arbetsflöde

Skapa ett återanvändbart arbetsflöde för prestandatestning:

# .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 }}
loadfocus-api config set teamid ${{ secrets.LOADFOCUS_TEAM_ID }}
- name: Run Performance Tests
run: |
loadfocus-api jmeter run-test \
--name "${{ inputs.test-name }}" \
--thresholds "${{ inputs.thresholds }}" \
--waitTimeout ${{ inputs.wait-timeout }} \
--format json > performance_results.json
- name: Upload Performance Test Results
uses: actions/upload-artifact@v3
with:
name: performance-test-results
path: performance_results.json

Anropa det sedan från ett annat arbetsflöde:

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

Tips för GitHub Actions-integration

  1. Cachning: Cacha npm-beroenden för att snabba upp arbetsflödeskörningar:

    - 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. Samtidighetskontroll: Begränsa samtidiga prestandatester:

    concurrency:
    group: performance-test-${{ github.ref }}
    cancel-in-progress: false
  3. Miljöspecifika tester: Använd GitHub-miljöer för olika testkonfigurationer:

    jobs:
    performance-test:
    runs-on: ubuntu-latest
    environment: staging
    # Miljöspecifika variabler är tillgängliga här
  4. Villkorlig testning: Kör bara prestandatester när specifika filer ändras:

    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]')

För mer information, se GitHub Actions-dokumentationen och LoadFocus API-klientdokumentationen.