GitLab CI/CD

Tämä opas selittää kuinka integroida LoadFocus JMeter API Client GitLab CI/CD:n kanssa automatisoitua suorituskykytestausta varten.

Asennusvaiheet

1. Tallenna tunnistetiedot GitLab CI/CD -muuttujina

Tallenna ensin LoadFocus API -tunnistetietosi GitLab CI/CD -muuttujina:

  1. Siirry GitLab-projektiisi
  2. Siirry kohtaan Settings > CI/CD > Variables
  3. Lisää seuraavat muuttujat:
    • LOADFOCUS_API_KEY: LoadFocus API -avaimesi (merkitse "Masked")
    • LOADFOCUS_TEAM_ID: LoadFocus tiimi-ID:si

2. Luo GitLab CI/CD -putki

Luo tai päivitä .gitlab-ci.yml-tiedostosi repositoriossasi:

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

Vinkkejä GitLab CI/CD -integraatioon

  1. Välimuisti: Välimuistita npm-riippuvuudet putkiajojen nopeuttamiseksi:

    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. Aikakatkaisuasetukset: Aseta aikakatkaisut pitkäkestoisille suorituskykytesteille:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    timeout: 2h # Set a 2-hour timeout
    script:
    # Performance test script...
  3. Manuaaliset laukaisut: Mahdollista suorituskykytestien manuaalinen laukaisu:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # Performance test script...
    when: manual
  4. Dynaaminen testikonfigurointi: Käytä GitLabin ennalta määritettyjä muuttujia testien dynaamiseen konfigurointiin:

    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

Lisätietoja löydät GitLab CI/CD -dokumentaatiosta ja LoadFocus API Client -dokumentaatiosta.