diff --git a/cmd/new_test.go b/cmd/new_test.go index a3d58f68949..5f955a0b423 100644 --- a/cmd/new_test.go +++ b/cmd/new_test.go @@ -55,7 +55,7 @@ func TestNewScriptCmd(t *testing.T) { jsData := string(data) assert.Contains(t, jsData, "export const options = {") - assert.Contains(t, jsData, "export default function () {") + assert.Contains(t, jsData, "export default function() {") }) } } @@ -91,5 +91,5 @@ func TestNewScriptCmd_FileExists_Overwrite(t *testing.T) { require.NoError(t, err) assert.Contains(t, string(data), "export const options = {") - assert.Contains(t, string(data), "export default function () {") + assert.Contains(t, string(data), "export default function() {") } diff --git a/cmd/newtemplates/browser.js b/cmd/newtemplates/browser.js index b9376541071..46dbc00f15a 100644 --- a/cmd/newtemplates/browser.js +++ b/cmd/newtemplates/browser.js @@ -1,5 +1,6 @@ import { browser } from "k6/browser"; -import { check } from "k6"; +import http from "k6/http"; +import { sleep, check } from 'k6'; const BASE_URL = __ENV.BASE_URL || "https://quickpizza.grafana.com"; @@ -21,7 +22,16 @@ export const options = { }, {{ end }} }; -export default async function () { +export function setup() { + let res = http.get(BASE_URL); + if (res.status !== 200) { + throw new Error( + `Got unexpected status code ${res.status} when trying to setup. Exiting.` + ); + } +} + +export default async function() { let checkData; const page = await browser.newPage(); try { @@ -40,4 +50,5 @@ export default async function () { } finally { await page.close(); } + sleep(1); } diff --git a/cmd/newtemplates/minimal.js b/cmd/newtemplates/minimal.js index 58d64064afb..191ced04806 100644 --- a/cmd/newtemplates/minimal.js +++ b/cmd/newtemplates/minimal.js @@ -1,5 +1,5 @@ import http from 'k6/http'; -import { sleep } from 'k6'; +import { sleep, check } from 'k6'; export const options = { vus: 10, @@ -11,7 +11,8 @@ export const options = { }, {{ end }} }; -export default function () { - http.get('https://quickpizza.grafana.com'); +export default function() { + let res = http.get('https://quickpizza.grafana.com'); + check(res, { "status is 200": (res) => res.status === 200 }); sleep(1); } diff --git a/cmd/newtemplates/protocol.js b/cmd/newtemplates/protocol.js index 0d302d94be4..623be6fabe5 100644 --- a/cmd/newtemplates/protocol.js +++ b/cmd/newtemplates/protocol.js @@ -4,8 +4,15 @@ import { check, sleep } from "k6"; const BASE_URL = __ENV.BASE_URL || 'https://quickpizza.grafana.com'; export const options = { - vus: 5, - duration: '10s',{{ if .EnableCloud }} + vus: 10, + stages: [ + { duration: "10s", target: 5 }, + { duration: "20s", target: 10 }, + { duration: "1s", target: 0 }, + ], thresholds: { + http_req_failed: ["rate<0.01"], + http_req_duration: ["p(95)<500", "p(99)<1000"], + },{{ if .EnableCloud }} cloud: { {{ if .ProjectID }} projectID: {{ .ProjectID }}, {{ else }} // projectID: 12345, // Replace this with your own projectID {{ end }} @@ -13,7 +20,16 @@ export const options = { }, {{ end }} }; -export default function () { +export function setup() { + let res = http.get(BASE_URL); + if (res.status !== 200) { + throw new Error( + `Got unexpected status code ${res.status} when trying to setup. Exiting.` + ); + } +} + +export default function() { let restrictions = { maxCaloriesPerSlice: 500, mustBeVegetarian: false,