Skip to content

Commit

Permalink
gets duplicated code rate down
Browse files Browse the repository at this point in the history
  • Loading branch information
dagfinno committed Nov 4, 2024
1 parent e7cf0da commit 74d678c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 74 deletions.
87 changes: 17 additions & 70 deletions tests/k6/tests/serviceowner/performance/create-dialog-and-search.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import { postSO, getEU, expectStatusFor, expect, describe } from "../../../common/testimports.js";
import { SharedArray } from 'k6/data';
import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js';
import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

import { getDefaultThresholds } from '../../performancetest_common/common.js'
import { default as dialogToInsert } from '../../performancetest_data/01-create-dialog.js';

import { createDialog, options as dialogOptions } from './create-dialog.js';
import { simpleSearch, options as searchOptions } from '../../enduser/performance/simple-search.js';
const filenameServiceowners = '../../performancetest_data/.serviceowners-with-tokens.csv';

const serviceOwners = new SharedArray('serviceOwners', function () {
return papaparse.parse(open(filenameServiceowners), { header: true, skipEmptyLines: true }).data;
});

const filenameEndusers = '../../performancetest_data/.endusers-with-tokens.csv';

const endUsers = new SharedArray('endUsers', function () {
return papaparse.parse(open(filenameEndusers), { header: true, skipEmptyLines: true }).data;
const filenameEndusersWithTokens = '../../performancetest_data/.endusers-with-tokens.csv';
const endUsersWithTokens = new SharedArray('endUsersWithTokens', function () {
return papaparse.parse(open(filenameEndusersWithTokens), { header: true, skipEmptyLines: true }).data;
});

function joinThresholds(t1, t2) {
for (var k in t2) {
t1[k] = t2[k];
}
return t1;
}

export const options = {
scenarios: {
create_dialogs: {
Expand All @@ -34,73 +38,16 @@ export const options = {
vus: __ENV.evus,
duration: __ENV.duration,
}


},
summaryTrendStats: ['avg', 'min', 'med', 'max', 'p(95)', 'p(99)', 'p(99.5)', 'p(99.9)', 'count'],
thresholds: getDefaultThresholds(['http_req_duration', 'http_reqs'],[
'create dialog',
'simple search',
'get dialog'
])

thresholds: joinThresholds(searchOptions.thresholds, dialogOptions.thresholds)

};

export function createDialogs() {
if ((options.vus === undefined || options.vus === 1) && (options.iterations === undefined || options.iterations === 1)) {
createDialog(serviceOwners[0], endUsers[0]);
}
else {
createDialog(randomItem(serviceOwners), randomItem(endUsers));
}
}

export function createDialog(serviceOwner, endUser) {
var paramsWithToken = {
headers: {
Authorization: "Bearer " + serviceOwner.token
},
tags: { name: 'create dialog' }
}

describe('create dialog', () => {
let r = postSO('dialogs', dialogToInsert(endUser.ssn, endUser.resource), paramsWithToken);
expect(r.status, 'response status').to.equal(201);
});

createDialog(randomItem(serviceOwners), randomItem(endUsersWithTokens));
}

export function simpleSearches() {
if ((options.vus === undefined || options.vus === 1) && (options.iterations === undefined || options.iterations === 1)) {
simpleSearch(endUsers[0]);
}
else {
simpleSearch(randomItem(endUsers));
}
}

export function simpleSearch(enduser) {
let paramsWithToken = {
headers: {
Authorization: "Bearer " + enduser.token
},
tags: { name: 'simple search' }
}
let defaultParty = "urn:altinn:person:identifier-no:" + enduser.ssn;
let defaultFilter = "?Party=" + defaultParty;
describe('Perform simple dialog list', () => {
paramsWithToken.tags.name = 'simple search'
let r = getEU('dialogs' + defaultFilter, paramsWithToken);
expectStatusFor(r).to.equal(200);
expect(r, 'response').to.have.validJsonBody();
if ( r.json().items.length > 0 ) {
let dialogId = r.json().items[0].id;
if (dialogId) {
paramsWithToken.tags.name = 'get dialog'
let d = getEU('dialogs/' + dialogId, paramsWithToken);
expectStatusFor(d).to.equal(200);
expect(d, 'response').to.have.validJsonBody();
}
}
});
simpleSearch(randomItem(endUsersWithTokens));
}
1 change: 0 additions & 1 deletion tests/k6/tests/serviceowner/performance/create-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
import { getDefaultThresholds } from '../../performancetest_common/common.js'
import { default as dialogToInsert } from '../../performancetest_data/01-create-dialog.js';


const filenameServiceowners = '../../performancetest_data/.serviceowners-with-tokens.csv';
const filenameEndusers = `../../performancetest_data/endusers-${__ENV.API_ENVIRONMENT}.csv`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export let options = {

export default function() {
if ((options.vus === undefined || options.vus === 1) && (options.iterations === undefined || options.iterations === 1)) {
createDialog(serviceOwners[0], endUsers[0]);
createAndRemoveDialog(serviceOwners[0], endUsers[0]);
}
else {
createDialog(randomItem(serviceOwners), randomItem(endUsers));
createAndRemoveDialog(randomItem(serviceOwners), randomItem(endUsers));
}
}

export function createDialog(serviceOwner, endUser) {
export function createAndRemoveDialog(serviceOwner, endUser) {
var paramsWithToken = {
headers: {
Authorization: "Bearer " + serviceOwner.token
Expand Down

0 comments on commit 74d678c

Please sign in to comment.