Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(performance): create a k6 purge script to run after creating dialogs #1435

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions tests/k6/tests/serviceowner/performance/purge-dialogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* This script purges dialogs that have not been cleaned up by the tests.
* The script is intended to be run after the main tests have completed.
*
* The script retrieves all dialogs that contain a sentinel value in the search query.
* It then purges these dialogs.
*
* Run: k6 run tests/k6/tests/serviceowner/performance/purge-dialogs.js -e env=yt01
*/
import { getSO, purgeSO } from '../../../common/request.js';
import { serviceOwners } from '../../performancetest_common/readTestdata.js';
import { expect, expectStatusFor } from "../../../common/testimports.js";
import { getDefaultThresholds } from '../../performancetest_common/getDefaultThresholds.js';
import { describe } from '../../../common/describe.js';
import { sentinelValue } from '../../../common/config.js';

/**
* Retrieves the dialog ids to purge.
*
* @param {Object} serviceOwner - The service owner object.
* @returns {Array} - The dialog ids to purge.
*/
function getDialogs(serviceOwner) {
var paramsWithToken = {
headers: {
Authorization: "Bearer " + serviceOwner.token
}
}
let hasNextPage = false;
let continuationToken = "";
let dialogIdsToPurge = [];
do {
let r = getSO('dialogs/?Search=' + sentinelValue + continuationToken, paramsWithToken);
expectStatusFor(r).to.equal(200);
expect(r, 'response').to.have.validJsonBody();
let response = r.json();
if (response.items && response.items.length > 0) {
response.items.forEach((item) => {
dialogIdsToPurge.push(item.id);
});
if (response.continuationToken) {
continuationToken = "&continuationToken=" + response.continuationToken;
} else {
continuationToken = "";
}
}
console.log("Found " + dialogIdsToPurge.length + " unpurged dialogs");
if (dialogIdsToPurge.length < 200) {
hasNextPage = response.hasNextPage;
}
else {
hasNextPage = false;
}
} while (hasNextPage);
return dialogIdsToPurge;
}

export let options = {
summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(95)', 'p(99)', 'p(99.5)', 'p(99.9)', 'count'],
thresholds: getDefaultThresholds(['http_req_duration', 'http_reqs'],[
'purge dialog'
]),
setupTimeout: '4m',
};

export function setup() {
let data = [];
if (!serviceOwners || serviceOwners.length === 0) {
throw new Error('No service owners loaded for testing');
}
for (const serviceOwner of serviceOwners) {
let dialogIdsToPurge = getDialogs(serviceOwner);
data.push({token: serviceOwner.token, dialogIdsToPurge: dialogIdsToPurge});
}

return data;
}

/**
* Purges dialogs.
* In single user mode, the first service owner is used. Only one iteration is performed.
* In multi user mode, all service owners are used.
*/
export default function(serviceOwners) {
if (!serviceOwners || serviceOwners.length === 0) {
throw new Error('No service owners loaded for testing');
}

const isSingleUserMode = (options.vus ?? 1) === 1 && (options.iterations ?? 1) === 1 && (options.duration ?? 0) === 0;
if (isSingleUserMode) {
purgeDialogs(serviceOwners[0]);
}
else {
for (const serviceOwner of serviceOwners) {
purgeDialogs(serviceOwner);
}
}
}

/**
* Purges dialogs.
*
* @param {Object} serviceOwner - The service owner object.
*/
export function purgeDialogs(serviceOwner) {
var paramsWithToken = {
headers: {
Authorization: "Bearer " + serviceOwner.token
},
tags: { name: 'purge dialog' }
}
describe('Post run: checking for unpurged dialogs', () => {
let dialogIdsToPurge = serviceOwner.dialogIdsToPurge;
if (dialogIdsToPurge.length > 0) {
console.error("Found " + dialogIdsToPurge.length + " unpurged dialogs, make sure that all tests clean up after themselves. Purging ...");
for(var i = dialogIdsToPurge.length - 1; i>=0; i--) {
let r = purgeSO('dialogs/' + dialogIdsToPurge[i], paramsWithToken);
if (r.status != 204) {
console.error("Failed to purge dialog with id: " + dialogIdsToPurge[i]);
console.log(r);
}
else {
console.log("Purged dialog with id: " + dialogIdsToPurge[i]);
dialogIdsToPurge.splice(i, 1);
}
}
dagfinno marked this conversation as resolved.
Show resolved Hide resolved
}

// Fail the test after purging for visibility
expect(dialogIdsToPurge.length, 'unpurged dialogs').to.equal(0);
});
}