Skip to content

Commit

Permalink
chore: enhancements in k6 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Baliasnikov committed Jun 28, 2023
1 parent e8411dd commit 3600d38
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Actor {
credentialsService: CredentialsService;
proofsService: ProofsService;
didService: DidService;
longFormDid: string | undefined;
did: string | undefined;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,32 @@ export class Issuer extends Actor {
);
}

/**
* Creates an unpublished DID
*/
createUnpublishedDid() {
this.longFormDid = this.didService.createUnpublishedDid(this.issuerDidTemplate).longFormDid;
}

/**
* Creates and publishes a DID.
*/
createAndPublishDid() {
const unpublishedDid = this.didService.createUnpublishedDid(this.issuerDidTemplate).longFormDid;
this.did = this.didService.publishDid(unpublishedDid).didRef;
this.didService.waitForDidState(unpublishedDid, "PUBLISHED");
publishDid() {
this.did = this.didService.publishDid(this.longFormDid!).didRef;
this.didService.waitForDidState(this.longFormDid!, "PUBLISHED");
}

/**
* Creates a credential offer for the holder.
*/
createCredentialOffer() {
this.credential = this.credentialsService.createCredentialOffer(this.did!, this.connectionWithHolder!);
}

/**
* Waits for the credential offer to be sent.
*/
waitForCredentialOfferToBeSent() {
this.credentialsService.waitForCredentialState(this.credential!, "OfferSent");
}

Expand All @@ -88,6 +100,12 @@ export class Issuer extends Actor {
*/
issueCredential() {
this.credentialsService.issueCredential(this.credential!);
}

/**
* Waits for the credential to be sent.
*/
waitForCredentialToBeSent() {
this.credentialsService.waitForCredentialState(this.credential!, "CredentialSent");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { HttpService } from "./HttpService";
import { WAITING_LOOP_MAX_ITERATIONS, WAITING_LOOP_PAUSE_INTERVAL } from "./Config";
import { IssueCredentialRecord, Connection } from "@input-output-hk/prism-typescript-client";
import vu from "k6/execution";
import {v4 as uuidv4} from 'uuid';

/**
* A service class for managing credentials in the application.
* Extends the HttpService class.
*/
export class CredentialsService extends HttpService {

myuuid = uuidv4();

/**
* Creates a credential offer for a specific issuing DID and connection.
* @param {string} issuingDid - The issuing DID.
Expand All @@ -18,7 +21,7 @@ export class CredentialsService extends HttpService {
*/
createCredentialOffer(issuingDid: string, connection: Connection): IssueCredentialRecord {
const payload = `{
"claims": { "offerId": "${vu.vu.idInInstance}-${vu.vu.idInTest}-${vu.vu.iterationInScenario}" },
"claims": { "offerId": "${this.myuuid}-${vu.vu.idInInstance}-${vu.vu.idInTest}-${vu.vu.iterationInScenario}" },
"issuingDID": "${issuingDid}",
"connectionId": "${connection.connectionId}",
"automaticIssuance": false
Expand Down Expand Up @@ -77,8 +80,9 @@ export class CredentialsService extends HttpService {
let iterations = 0;
let record: IssueCredentialRecord | undefined;
do {
console.log(`${this.myuuid}-${vu.vu.idInInstance}-${vu.vu.idInTest}-${vu.vu.iterationInScenario}`)
record = this.getCredentialRecords().find(
r => r.claims["offerId"] === `${vu.vu.idInInstance}-${vu.vu.idInTest}-${vu.vu.iterationInScenario}`
r => r.claims["offerId"] === `${this.myuuid}-${vu.vu.idInInstance}-${vu.vu.idInTest}-${vu.vu.iterationInScenario}`
&& r.protocolState === "OfferReceived");
if (record) {
return record;
Expand All @@ -101,6 +105,7 @@ export class CredentialsService extends HttpService {
do {
const response = this.getCredentialRecord(credentialRecord);
currentState = response.protocolState;
console.log(`Credential state: ${currentState}`)
sleep(WAITING_LOOP_PAUSE_INTERVAL);
iterations++;
} while (currentState !== state && iterations < WAITING_LOOP_MAX_ITERATIONS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { group } from "k6";
import { Holder, Issuer } from "../actors";

export const issuer = new Issuer();
export const holder = new Holder();

export function connectionFlow() {
group('Issuer initiates connection with Holder', function () {
issuer.createHolderConnection();
});

group('Holder accepts connection with Issuer', function () {
holder.acceptIssuerConnection(issuer.connectionWithHolder!.invitation);
});

group('Issuer finalizes connection with Holder', function () {
issuer.finalizeConnectionWithHolder();
});

group('Holder finalizes connection with Issuer', function () {
holder.finalizeConnectionWithIssuer();
});
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { group } from 'k6';
import { Options } from 'k6/options';
import { Issuer, Holder } from '../../actors';
import { Connection } from '@input-output-hk/prism-typescript-client';

// export let options: Options = {
// stages: [
// { duration: '1m', target: 5 },
// ],
// thresholds: {
// http_req_failed: [{
// threshold: 'rate<=0.05',
// abortOnFail: true,
// }],
// http_req_duration: ['p(95)<=100'],
// checks: ['rate>=0.99'],
// },
// };

export let options: Options = {
scenarios: {
smoke: {
executor: 'constant-vus',
vus: 3,
duration: "1s",
},
},
thresholds: {
'http_req_duration{group:::Issuer creates credential offer}': ['max >= 0'],
'http_reqs{group:::Issuer creates credential offer}': ['count >= 0'],
'group_duration{group:::Issuer creates credential offer}': ['max >= 0'],
},
};

export const issuer = new Issuer();
export const holder = new Holder();

export function setup() {
group('Issuer publishes DID', function () {
issuer.createUnpublishedDid();
issuer.publishDid();
});

group('Holder creates unpublished DID', function () {
holder.createUnpublishedDid();
});

group('Issuer connects with Holder', function () {
issuer.createHolderConnection();
holder.acceptIssuerConnection(issuer.connectionWithHolder!.invitation);
issuer.finalizeConnectionWithHolder();
holder.finalizeConnectionWithIssuer();
});

return {
issuerDid: issuer.did,
holderDid: holder.did,
connectionWithHolder: issuer.connectionWithHolder!,
connectionWithIssuer: holder.connectionWithIssuer!
};
}

export default (data: { issuerDid: string; holderDid: string; connectionWithHolder: Connection, connectionWithIssuer: Connection }) => {

// This is the only way to pass data from setup to default
issuer.did = data.issuerDid;
holder.did = data.holderDid;
issuer.connectionWithHolder = data.connectionWithHolder;
holder.connectionWithIssuer = data.connectionWithIssuer;

group('Issuer creates credential offer', function () {
issuer.createCredentialOffer();
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Options } from 'k6/options';
import { Issuer } from '../../actors';

// export let options: Options = {
// stages: [
// { duration: '1m', target: 5 },
// ],
// thresholds: {
// http_req_failed: [{
// threshold: 'rate<=0.05',
// abortOnFail: true,
// }],
// http_req_duration: ['p(95)<=100'],
// checks: ['rate>=0.99'],
// },
// };

export let options: Options = {
vus: 1,
iterations: 3000
};

const issuer = new Issuer();

export default () => {
issuer.createUnpublishedDid();
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export let options: Options = {
const issuer = new Issuer();

export default () => {
issuer.createAndPublishDid();
issuer.createUnpublishedDid();
issuer.publishDid();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Options } from 'k6/options';
import { connectionFlow } from '../common';

export let options: Options = {
scenarios: {
smoke: {
executor: 'constant-vus',
vus: 3,
duration: "1m",
},
},
thresholds: {
http_req_failed: [{
threshold: 'rate==0',
abortOnFail: true,
}],
http_req_duration: ['p(95)<=500'],
checks: ['rate==1'],
},
};

export default() => {
connectionFlow();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const verifier = new Verifier();

export function setup() {
group('Issuer publishes DID', function () {
issuer.createAndPublishDid();
issuer.createUnpublishedDid();
issuer.publishDid();
});

group('Holder creates unpublished DID', function () {
Expand All @@ -37,9 +38,11 @@ export default (data: { issuerDid: string; holderDid: string; }) => {

group('Issuer creates credential offer for Holder', function () {
issuer.createCredentialOffer();
issuer.waitForCredentialOfferToBeSent();
holder.waitAndAcceptCredentialOffer();
issuer.receiveCredentialRequest();
issuer.issueCredential();
issuer.waitForCredentialToBeSent();
holder.receiveCredential();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import { group } from 'k6';
import { Options } from 'k6/options';
import { Issuer, Holder } from '../../actors';
import {issuer, holder} from '../common';

export let options: Options = {
stages: [
{ duration: '1m', target: 5 },
{ duration: '1s', target: 1 },
],
thresholds: {
http_req_failed: [{
threshold: 'rate<=0.05',
abortOnFail: true,
}],
http_req_duration: ['p(95)<=100'],
checks: ['rate>=0.99'],
},
// thresholds: {
// http_req_failed: [{
// threshold: 'rate<=0.05',
// abortOnFail: true,
// }],
// http_req_duration: ['p(95)<=1000'],
// checks: ['rate>=0.99'],
// },
};

const issuer = new Issuer();
const holder = new Holder();

// This is setup code. It runs once at the beginning of the test, regardless of the number of VUs.
export function setup() {

group('Issuer publishes DID', function () {
issuer.createAndPublishDid();
issuer.createUnpublishedDid();
issuer.publishDid();
});

group('Holder creates unpublished DID', function () {
Expand All @@ -48,6 +46,7 @@ export default (data: { issuerDid: string; holderDid: string; }) => {

group('Issuer creates credential offer for Holder', function () {
issuer.createCredentialOffer();
issuer.waitForCredentialOfferToBeSent();
});

group('Holder achieves and accepts credential offer from Issuer', function () {
Expand All @@ -57,6 +56,7 @@ export default (data: { issuerDid: string; holderDid: string; }) => {
group('Issuer issues credential to Holder', function () {
issuer.receiveCredentialRequest();
issuer.issueCredential();
issuer.waitForCredentialToBeSent();
});

group('Holder receives credential from Issuer', function () {
Expand Down

0 comments on commit 3600d38

Please sign in to comment.