負荷テストAPIインテグレーション
このガイドでは、コマンドラインインターフェース(CLI)とアプリケーションでJavaScriptライブラリを直接使用する両方の方法で、LoadFocus APIクライアントの使用方法を説明します。
目次
インストール
グローバルインストール
npm install -g @loadfocus/loadfocus-api-client
ローカルプロジェクトインストール
npm install @loadfocus/loadfocus-api-client
設定
LoadFocus APIクライアントを使用する前に、API資格情報を設定する必要があります。
CLI設定
# APIキーとチームIDを設定loadfocus-api config set apikey YOUR_API_KEYloadfocus-api config set teamid YOUR_TEAM_ID# 設定を確認loadfocus-api config show
JavaScript設定
const { configManager } = require('@loadfocus/loadfocus-api-client');// 設定configManager.set('apikey', 'YOUR_API_KEY');configManager.set('teamid', 'YOUR_TEAM_ID');// 設定の確認console.log(configManager.get('apikey')); // APIキーが表示されるはずconsole.log(configManager.isConfigured()); // 必要な設定がすべて完了していればtrueが表示
コマンドラインインターフェース(CLI)
LoadFocus APIクライアントは、LoadFocus APIと連携するための包括的なCLIを提供します。
JMeterテストの実行
テストを実行する
# テスト名でテストを実行loadfocus-api jmeter execute --name "My JMeter Test"# 特定のパラメータでテストを実行loadfocus-api jmeter execute --name "My JMeter Test" --threads 50 --rampup 30 --duration 300
テストを実行して完了を待つ
# テストを実行して完了を待つloadfocus-api jmeter run-test --name "My JMeter Test"# しきい値付きでテストを実行loadfocus-api jmeter run-test --name "My JMeter Test" --thresholds "avgresponse<=200,errors==0,p95<=250"# カスタムタイムアウトとポーリングインターバルでテストを実行loadfocus-api jmeter run-test --name "My JMeter Test" --waitTimeout 1800 --pollInterval 15
テストステータスのモニタリング
# テスト名とIDでステータスを確認loadfocus-api jmeter status --name "My JMeter Test" --id 12345# 最近のテスト実行リストを取得loadfocus-api jmeter runs --limit 10
結果の取得
# 特定のテストの結果を取得loadfocus-api jmeter results --name "My JMeter Test" --id 12345# 特定のメトリクスを含む結果を取得loadfocus-api jmeter results --name "My JMeter Test" --id 12345 --include samples,avgresponse,errors
しきい値の使用
run-testコマンドは、パフォーマンスメトリクスに基づいてテストの合格・不合格を自動的に判定するしきい値評価をサポートしています。
# 複数のしきい値でテストを実行loadfocus-api jmeter run-test --name "My JMeter Test" --thresholds "avgresponse<=200,errors==0,p95<=250,hitspersec>=10"
サポートされるしきい値演算子:
<=- 以下<- 未満>=- 以上>- より大きい==- 等しい!=- 等しくない
出力フォーマット
# JSON形式で結果を取得loadfocus-api jmeter run-test --name "My JMeter Test" --format json > results.json# デフォルトの整形出力loadfocus-api jmeter run-test --name "My JMeter Test"
JavaScriptライブラリの使用
LoadFocus APIクライアントは、アプリケーション内でJavaScriptライブラリとしても直接使用できます。
基本セットアップ
// LoadFocus APIクライアントのインポートconst loadfocus = require('@loadfocus/loadfocus-api-client');// 特定のコンポーネントにアクセスconst { JMeterClient, configManager } = loadfocus;
JMeterクライアント
// JMeterクライアントの作成const jmeterClient = new loadfocus.JMeterClient();// または明示的な設定でconst jmeterClient = new loadfocus.JMeterClient({apikey: 'YOUR_API_KEY',teamid: 'YOUR_TEAM_ID'});
テストの実行
async function executeTest() {try {const result = await jmeterClient.execute({testrunname: 'My JMeter Test',threads: 50,rampup: 30,duration: 300});console.log('Test execution started:', result);return result.testrunid; // 後で使用するためにテストIDを返す} catch (error) {console.error('Error executing test:', error);}}
テストステータスのモニタリング
async function checkTestStatus(testName, testId) {try {const status = await jmeterClient.getStatus({testrunname: testName,testrunid: testId});console.log('Test status:', status);return status.state; // 現在のステートを返す} catch (error) {console.error('Error checking test status:', error);}}
結果の取得
async function getTestResults(testName, testId) {try {// テストの利用可能なラベルを取得const labels = await jmeterClient.getLabels({testrunname: testName,testrunid: testId});console.log('Test labels:', labels);// 各ラベルの結果を取得const allResults = [];for (const label of labels) {const labelResults = await jmeterClient.getResults({testrunname: testName,testrunid: testId,filter: label});allResults.push({label,results: labelResults});}console.log('Test results:', allResults);return allResults;} catch (error) {console.error('Error retrieving test results:', error);}}
完全な例
テストを実行し、完了を待ち、結果を取得する完全な例です:
const { JMeterClient, configManager } = require('@loadfocus/loadfocus-api-client');// 設定のセットアップconfigManager.set('apikey', 'YOUR_API_KEY');configManager.set('teamid', 'YOUR_TEAM_ID');// クライアントの作成const jmeterClient = new JMeterClient();async function runCompleteTest() {try {// テストを実行console.log('Executing test...');const executeResult = await jmeterClient.execute({testrunname: 'My JMeter Test'});const testId = executeResult.testrunid;console.log(`Test execution started with ID: ${testId}`);// 完了を待つconsole.log('Waiting for test to complete...');let completed = false;while (!completed) {const status = await jmeterClient.getStatus({testrunname: 'My JMeter Test',testrunid: testId});console.log(`Current state: ${status.state}`);if (status.state === 'finished') {completed = true;} else if (status.state === 'failed' || status.state === 'error') {throw new Error(`Test failed with state: ${status.state}`);} else {// 再チェック前に待機await new Promise(resolve => setTimeout(resolve, 10000));}}// 結果を取得console.log('Getting test results...');const labels = await jmeterClient.getLabels({testrunname: 'My JMeter Test',testrunid: testId});const allResults = [];for (const label of labels) {const labelResults = await jmeterClient.getResults({testrunname: 'My JMeter Test',testrunid: testId,filter: label});allResults.push({label,results: labelResults});}console.log('Test results:', JSON.stringify(allResults, null, 2));return allResults;} catch (error) {console.error('Error running test:', error);}}// テストを実行runCompleteTest();
高度な使用方法
カスタムHTTP設定
LoadFocus APIクライアントが使用するHTTPクライアントをカスタマイズできます:
const { JMeterClient } = require('@loadfocus/loadfocus-api-client');// カスタムHTTPオプションでクライアントを作成const jmeterClient = new JMeterClient({apikey: 'YOUR_API_KEY',teamid: 'YOUR_TEAM_ID',httpOptions: {timeout: 30000, // 30秒retries: 3,headers: {'User-Agent': 'My Custom Application'}}});
エラーハンドリング
LoadFocus APIクライアントは詳細なエラー情報を提供します:
try {const result = await jmeterClient.execute({testrunname: 'My JMeter Test'});} catch (error) {if (error.response) {// リクエストが送信され、サーバーが2xx範囲外のステータスコードで応答したconsole.error('API Error:', error.response.status, error.response.data);} else if (error.request) {// リクエストが送信されたがレスポンスがなかったconsole.error('Network Error:', error.request);} else {// リクエストのセットアップ中にエラーが発生したconsole.error('Request Error:', error.message);}}
トラブルシューティング
よくある問題
認証エラー:
- APIキーとチームIDが正しく設定されていることを確認してください
- APIキーに必要な権限があることを確認してください
テスト実行の失敗:
- テスト名がLoadFocusアカウントに存在することを確認してください
- アカウントの同時テスト制限に達していないか確認してください
タイムアウトの問題:
- 長時間実行されるテストの場合、
waitTimeoutパラメータを増やしてください - 同期的に待機する代わりに、ポーリングメカニズムの実装を検討してください
- 長時間実行されるテストの場合、
結果取得の問題:
- 結果を取得する前にテストが完了していることを確認してください
- テストIDが正しいか確認してください
デバッグ
より詳細な情報を得るために、デバッグログを有効にします:
// JavaScriptコード内でprocess.env.DEBUG = 'true';// CLIを使用する場合DEBUG=true loadfocus-api jmeter run-test --name "My JMeter Test"
追加のヘルプについては、LoadFocus APIドキュメントを参照するか、サポートにお問い合わせください。