-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { vu } from "k6/execution"; | ||
|
||
export type Result<T, E = string> = | ||
| { data: T; ok: true } | ||
| { error: E; ok: false }; | ||
|
||
export enum SYSTEM { | ||
KEYCLOAK = "keycloak", | ||
BACKEND = "backend", | ||
} | ||
|
||
export function getError(system: SYSTEM, message: string) { | ||
return `${vu.idInInstance}:${vu.iterationInInstance} | ${system} error | ${message}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import { sleep } from "k6"; | ||
import { Counter, Trend } from "k6/metrics"; | ||
import { Result } from "./types.ts"; | ||
|
||
const completedCounter = new Counter("usecases_completed"); | ||
const abortedCounter = new Counter("usecases_aborted"); | ||
|
||
const completedDuration = new Trend("usecases_completed_duration", true); | ||
const abortedDuration = new Trend("usecases_aborted_duration", true); | ||
|
||
export function wrapTestFunction(testFunction: () => void): () => void { | ||
return () => { | ||
export type WrappableTestFunction<T> = (testData?: T) => Result<undefined>; | ||
export type TestFunction<T> = (testData?: T) => void; | ||
|
||
export function wrapTestFunction<T>( | ||
testFunction: WrappableTestFunction<T>, | ||
): TestFunction<T> { | ||
return (testData?: T) => { | ||
const start = Date.now(); | ||
try { | ||
testFunction(); | ||
completedCounter.add(1); | ||
completedDuration.add(Date.now() - start); | ||
} catch (error: unknown) { | ||
abortedCounter.add(1, { name: getErrorName(error as Error) }); | ||
abortedDuration.add(Date.now() - start); | ||
console.log(`${__VU} - ${__ITER}`); | ||
console.log(error); | ||
const result = testFunction(testData); | ||
if (result.ok) { | ||
completedCounter.add(1); | ||
completedDuration.add(Date.now() - start); | ||
} else { | ||
abortedCounter.add(1, { name: result.error }); | ||
abortedDuration.add(Date.now() - start); | ||
console.error(`${__VU} - ${__ITER} | ${result.error}`); | ||
} | ||
} finally { | ||
sleep(1); | ||
} | ||
}; | ||
} | ||
|
||
function getErrorName(error: Error): string { | ||
let name = "unknown error"; | ||
if (error.name) name = `${error.name}`; | ||
else if (error.message) name = `${error.message}`; | ||
return name; | ||
} |