Skip to content

Instantly share code, notes, and snippets.

@moio
Created September 1, 2023 10:29
Show Gist options
  • Save moio/19146fc505ea995c69fed93bdff636de to your computer and use it in GitHub Desktop.
Save moio/19146fc505ea995c69fed93bdff636de to your computer and use it in GitHub Desktop.
k6 script to test Rancher's Steve API listing performance
import { check, fail } from 'k6';
import http from 'k6/http';
// Parameters
const vus = __ENV.VUS || 1
const perVuIterations = __ENV.PER_VU_ITERATIONS || 30
const baseUrl = __ENV.BASE_URL
const user = __ENV.USER
const password = __ENV.PASSWORD
const cluster = __ENV.CLUSTER || "local"
const resource = __ENV.RESOURCE || "management.cattle.io.settings"
// Option setting
export const options = {
insecureSkipTLSVerify: true,
scenarios: {
list : {
executor: 'per-vu-iterations',
exec: 'list',
vus: vus,
iterations: perVuIterations,
maxDuration: '24h',
}
},
thresholds: {
checks: ['rate>0.99']
}
}
// Test functions, in order of execution
export function setup() {
// log in
const res = http.post(`${baseUrl}/v3-public/localProviders/local?action=login`, JSON.stringify({
"description": "UI session",
"responseType": "cookie",
"username": user,
"password": password
}))
check(res, {
'logging in returns status 200': (r) => r.status === 200,
})
return http.cookieJar().cookiesForURL(res.url)
}
export function list(cookies) {
const url = cluster === "local"?
`${baseUrl}/v1/${resource}` :
`${baseUrl}/k8s/clusters/${cluster}/v1/${resource}`
let revision = null
let continueToken = null
while (true) {
const fullUrl = url + "?limit=100" +
(revision != null ? "&revision=" + revision : "") +
(continueToken != null ? "&continue=" + continueToken : "")
const res = http.get(fullUrl, {cookies: cookies})
const criterion = {}
criterion[`listing ${resource} from cluster ${cluster} returns status 200`] = (r) => r.status === 200
check(res, criterion)
try {
const body = JSON.parse(res.body)
if (body === undefined || body.continue === undefined) {
break
}
if (revision == null) {
revision = body.revision
}
continueToken = body.continue
}
catch (e) {
if (e instanceof SyntaxError) {
fail("Response body does not parse as JSON: " + res.body)
}
throw e
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment