GitHub Actions
Ten przewodnik wyjasnia, jak zintegrowac klienta API JMeter LoadFocus z GitHub Actions w celu automatycznego testowania wydajnosci.
Kroki konfiguracji
1. Przechowywanie danych uwierzytelniajacych jako sekretow GitHub
Najpierw przechowuj dane uwierzytelniajace API LoadFocus jako sekrety repozytorium GitHub:
- Przejdz do swojego repozytorium GitHub
- Przejdz do Settings > Secrets and variables > Actions
- Dodaj nastepujace sekrety repozytorium:
LOADFOCUS_API_KEY: Twoj klucz API LoadFocusLOADFOCUS_TEAM_ID: ID Twojego zespolu LoadFocus
2. Utworz workflow GitHub Actions
Utworz nowy plik w repozytorium w lokalizacji .github/workflows/performance-test.yml:
name: Performance Testson:push:branches: [ main, develop ]pull_request:branches: [ main ]schedule:- cron: '0 0 * * 1' # Uruchom o polnocy kazdego poniedzialkujobs:performance-test:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v3- name: Setup Node.jsuses: actions/setup-node@v3with:node-version: '16'- name: Install LoadFocus JMeter API Clientrun: npm install -g @loadfocus/loadfocus-api-client- name: Configure LoadFocus API Clientrun: |loadfocus-api config set apikey ${{ secrets.LOADFOCUS_API_KEY }}loadfocus-api config set teamid ${{ secrets.LOADFOCUS_TEAM_ID }}- name: Run Performance Testsrun: |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 Resultsuses: actions/upload-artifact@v3with:name: performance-test-resultspath: performance_results.json
3. Dodaj testowanie wydajnosci do workflow wdrozeniowego
Aby wdrozenie zaleลผalo od wynikow testow wydajnosci:
name: Build, Test, and Deployon:push:branches: [ main ]jobs:build:runs-on: ubuntu-lateststeps:# Twoje kroki budowania...performance-test:needs: buildruns-on: ubuntu-lateststeps:- uses: actions/checkout@v3- name: Setup Node.jsuses: actions/setup-node@v3with:node-version: '16'- name: Install LoadFocus JMeter API Clientrun: npm install -g @loadfocus/loadfocus-api-client- name: Configure LoadFocus API Clientrun: |loadfocus-api config set apikey ${{ secrets.LOADFOCUS_API_KEY }}loadfocus-api config set teamid ${{ secrets.LOADFOCUS_TEAM_ID }}- name: Run Performance Testsrun: |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 Resultsuses: actions/upload-artifact@v3with:name: performance-test-resultspath: performance_results.jsondeploy:needs: performance-testruns-on: ubuntu-lateststeps:# Twoje kroki wdrozenia...
Zaawansowana konfiguracja
Testowanie macierzowe dla wielu srodowisk
Uruchamiaj testy dla wielu srodowisk lub konfiguracji:
jobs:performance-test:runs-on: ubuntu-lateststrategy:matrix:environment: [dev, staging, production]test-type: [api, frontend]steps:# Kroki konfiguracji...- name: Run Performance Testsrun: |TEST_NAME="${{ matrix.test-type }}_test_${{ matrix.environment }}"# Dostosuj progi na podstawie srodowiskaif [ "${{ matrix.environment }}" == "production" ]; thenTHRESHOLDS="avgresponse<=150,errors==0,p95<=200"elseTHRESHOLDS="avgresponse<=300,errors==0,p95<=500"filoadfocus-api jmeter run-test \--name "$TEST_NAME" \--thresholds "$THRESHOLDS" \--format json > "performance_results_${{ matrix.environment }}_${{ matrix.test-type }}.json"
Komentowanie wynikow testow w PR
Dodaj wyniki testow wydajnosci jako komentarz do pull requestow:
- name: Comment PRuses: actions/github-script@v6if: 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 = '## Wyniki testow wydajnosci\n\n';comment += `**Ogolny wynik:** ${results.overallResult}\n\n`;comment += '### Wyniki wedlug etykiet\n\n';for (const label of results.labels) {comment += `#### ${label.label}\n`;comment += `- **Wynik:** ${label.result}\n`;comment += `- **Probki:** ${label.metrics.samples}\n`;comment += `- **Srednia odpowiedz:** ${label.metrics.avgresponse}ms\n`;comment += `- **Wskaznik bledow:** ${label.metrics.errors}\n\n`;}github.rest.issues.createComment({issue_number: context.issue.number,owner: context.repo.owner,repo: context.repo.repo,body: comment});
Wskazowki dotyczace integracji z GitHub Actions
Buforowanie: Buforuj zaleznosci npm, aby przyspieszyc uruchamianie workflow:
- name: Cache Node modulesuses: actions/cache@v3with:path: ~/.npmkey: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}restore-keys: |${{ runner.os }}-node-Kontrola wspolbieznosci: Ogranicz rownolegle testy wydajnosci:
concurrency:group: performance-test-${{ github.ref }}cancel-in-progress: falseTesty specyficzne dla srodowiska: Uzyj srodowisk GitHub dla roznych konfiguracji testow:
jobs:performance-test:runs-on: ubuntu-latestenvironment: staging# Zmienne specyficzne dla srodowiska sa tutaj dostepneTestowanie warunkowe: Uruchamiaj testy wydajnosci tylko gdy zmienia sie okreslone pliki:
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]')
Aby uzyskac wiecej informacji, zapoznaj sie z dokumentacja GitHub Actions i dokumentacja klienta API LoadFocus.