Last active
August 6, 2024 12:21
-
-
Save qnkhuat/2b0ddf64b2b5c339f20ccf7bb53d02b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import exec from 'k6/execution'; | |
import http from 'k6/http' | |
import { check, sleep } from 'k6'; | |
import { uuidv4 } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; | |
import { vu } from 'k6/execution'; | |
export const options = { | |
scenarios: { | |
contacts: { | |
executor: 'constant-vus', | |
vus: 8, | |
duration: '10m', | |
gracefulStop: '10m', | |
}, | |
}, | |
}; | |
export default function () { | |
const host = __ENV.host || "http://localhost:3000"; | |
const adminUser = JSON.stringify({ | |
username: __ENV.username || "[email protected]", | |
password: __ENV.password || "blackjet", | |
}); | |
const sandboxedUser = JSON.stringify({ | |
username: __ENV.sandboxedUsername || "[email protected]", | |
password: __ENV.sandboxedPassword || "blueberries", | |
}); | |
const user = vu.idInTest % 3 == 0 ? sandboxedUser : adminUser; // out of 4 users, 1 is sandboxed | |
console.log(`Logging in as ${JSON.parse(user).username}`); | |
const params = { | |
headers: { | |
'Content-Type': 'application/json', | |
} | |
}; | |
const dashboard_id = __ENV.dashboard_id || 10; | |
function logger (request_object) { | |
if (request_object.status >= 400) { | |
console.error(`${request_object.request.method} ${request_object.request.url} ${request_object.status} took ${request_object.timings.duration} ms total`) | |
console.error(request_object); | |
} else { | |
console.log(`${request_object.request.method} ${request_object.request.url} ${request_object.status} took ${request_object.timings.duration} ms total`) | |
} | |
} | |
function checker(request_object) { | |
if ( !check(request_object, { | |
'status code MUST be 200': (res) => res.status == 200 || res.status == 204 || res.status == 202, | |
})) { | |
logger(request_object) | |
} | |
return request_object; | |
} | |
// Auth | |
const res = checker(http.post(`${host}/api/session`, adminUser, params)); | |
const token = res.cookies["metabase.SESSION"][0].value | |
const jar = http.cookieJar(); | |
jar.set(`${host}`, 'metabase.SESSION', token) | |
sleep(1); | |
http.get(`${host}/api/user/current`) | |
http.get(`${host}/api/session/properties`) | |
// get Dashboard and metadata | |
const dashboard_load_id = uuidv4(); | |
let dashboard = checker(http.get(`${host}/api/dashboard/${dashboard_id}?dashboard_load_id=${dashboard_load_id}`)).json(); | |
console.log("Got dashboard"); | |
checker(http.get(`${host}/api/dashboard/${dashboard_id}/query_metadata?dashboard_load_id=${dashboard_load_id}`)); | |
console.log("Got metadata"); | |
// execute all the dashcards | |
const queryPayload = JSON.stringify({"parameters": [], "dashboard_id": dashboard_id, "dashboard_load_id": dashboard_load_id }); | |
const dashcards = dashboard["dashcards"]; | |
dashcards.map(dashcard => checker(http.post(`${host}/api/dashboard/${dashboard_id}/dashcard/${dashcard.id}/card/${dashcard.card_id}/query`, queryPayload, params))); | |
http.del(`${host}/api/session`); | |
console.log(`Executed ${dashcards.length} dashcards`); | |
sleep(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment