GitLab CI/CD

Siame vadove paaiskiname, kaip integruoti LoadFocus JMeter API klienta su GitLab CI/CD automatizuotam nasumo testavimui.

Nustatymo zingsniai

1. Saugokite kredencialus kaip GitLab CI/CD kintamuosius

Pirmiausia saugokite savo LoadFocus API kredencialus kaip GitLab CI/CD kintamuosius:

  1. Eikite i savo GitLab projekta
  2. Naviguokite i Settings > CI/CD > Variables
  3. Pridekite siuos kintamuosius:
    • LOADFOCUS_API_KEY: jusu LoadFocus API raktas (pazymekite kaip "Masked")
    • LOADFOCUS_TEAM_ID: jusu LoadFocus komandos ID

2. Sukurkite GitLab CI/CD konvejeri

Sukurkite arba atnaujinkite savo .gitlab-ci.yml faila saugykloje:

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:
- 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
artifacts:
paths:
- performance_results.json
expire_in: 1 week
when: always
only:
- main
- develop
deploy:
stage: deploy
script:
- echo "Deploying application..."
only:
- main
when: on_success

3. Perziurekite testo rezultatus

Po konvejerio vykdymo:

  1. Eikite i savo GitLab projekta
  2. Naviguokite i CI/CD > Pipelines
  3. Raskite savo konvejeri ir spauskite ant jo
  4. Eikite i "performance_test" uzduoti
  5. Spauskite "Browse" desiniajame soniniame skydelyje, kad perziuretumete artefaktus
  6. Atsisiuskite ir perziurekite performance_results.json faila

Isplestine konfiguracija

Aplinkoms specifinis testavimas

Vykdykite skirtingus nasumo testus skirtingoms aplinkoms:

.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

Lygiagretus testavimas

Vykdykite kelis nasumo testus lygiagrecziai:

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

Patarimai GitLab CI/CD integracijai

  1. Podeliu naudojimas: Naudokite npm priklausomybiu podeli, kad pagreitintumete konvejerio vykdyma.

  2. Laiko limito nustatymai: Nustatykite laiko limitus ilgai trunkancziems nasumo testams:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    timeout: 2h
    script:
    # Nasumo testo skriptas...
  3. Rankinis paleidimas: Leiskite nasumo testus paleisti rankiniu budu:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Nasumo testo skriptas...
    when: manual
  4. Pranesimai: Siuskite pranesimus, kai nasumo testai nepavyksta:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Nasumo testo skriptas...
    after_script:
    - |
    if [ $? -ne 0 ]; then
    curl -X POST -H "Content-Type: application/json" \
    -d "{\"text\":\"Performance test failed for ${CI_PROJECT_NAME}\"}" \
    $WEBHOOK_URL
    fi

Daugiau informacijos rasite GitLab CI/CD dokumentacijoje ir LoadFocus API kliento dokumentacijoje.