CircleCIインテグレーション
このガイドでは、LoadFocus JMeter APIクライアントをCircleCIと統合して自動パフォーマンステストを実現する方法を説明します。
セットアップ手順
1. 環境変数を追加する
LoadFocus APIの資格情報をCircleCIの環境変数として保存します:
- Navigate to your project in CircleCI
- Go to Project Settings > Environment Variables
- Add the following variables:
LOADFOCUS_API_KEY: Your LoadFocus API keyLOADFOCUS_TEAM_ID: Your LoadFocus team ID
2. CircleCIワークフローを設定する
.circleci/config.ymlファイルを作成または更新してパフォーマンステストを含めます:
version: 2.1jobs:performance_test:docker:- image: cimg/node:16.13steps:- checkout- run:name: Install LoadFocus JMeter API Clientcommand: npm install -g @loadfocus/loadfocus-api-client- run:name: Configure LoadFocus API Clientcommand: |loadfocus-api config set apikey $LOADFOCUS_API_KEYloadfocus-api config set teamid $LOADFOCUS_TEAM_ID- run:name: Run Performance Testscommand: |loadfocus-api jmeter run-test \--name "CircleCI_${CIRCLE_PROJECT_REPONAME}_${CIRCLE_BRANCH}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json- store_artifacts:path: performance_results.jsondestination: performance-test-resultsworkflows:version: 2build_test_deploy:jobs:- build_and_test- performance_test:requires:- build_and_test- deploy:requires:- performance_testfilters:branches:only: main
3. パフォーマンスの問題でビルドを失敗させる
パフォーマンスのしきい値が満たされない場合にパイプラインを失敗させるには、「Run Performance Tests」ステップを修正します:
- run:name: Run Performance Testscommand: |loadfocus-api jmeter run-test \--name "CircleCI_${CIRCLE_PROJECT_REPONAME}_${CIRCLE_BRANCH}" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > performance_results.json# Check exit code - the command will exit with code 1 if thresholds are not metif [ $? -ne 0 ]; thenecho "Performance test failed to meet thresholds"exit 1fi
高度な設定
並列テスト
複数のパフォーマンステストを並列で実行するには:
performance_tests:docker:- image: cimg/node:16.13parallelism: 3steps:- checkout- run:name: Install LoadFocus JMeter API Clientcommand: npm install -g @loadfocus/loadfocus-api-client- run:name: Configure LoadFocus API Clientcommand: |loadfocus-api config set apikey $LOADFOCUS_API_KEYloadfocus-api config set teamid $LOADFOCUS_TEAM_ID- run:name: Run Performance Testscommand: |# Get test name based on indexTESTS=("API_Test" "Frontend_Test" "Database_Test")INDEX=$(( $CIRCLE_NODE_INDEX % 3 ))TEST_NAME=${TESTS[$INDEX]}echo "Running test: $TEST_NAME"loadfocus-api jmeter run-test \--name "$TEST_NAME" \--thresholds "avgresponse<=200,errors==0,p95<=250" \--format json > "performance_results_${TEST_NAME}.json"- store_artifacts:path: performance_results_*.jsondestination: performance-test-results
CircleCIインテグレーションのヒント
Resource Allocation: Ensure your CircleCI plan has sufficient resources for running performance tests, especially if they're long-running.
Timeout Settings: For longer tests, adjust the job timeout in CircleCI:
- run:name: Run Performance Testscommand: loadfocus-api jmeter run-test --name "Test_Name" --waitTimeout 3600no_output_timeout: 60mConditional Testing: Only run performance tests on specific branches or for specific commits:
performance_test:# Only run if commit message contains [PERF-TEST]when:condition:or:- equal: [ main, << pipeline.git.branch >> ]- matches:pattern: ".*\\[PERF-TEST\\].*"value: << pipeline.git.commit.message >>Notifications: Set up notifications for performance test failures:
- run:name: Notify on Performance Test Failurecommand: |if [ $? -ne 0 ]; thencurl -X POST -H "Content-Type: application/json" \-d '{"text":"Performance test failed for $CIRCLE_PROJECT_REPONAME"}' \$SLACK_WEBHOOK_URLfiwhen: on_fail
詳細については、CircleCIドキュメントおよびLoadFocus APIクライアントドキュメントをご参照ください。