GitLab CI/CD

이 가이드에서는 자동화된 성능 테스트를 위해 LoadFocus JMeter API Client를 GitLab CI/CD와 통합하는 방법을 설명합니다.

설정 단계

1. GitLab CI/CD 변수로 자격 증명 저장

먼저 LoadFocus API 자격 증명을 GitLab CI/CD 변수로 저장합니다:

  1. GitLab 프로젝트로 이동합니다
  2. Settings > CI/CD > Variables로 이동합니다
  3. 다음 변수를 추가합니다:
    • LOADFOCUS_API_KEY: LoadFocus API 키 ("Masked"로 표시)
    • LOADFOCUS_TEAM_ID: LoadFocus 팀 ID

2. GitLab CI/CD 파이프라인 생성

리포지토리에 .gitlab-ci.yml 파일을 생성하거나 업데이트합니다:

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:
# LoadFocus JMeter API Client 설치
- npm install -g @loadfocus/loadfocus-api-client
# 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. 테스트 결과 보기

파이프라인이 실행된 후:

  1. GitLab 프로젝트로 이동합니다
  2. CI/CD > Pipelines로 이동합니다
  3. 파이프라인을 찾아 클릭합니다
  4. "performance_test" 작업으로 이동합니다
  5. 오른쪽 사이드바의 "Browse"를 클릭하여 아티팩트를 확인합니다
  6. performance_results.json 파일을 다운로드하여 확인합니다

고급 구성

환경별 테스트

환경에 따라 다른 성능 테스트를 실행합니다:

.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

병렬 테스트

여러 성능 테스트를 병렬로 실행합니다:

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

그런 다음 GitLab에서 파이프라인 스케줄을 생성합니다:

  1. GitLab 프로젝트로 이동합니다
  2. CI/CD > Schedules로 이동합니다
  3. "New schedule"을 클릭합니다
  4. 스케줄을 설정합니다 (예: 매일 자정)

성능 보고서 생성

JSON 결과에서 HTML 보고서를 생성합니다:

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
# HTML 보고서 생성
- npm install -g performance-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
# 선택 사항: 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

GitLab 기능과의 통합

Merge Request 위젯

GitLab의 JUnit 보고서 기능을 사용하여 Merge Request에 성능 테스트 결과를 표시합니다:

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
# JUnit 형식으로 변환 (커스텀 스크립트 사용)
- 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 메트릭

GitLab의 메트릭 기능을 사용하여 시간에 따른 성능을 추적합니다:

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
# 메트릭을 추출하여 GitLab에 보고
- |
# JSON을 파싱하여 메트릭 추출
RESPONSE_TIME=$(jq '.labels[0].metrics.avgresponse' performance_results.json)
ERROR_RATE=$(jq '.labels[0].metrics.errors' performance_results.json)
# 메트릭 보고
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 환경

성능 테스트를 특정 환경과 연결합니다:

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

GitLab CI/CD 통합 팁

  1. 캐싱: npm 종속성을 캐싱하여 파이프라인 실행 속도를 높입니다:

    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
    # 나머지 스크립트...
  2. 타임아웃 설정: 오래 실행되는 성능 테스트에 대한 타임아웃을 설정합니다:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    timeout: 2h # 2시간 타임아웃 설정
    script:
    # 성능 테스트 스크립트...
  3. 수동 트리거: 성능 테스트를 수동으로 트리거할 수 있도록 합니다:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # 성능 테스트 스크립트...
    when: manual
  4. 동적 테스트 구성: GitLab 사전 정의 변수를 사용하여 테스트를 동적으로 구성합니다:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    - |
    # 브랜치에 따라 임계값 설정
    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. 알림: 성능 테스트가 실패할 때 알림을 보냅니다:

    performance_test:
    stage: performance
    image: node:${NODE_VERSION}
    script:
    # 성능 테스트 스크립트...
    after_script:
    - |
    if [ $? -ne 0 ]; then
    # curl, GitLab API 또는 기타 방법을 사용하여 알림 전송
    curl -X POST -H "Content-Type: application/json" \
    -d "{\"text\":\"${CI_PROJECT_NAME}의 성능 테스트가 실패했습니다\"}" \
    $WEBHOOK_URL
    fi

자세한 내용은 GitLab CI/CD 문서LoadFocus API Client 문서를 참조하세요.