mirror of
https://github.com/suitenumerique/docs.git
synced 2026-09-25 11:05:14 +02:00
To stress test the backend application we will use the k6 project from grafana. 2 scenarios are implemented and documented in the README. How to use this project is also documented in the README
42 lines
1.8 KiB
JavaScript
42 lines
1.8 KiB
JavaScript
/**
|
|
* The users of a run: the manifest `create_load_test_sessions` writes
|
|
* (src/backend/loadtest), one session per virtual user.
|
|
*
|
|
* k6 reads the file once, in the init context, and every VU keeps the session
|
|
* of its rank: with more VUs than sessions a user is logged in several times,
|
|
* and the API throttles per user (80/min on documents) — ask for at least as
|
|
* many sessions as VUs.
|
|
*/
|
|
import { SharedArray } from 'k6/data';
|
|
|
|
const manifest = JSON.parse(open(__ENV.MANIFEST || '/manifest.json'));
|
|
if (!manifest.cookie_name || !manifest.sessions || manifest.sessions.length === 0) {
|
|
throw new Error('The manifest holds no session');
|
|
}
|
|
if (manifest.expires_at && Date.parse(manifest.expires_at) < Date.now()) {
|
|
throw new Error(`The sessions of the manifest expired at ${manifest.expires_at}`);
|
|
}
|
|
|
|
// shared between the VUs rather than copied into each of them
|
|
const sessions = new SharedArray('sessions', () => manifest.sessions);
|
|
const publicDocuments = new SharedArray('public documents', () => manifest.public_documents || []);
|
|
|
|
export const COOKIE_NAME = manifest.cookie_name;
|
|
export const sessionCount = () => sessions.length;
|
|
|
|
/** The session of this VU. */
|
|
export const mySession = () => sessions[(__VU - 1) % sessions.length];
|
|
|
|
/** A document of the VU's user, `editable` or any it may read; a public one when it has none. */
|
|
export const myDocument = (session, editable = false) => {
|
|
const own = editable
|
|
? session.editable_documents
|
|
: session.readonly_documents.concat(session.editable_documents);
|
|
const pool = own.length > 0 ? own : publicDocuments;
|
|
if (pool.length === 0) return null;
|
|
return pool[__ITER % pool.length];
|
|
};
|
|
|
|
export const publicDocument = () =>
|
|
publicDocuments.length > 0 ? publicDocuments[__ITER % publicDocuments.length] : null;
|