0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

k6でKeyCloakのログインとログアウトを負荷試験するスクリプト

Last updated at Posted at 2022-09-05

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);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?