Skip to content

Instantly share code, notes, and snippets.

@syossan27
Created December 4, 2022 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syossan27/0a731378ea5176fb920024c33ff3378c to your computer and use it in GitHub Desktop.
Save syossan27/0a731378ea5176fb920024c33ff3378c to your computer and use it in GitHub Desktop.
// Creator: k6 Browser Recorder 0.6.2
import { sleep, group } from 'k6'
import http from 'k6/http'
import { Trend, Gauge, Counter, Rate } from 'k6/metrics'
const testTrend = new Trend('testTrend', true);
const testGauge = new Gauge('testGauge');
const testCounter = new Counter('testCounter');
const testRate = new Rate('testRate')
export const options = {
scenarios: {
test_scenario: {
// rate, timeUnitを実行するためのExecutor
executor: 'constant-arrival-rate',
duration: '1s',
// duration: '5s',
// timeUnitで指定された時間毎に反復するテスト回数
rate: 1,
// rate: 20,
// rateを反復させる時間
timeUnit: '1s',
// 初期に割り当てられるVUS数
preAllocatedVUs: 40,
// VUSが足りなかった場合に増える最大ユーザー数
maxVUs: 100,
},
},
thresholds: {
testTrend: ['avg < 120'],
testGauge: ['value < 1000'],
testCounter: ['count < 1'],
testRate: [{
threshold: 'rate < 0.1',
abortOnFail: true,
delayAbortEval: '1m'
}],
},
}
export default function main() {
let response
group('page_1 - http://example.com/', function () {
response = http.get('http://example.com/', {
headers: {
'upgrade-insecure-requests': '1',
},
})
})
// レスポンスが返ってくるまでの平均時間が120ms以下かどうか
testTrend.add(response.timings.duration);
// レスポンスボディのサイズが1000以下かどうか
testGauge.add(response.body.length);
// レスポンスボディ内のタイトルがExample Domainじゃないかどうか
const doc = response.html();
if (doc.find('title').text() === "Example Domain") {
testCounter.add(1);
}
// レスポンスの10%以上が200以外だった場合、1分後に負荷計測を中断する
testRate.add(response.status !== 200);
// Automatically added sleep
sleep(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment