GitLab CI/CD

Ten przewodnik wyjasnia, jak zintegrowac klienta API JMeter LoadFocus z GitLab CI/CD w celu automatycznego testowania wydajnosci.

Kroki konfiguracji

1. Przechowywanie danych uwierzytelniajacych jako zmiennych GitLab CI/CD

Najpierw przechowuj dane uwierzytelniajace API LoadFocus jako zmienne GitLab CI/CD:

  1. Przejdz do swojego projektu GitLab
  2. Przejdz do Settings > CI/CD > Variables
  3. Dodaj nastepujace zmienne:
    • LOADFOCUS_API_KEY: Twoj klucz API LoadFocus (oznacz jako "Masked")
    • LOADFOCUS_TEAM_ID: ID Twojego zespolu LoadFocus

2. Utworz potok GitLab CI/CD

Utworz lub zaktualizuj plik .gitlab-ci.yml w swoim repozytorium:

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:
# Zainstaluj klienta API JMeter LoadFocus
- npm install -g @loadfocus/loadfocus-api-client
# Skonfiguruj klienta API LoadFocus
- loadfocus-api config set apikey $LOADFOCUS_API_KEY
- loadfocus-api config set teamid $LOADFOCUS_TEAM_ID
# Uruchom testy wydajnosci
- |
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
# Opcjonalnie: Uruchom tylko na okreslonych galeziach
only:
- main
- develop
deploy:
stage: deploy
script:
- echo "Deploying application..."
only:
- main
# Wdrazaj tylko jesli wszystkie poprzednie etapy powiodly sie
when: on_success

3. Przegladanie wynikow testow

Po uruchomieniu potoku:

  1. Przejdz do swojego projektu GitLab
  2. Przejdz do CI/CD > Pipelines
  3. Znajdz swoj potok i kliknij go
  4. Przejdz do zadania "performance_test"
  5. Kliknij "Browse" w prawym panelu bocznym, aby wyswietlic artefakty
  6. Pobierz i przejrzyj plik performance_results.json

Zaawansowana konfiguracja

Testowanie specyficzne dla srodowiska

Uruchamiaj rozne testy wydajnosci dla roznych srodowisk:

.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

Testowanie rownolegle

Uruchom wiele testow wydajnosci rownolegle:

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

Wskazowki dotyczace integracji z GitLab CI/CD

  1. Buforowanie: Buforuj zaleznosci npm, aby przyspieszyc uruchamianie potoku:

    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
    # Reszta skryptu...
  2. Ustawienia timeout: Ustaw timeout dla dlugotrwalych testow wydajnosci:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    timeout: 2h # Ustaw timeout na 2 godziny
    script:
    # Skrypt testu wydajnosci...
  3. Reczne wyzwalanie: Pozwol na reczne wyzwalanie testow wydajnosci:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Skrypt testu wydajnosci...
    when: manual
  4. Dynamiczna konfiguracja testow: Uzyj predefiniowanych zmiennych GitLab do dynamicznej konfiguracji testow:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    - |
    # Ustaw progi na podstawie galezi
    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

Aby uzyskac wiecej informacji, zapoznaj sie z dokumentacja GitLab CI/CD i dokumentacja klienta API LoadFocus.