Skip to content

Commit e97c981

Browse files
committed
[feat] Ignore K6 ASCII art and start summary of test run from output
1 parent e68f5b6 commit e97c981

File tree

3 files changed

+132
-9
lines changed

3 files changed

+132
-9
lines changed

dist/index.js

+62-3
Original file line numberDiff line numberDiff line change
@@ -30012,7 +30012,7 @@ async function run() {
3001230012
});
3001330013
// Parse k6 command output and extract test run URLs if running in cloud mode.
3001430014
// Also, print the output to the console, excluding the progress lines.
30015-
child.stdout?.on('data', (data) => (0, k6OutputParser_1.parseK6Output)(data, isCloud ? TEST_RESULT_URLS_MAP : null, TOTAL_TEST_RUNS));
30015+
child.stdout?.on('data', (data) => (0, k6OutputParser_1.parseK6Output)(data, TEST_RESULT_URLS_MAP, TOTAL_TEST_RUNS));
3001630016
return child;
3001730017
}
3001830018
}
@@ -30083,7 +30083,7 @@ function extractTestRunUrl(data, testResultUrlsMap) {
3008330083
* @param {string} data - The k6 command output data as string
3008430084
* @param {TestResultUrlsMap} testResultUrlsMap - The map containing the script path and output URL
3008530085
*
30086-
* @returns {void}
30086+
* @returns {boolean} - Returns true if the script path and output URL were successfully extracted and added to the map. Otherwise, returns false.
3008730087
*
3008830088
*/
3008930089
// Extracting the script path
@@ -30094,6 +30094,55 @@ function extractTestRunUrl(data, testResultUrlsMap) {
3009430094
const output = outputMatch ? outputMatch[1] : null;
3009530095
if (scriptPath && output) {
3009630096
testResultUrlsMap[scriptPath] = output;
30097+
return true;
30098+
}
30099+
else {
30100+
return false;
30101+
}
30102+
}
30103+
function checkIfK6ASCIIArt(data) {
30104+
/**
30105+
* This function checks if the given data is the k6 ASCII art or not.
30106+
*
30107+
* @param {string} data - The data to check
30108+
*
30109+
* @returns {boolean} - Returns true if the data is the k6 ASCII art. Otherwise, returns false.
30110+
*
30111+
* The k6 ASCII art is as follows:
30112+
*
30113+
*
30114+
*
30115+
* /\ |‾‾| /‾‾/ /‾‾/
30116+
* /\ / \ | |/ / / /
30117+
* / \/ \ | ( / ‾‾\
30118+
* / \ | |\ \ | (‾) |
30119+
* / __________ \ |__| \__\ \_____/ .io
30120+
*
30121+
* To determine if the data is the k6 ASCII art, the function checks the following:
30122+
* 1. The function checks if the data contains only the following characters:
30123+
* |, ' ', '\n', '/', '‾', '(', ')', '_', '.', 'i', 'o'
30124+
*
30125+
* 2. The function also checks if the data contains ".io" at the end.
30126+
*
30127+
* */
30128+
if (!data.includes(".io")) {
30129+
return false;
30130+
}
30131+
let K6_ASCII_ART_CHARS = [
30132+
'|', ' ', '\n', '/',
30133+
'‾', '(', ')', '_',
30134+
'.', 'i', 'o', '\\'
30135+
], dataChars = new Set(data);
30136+
if (dataChars.size !== K6_ASCII_ART_CHARS.length) {
30137+
return false;
30138+
}
30139+
else {
30140+
for (let char of dataChars) {
30141+
if (!K6_ASCII_ART_CHARS.includes(char)) {
30142+
return false;
30143+
}
30144+
}
30145+
return true;
3009730146
}
3009830147
}
3009930148
function parseK6Output(data, testResultUrlsMap, totalTestRuns) {
@@ -30111,7 +30160,17 @@ function parseK6Output(data, testResultUrlsMap, totalTestRuns) {
3011130160
const dataString = data.toString(), lines = dataString.split('\n');
3011230161
// Extract test run URLs
3011330162
if (testResultUrlsMap && Object.keys(testResultUrlsMap).length < totalTestRuns) {
30114-
extractTestRunUrl(dataString, testResultUrlsMap);
30163+
if (extractTestRunUrl(dataString, testResultUrlsMap)) {
30164+
// Test URL was extracted successfully and added to the map.
30165+
// Ignore further output parsing for this data.
30166+
return;
30167+
}
30168+
if (checkIfK6ASCIIArt(dataString)) {
30169+
// Ignore the k6 ASCII art.
30170+
// Checking the k6 ASCII art here because it is printed at the start of execution,
30171+
// hence if all the test URLs are extracted, the ASCII art will not be printed.
30172+
return;
30173+
}
3011530174
}
3011630175
const filteredLines = lines.filter((line) => {
3011730176
const isRegexMatch = REGEX_EXPRESSIONS.runningIteration.test(line) || REGEX_EXPRESSIONS.runProgress.test(line);

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export async function run(): Promise<void> {
123123
});
124124
// Parse k6 command output and extract test run URLs if running in cloud mode.
125125
// Also, print the output to the console, excluding the progress lines.
126-
child.stdout?.on('data', (data) => parseK6Output(data, isCloud ? TEST_RESULT_URLS_MAP : null, TOTAL_TEST_RUNS));
126+
child.stdout?.on('data', (data) => parseK6Output(data, TEST_RESULT_URLS_MAP, TOTAL_TEST_RUNS));
127127

128128
return child;
129129
}

src/k6OutputParser.ts

+69-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const REGEX_EXPRESSIONS = {
55
output: /^\s*output:\s*(.+)$/m,
66
runningIteration: /running \(.*\), \d+\/\d+ VUs, \d+ complete and \d+ interrupted iterations/g,
77
runProgress: /\[ *(\d+)% *\] *\d+ VUs/g
8-
};
8+
};
99

1010

11-
function extractTestRunUrl(data: string, testResultUrlsMap: TestResultUrlsMap): void {
11+
function extractTestRunUrl(data: string, testResultUrlsMap: TestResultUrlsMap): boolean {
1212
/**
1313
* This function extracts the script path and output URL from the k6 output.
1414
* It then adds the script path and output URL to the testResultUrlsMap which is a reference to
@@ -17,7 +17,7 @@ function extractTestRunUrl(data: string, testResultUrlsMap: TestResultUrlsMap):
1717
* @param {string} data - The k6 command output data as string
1818
* @param {TestResultUrlsMap} testResultUrlsMap - The map containing the script path and output URL
1919
*
20-
* @returns {void}
20+
* @returns {boolean} - Returns true if the script path and output URL were successfully extracted and added to the map. Otherwise, returns false.
2121
*
2222
*/
2323

@@ -31,10 +31,63 @@ function extractTestRunUrl(data: string, testResultUrlsMap: TestResultUrlsMap):
3131

3232
if (scriptPath && output) {
3333
testResultUrlsMap[scriptPath] = output;
34+
return true;
35+
} else {
36+
return false;
3437
}
3538
}
3639

3740

41+
function checkIfK6ASCIIArt(data: string): boolean {
42+
/**
43+
* This function checks if the given data is the k6 ASCII art or not.
44+
*
45+
* @param {string} data - The data to check
46+
*
47+
* @returns {boolean} - Returns true if the data is the k6 ASCII art. Otherwise, returns false.
48+
*
49+
* The k6 ASCII art is as follows:
50+
*
51+
*
52+
*
53+
* /\ |‾‾| /‾‾/ /‾‾/
54+
* /\ / \ | |/ / / /
55+
* / \/ \ | ( / ‾‾\
56+
* / \ | |\ \ | (‾) |
57+
* / __________ \ |__| \__\ \_____/ .io
58+
*
59+
* To determine if the data is the k6 ASCII art, the function checks the following:
60+
* 1. The function checks if the data contains only the following characters:
61+
* |, ' ', '\n', '/', '‾', '(', ')', '_', '.', 'i', 'o'
62+
*
63+
* 2. The function also checks if the data contains ".io" at the end.
64+
*
65+
* */
66+
67+
if (!data.includes(".io")) {
68+
return false;
69+
}
70+
71+
let K6_ASCII_ART_CHARS = [
72+
'|', ' ', '\n', '/',
73+
'‾', '(', ')', '_',
74+
'.', 'i', 'o', '\\'
75+
],
76+
dataChars = new Set(data);
77+
78+
79+
if (dataChars.size !== K6_ASCII_ART_CHARS.length) {
80+
return false;
81+
} else {
82+
for (let char of dataChars) {
83+
if (!K6_ASCII_ART_CHARS.includes(char)) {
84+
return false;
85+
}
86+
}
87+
return true;
88+
}
89+
}
90+
3891
export function parseK6Output(data: Buffer, testResultUrlsMap: TestResultUrlsMap | null, totalTestRuns: number): void {
3992
/*
4093
* This function is responsible for parsing the output of the k6 command.
@@ -53,8 +106,19 @@ export function parseK6Output(data: Buffer, testResultUrlsMap: TestResultUrlsMap
53106

54107
// Extract test run URLs
55108
if (testResultUrlsMap && Object.keys(testResultUrlsMap).length < totalTestRuns) {
56-
extractTestRunUrl(dataString, testResultUrlsMap);
57-
}
109+
if (extractTestRunUrl(dataString, testResultUrlsMap)) {
110+
// Test URL was extracted successfully and added to the map.
111+
// Ignore further output parsing for this data.
112+
return;
113+
}
114+
115+
if (checkIfK6ASCIIArt(dataString)) {
116+
// Ignore the k6 ASCII art.
117+
// Checking the k6 ASCII art here because it is printed at the start of execution,
118+
// hence if all the test URLs are extracted, the ASCII art will not be printed.
119+
return;
120+
}
121+
}
58122

59123
const filteredLines = lines.filter((line) => {
60124
const isRegexMatch = REGEX_EXPRESSIONS.runningIteration.test(line) || REGEX_EXPRESSIONS.runProgress.test(line);

0 commit comments

Comments
 (0)