k6を使ってKeyCloakの負荷試験を実行するスクリプトです。
import http from 'k6/http';
import { sleep, check } from 'k6';
export default function () {
const authData = { username: 'hoge', password: 'hoge!' };
// show login page
const loginPage = 'https://keycloak/auth/realms/my-realm/profile'
const loginResponse = http.get(loginPage)
check(loginResponse, {
'is login page status 200': (r) => r.status === 200,
});
const cookies = loginResponse.cookies;
// extract session code from a form
const regex = new RegExp('session_code=([a-zA-Z0-9].*)"');
const matchedPattern = regex.exec(loginResponse.body);
check(matchedPattern, {
'is session code matched with regex': (matched) => matched[1] !== undefined,
})
const sessionCode = matchedPattern[1];
// authenticate
const authenticateUrl = 'https://keycloak/auth/realms/my-realm/login-actions/authenticate?session_code=' + sessionCode + '&client_id=client_id';
const authResponse = http.post(authenticateUrl, JSON.stringify(authData), {
cookies: {
KC_RESTART: cookies.KC_RESTART[0].value,
AUTH_SESSION_ID: cookies.AUTH_SESSION_ID[0].value,
AUTH_SESSION_ID_LEGACY: cookies.AUTH_SESSION_ID_LEGACY[0].value,
}
});
check(authResponse, {
'is authentication status 200': (r) => r.status === 200,
});
// logout
const logoutUrl = 'https://keycloak/auth/realms/my-realm/protocol/openid-connect/logout';
const logoutResponse = http.get(logoutUrl);
check(logoutResponse, {
'is logout status 200': (r) => r.status === 200,
});
sleep(1);
}