GitLab CI/CD

Ez az útmutató bemutatja, hogyan integrálhatja a LoadFocus JMeter API klienst a GitLab CI/CD-vel az automatizált teljesítményteszteléshez.

Beállítási lépések

1. Hitelesítő adatok tárolása GitLab CI/CD változókként

Először tárolja LoadFocus API hitelesítő adatait GitLab CI/CD változókként:

  1. Navigáljon a GitLab projektjéhez
  2. Lépjen a Settings > CI/CD > Variables menüpontba
  3. Adja hozzá a következő változókat:
    • LOADFOCUS_API_KEY: Az Ön LoadFocus API kulcsa (jelölje "Masked"-ként)
    • LOADFOCUS_TEAM_ID: Az Ön LoadFocus csapat azonosítója

2. GitLab CI/CD Pipeline létrehozása

Hozza létre vagy frissítse a .gitlab-ci.yml fájlt a tárhelyén:

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. Teszteredmények megtekintése

A pipeline futtatása után:

  1. Navigáljon a GitLab projektjéhez
  2. Lépjen a CI/CD > Pipelines menüpontba
  3. Keresse meg a pipeline-ját és kattintson rá
  4. Navigáljon a "performance_test" feladathoz
  5. Kattintson az oldalsávon a "Browse" opcióra az artifaktumok megtekintéséhez
  6. Töltse le és tekintse meg a performance_results.json fájlt

Haladó konfiguráció

Környezet-specifikus tesztelés

Különböző teljesítménytesztek futtatása különböző környezetekhez:

.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

Párhuzamos tesztelés

Több teljesítményteszt párhuzamos futtatása:

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

Tippek a GitLab CI/CD integrációhoz

  1. Gyorsítótárazás: Gyorsítótárazza az npm függőségeket a pipeline futtatások felgyorsításához:

    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. Időtúllépés beállítások: Állítson be időtúllépéseket a hosszan futó teljesítménytesztekhez:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    timeout: 2h # Set a 2-hour timeout
    script:
    # Performance test script...
  3. Kézi indítás: Tegye lehetővé a teljesítménytesztek kézi indítását:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Performance test script...
    when: manual
  4. Dinamikus teszt konfiguráció: Használjon GitLab előre meghatározott változókat a tesztek dinamikus konfigurálásához:

    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. Értesítések: Küldjön értesítéseket, ha a teljesítménytesztek meghiúsulnak:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Performance test script...
    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

További információkért tekintse meg a GitLab CI/CD dokumentációt és a LoadFocus API kliens dokumentációt.