GitLab CI/CD

Tento průvodce vysvětluje, jak integrovat klienta LoadFocus JMeter API s GitLab CI/CD pro automatizované výkonnostní testování.

Kroky nastavení

1. Uložení přihlašovacích údajů jako proměnných GitLab CI/CD

Nejprve uložte vaše LoadFocus API přihlašovací údaje jako proměnné GitLab CI/CD:

  1. Přejděte do vašeho GitLab projektu
  2. Přejděte na Settings > CI/CD > Variables
  3. Přidejte následující proměnné:
    • LOADFOCUS_API_KEY: Váš LoadFocus API klíč (označte jako "Masked")
    • LOADFOCUS_TEAM_ID: Vaše LoadFocus team ID

2. Vytvoření GitLab CI/CD pipeline

Vytvořte nebo aktualizujte váš soubor .gitlab-ci.yml ve vašem repozitáři:

stages:
- build
- test
- performance
- deploy
variables:
NODE_VERSION: "16"
build:
stage: build
image: node:${NODE_VERSION}
script:
- npm install
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 week
test:
stage: test
image: node:${NODE_VERSION}
script:
- npm install
- npm test
performance_test:
stage: performance
image: node:${NODE_VERSION}
script:
# Install LoadFocus JMeter API Client
- npm install -g @loadfocus/loadfocus-api-client
# Configure LoadFocus API Client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
# Run Performance Tests
- |
loadfocus-api jmeter run-test \
--name "GitLab_${CI_PROJECT_NAME}_${CI_COMMIT_REF_NAME}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
artifacts:
paths:
- performance_results.json
expire_in: 1 week
when: always
# Optional: Only run on specific branches
only:
- main
- develop
deploy:
stage: deploy
script:
- echo "Deploying application..."
only:
- main
# Only deploy if all previous stages succeeded
when: on_success

3. Zobrazení výsledků testů

Po spuštění pipeline:

  1. Přejděte do vašeho GitLab projektu
  2. Přejděte na CI/CD > Pipelines
  3. Najděte váš pipeline a klikněte na něj
  4. Přejděte na úlohu "performance_test"
  5. Klikněte na "Browse" v pravém postranním panelu pro zobrazení artefaktů
  6. Stáhněte a zobrazte soubor performance_results.json

Pokročilá konfigurace

Testování specifické pro prostředí

Spusťte různé výkonnostní testy pro různá prostředí:

.performance_test_template: &performance_test_definition
stage: performance
image: node:${NODE_VERSION}
script:
- npm install -g @loadfocus/loadfocus-api-client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
- |
loadfocus-api jmeter run-test \
--name "${TEST_NAME}" \
--thresholds "${THRESHOLDS}" \
--format json > performance_results.json
artifacts:
paths:
- performance_results.json
expire_in: 1 week
when: always
performance_test_develop:
<<: *performance_test_definition
variables:
TEST_NAME: "API_Test_Develop"
THRESHOLDS: "avgresponse<=300,errors==0,p95<=500"
only:
- develop
performance_test_staging:
<<: *performance_test_definition
variables:
TEST_NAME: "API_Test_Staging"
THRESHOLDS: "avgresponse<=250,errors==0,p95<=350"
only:
- staging
performance_test_production:
<<: *performance_test_definition
variables:
TEST_NAME: "API_Test_Production"
THRESHOLDS: "avgresponse<=200,errors==0,p95<=250"
only:
- main

Paralelní testování

Spusťte více výkonnostních testů paralelně:

performance_test_api:
stage: performance
image: node:${NODE_VERSION}
script:
- npm install -g @loadfocus/loadfocus-api-client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
- |
loadfocus-api jmeter run-test \
--name "API_Performance_Test" \
--thresholds "avgresponse<=150,errors==0" \
--format json > api_performance_results.json
artifacts:
paths:
- api_performance_results.json
expire_in: 1 week
performance_test_ui:
stage: performance
image: node:${NODE_VERSION}
script:
- npm install -g @loadfocus/loadfocus-api-client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
- |
loadfocus-api jmeter run-test \
--name "UI_Performance_Test" \
--thresholds "avgresponse<=300,errors==0" \
--format json > ui_performance_results.json
artifacts:
paths:
- ui_performance_results.json
expire_in: 1 week

Poté vytvořte plán pipeline v GitLab:

  1. Přejděte do vašeho GitLab projektu
  2. Přejděte na CI/CD > Schedules
  3. Klikněte na "New schedule"
  4. Nastavte rozvrh (např. každý den o půlnoci)

Vytváření výkonnostních reportů

Generujte HTML reporty z JSON výsledků:

performance_test:
stage: performance
image: node:${NODE_VERSION}
script:
# Run performance test
- npm install -g @loadfocus/loadfocus-api-client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
- |
loadfocus-api jmeter run-test \
--name "GitLab_${CI_PROJECT_NAME}_${CI_COMMIT_REF_NAME}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
# Generate HTML report
- npm install -g performance-report-generator # Replace with actual report generator
- performance-report-generator --input performance_results.json --output performance_report.html
artifacts:
paths:
- performance_results.json
- performance_report.html
expire_in: 1 week
when: always
# Optional: Publish report as GitLab Pages
pages:
stage: deploy
dependencies:
- performance_test
script:
- mkdir -p public/performance-reports
- cp performance_report.html public/performance-reports/index.html
artifacts:
paths:
- public
only:
- main

Integrace s funkcemi GitLab

Widgety v Merge Requestech

Zobrazte výsledky výkonnostních testů v merge requestech pomocí funkce JUnit reportů v GitLab:

performance_test:
stage: performance
image: node:${NODE_VERSION}
script:
# Run performance test
- npm install -g @loadfocus/loadfocus-api-client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
- |
loadfocus-api jmeter run-test \
--name "GitLab_${CI_PROJECT_NAME}_${CI_COMMIT_REF_NAME}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
# Convert to JUnit format (using a custom script)
- node convert-to-junit.js performance_results.json junit-report.xml
artifacts:
reports:
junit: junit-report.xml
paths:
- performance_results.json
expire_in: 1 week

GitLab metriky

Použijte funkci metrik v GitLab pro sledování výkonu v průběhu času:

performance_test:
stage: performance
image: node:${NODE_VERSION}
script:
# Run performance test
- npm install -g @loadfocus/loadfocus-api-client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
- |
loadfocus-api jmeter run-test \
--name "GitLab_${CI_PROJECT_NAME}_${CI_COMMIT_REF_NAME}" \
--thresholds "avgresponse<=200,errors==0,p95<=250" \
--format json > performance_results.json
# Extract metrics and report them to GitLab
- |
# Parse JSON and extract metrics
RESPONSE_TIME=$(jq '.labels[0].metrics.avgresponse' performance_results.json)
ERROR_RATE=$(jq '.labels[0].metrics.errors' performance_results.json)
# Report metrics
echo "performance_avg_response_time ${RESPONSE_TIME}" >> metrics.txt
echo "performance_error_rate ${ERROR_RATE}" >> metrics.txt
artifacts:
reports:
metrics: metrics.txt
paths:
- performance_results.json
expire_in: 1 week

GitLab prostředí

Přiřaďte výkonnostní testy ke konkrétním prostředím:

performance_test_staging:
stage: performance
image: node:${NODE_VERSION}
environment:
name: staging
script:
- npm install -g @loadfocus/loadfocus-api-client
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
- |
loadfocus-api jmeter run-test \
--name "Staging_Performance_Test" \
--thresholds "avgresponse<=250,errors==0" \
--format json > performance_results.json
artifacts:
paths:
- performance_results.json
expire_in: 1 week
only:
- staging

Tipy pro integraci s GitLab CI/CD

  1. Cachování: Cachujte npm závislosti pro zrychlení běhů pipeline:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
    - node_modules/
    script:
    - npm install -g @loadfocus/loadfocus-api-client
    # Rest of the script...
  2. Nastavení timeoutu: Nastavte timeouty pro dlouhotrvající výkonnostní testy:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    timeout: 2h # Set a 2-hour timeout
    script:
    # Performance test script...
  3. Ruční spouštění: Umožněte ruční spouštění výkonnostních testů:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Performance test script...
    when: manual
  4. Dynamická konfigurace testů: Použijte předdefinované proměnné GitLab pro dynamickou konfiguraci testů:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    - |
    # Set thresholds based on branch
    if [ "$CI_COMMIT_REF_NAME" = "main" ]; then
    THRESHOLDS="avgresponse<=200,errors==0,p95<=250"
    else
    THRESHOLDS="avgresponse<=300,errors==0,p95<=500"
    fi
    loadfocus-api jmeter run-test \
    --name "GitLab_${CI_PROJECT_NAME}_${CI_COMMIT_REF_NAME}" \
    --thresholds "$THRESHOLDS" \
    --format json > performance_results.json
  5. Notifikace: Odesílejte notifikace při selhání výkonnostních testů:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Performance test script...
    after_script:
    - |
    if [ $? -ne 0 ]; then
    # Send notification using curl, GitLab API, or other method
    curl -X POST -H "Content-Type: application/json" \
    -d "{\"text\":\"Performance test failed for ${CI_PROJECT_NAME}\"}" \
    $WEBHOOK_URL
    fi

Pro více informací se podívejte na dokumentaci GitLab CI/CD a dokumentaci LoadFocus API klienta.