Skip to content

Commit

Permalink
821 Spike Testing Script (#845)
Browse files Browse the repository at this point in the history
* spike script
  • Loading branch information
kclark-scottlogic authored Feb 29, 2024
1 parent 16a8cbd commit d5f19c0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
42 changes: 2 additions & 40 deletions k6/load.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { check, sleep } from 'k6';
import exec from 'k6/execution';
import http from 'k6/http';
import { postWithSessionUpdate } from './requestFunctions.js';

export const options = {
//vus and duration can be changed depending on what simulation needed running
Expand All @@ -13,40 +11,4 @@ export const options = {
},
};

const baseUrl = 'http://localhost:3001';
const cookieName = 'prompt-injection.sid';
const vuCookieJar = (() => {
const cookieJars = {};
return {
get: (id) => cookieJars[id],
set: (id, jar) => (cookieJars[id] = jar),
};
})();

export default () => {
// Use same jar for every iteration of same VU! k6 doesn't do this for us :(
const vuID = exec.vu.idInTest;
let jar = vuCookieJar.get(vuID);
if (!jar) {
jar = http.cookieJar();
vuCookieJar.set(vuID, jar);
}
let originalCookie = (jar.cookiesForURL(baseUrl)[cookieName] || [])[0];

const data = { infoMessage: 'Hi', chatMessageType: 'LEVEL_INFO', level: 3 };
const response = http.post(`${baseUrl}/test/load`, JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
jar,
});
// Expecting cookie to match original, OR first-time be added to the jar
const expectedCookie =
originalCookie || jar.cookiesForURL(baseUrl)[cookieName][0];
check(response, {
'response code was 200': (response) => response.status === 200,
'cookie was preserved': (response) =>
response.cookies[cookieName].length === 1 &&
response.cookies[cookieName][0].value === expectedCookie,
});

sleep(1);
};
export default postWithSessionUpdate;
41 changes: 41 additions & 0 deletions k6/requestFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { check, sleep } from 'k6';
import exec from 'k6/execution';
import http from 'k6/http';

const baseUrl = 'http://localhost:3001';
const cookieName = 'prompt-injection.sid';
const vuCookieJar = (() => {
const cookieJars = {};
return {
get: (id) => cookieJars[id],
set: (id, jar) => (cookieJars[id] = jar),
};
})();

export function postWithSessionUpdate() {
// Use same jar for every iteration of same VU! k6 doesn't do this for us :(
const vuID = exec.vu.idInTest;
let jar = vuCookieJar.get(vuID);
if (!jar) {
jar = http.cookieJar();
vuCookieJar.set(vuID, jar);
}
let originalCookie = (jar.cookiesForURL(baseUrl)[cookieName] || [])[0];

const data = { infoMessage: 'Hi', chatMessageType: 'LEVEL_INFO', level: 3 };
const response = http.post(`${baseUrl}/test/load`, JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
jar,
});
// Expecting cookie to match original, OR first-time be added to the jar
const expectedCookie =
originalCookie || jar.cookiesForURL(baseUrl)[cookieName][0];
check(response, {
'response code was 200': (response) => response.status === 200,
'cookie was preserved': (response) =>
response.cookies[cookieName].length === 1 &&
response.cookies[cookieName][0].value === expectedCookie,
});

sleep(1);
}
11 changes: 11 additions & 0 deletions k6/spike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { postWithSessionUpdate } from './requestFunctions.js';

export const options = {
// Key configurations for spike in this section
stages: [
{ duration: '2m', target: 2000 }, // fast ramp-up to a high point
{ duration: '1m', target: 0 }, // quick ramp-down to 0 users
],
};

export default postWithSessionUpdate;

0 comments on commit d5f19c0

Please sign in to comment.