-
Notifications
You must be signed in to change notification settings - Fork 0
/
examineReports.ts
147 lines (128 loc) · 4.49 KB
/
examineReports.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { xml2json } from "xml-js";
import fs from "node:fs";
const getAverageTestCaseTimes = (
fileContents: Map<string, any>
): Map<string, number> => {
// jest: testsuites -> testsuite -> testcase x3
// vitest: testsuites -> testsuite -> testcase x3
// mocha: testsuite -> testcase x3
// key is `${framework} ${testType}`, value is array of arrays of times
let aggregatedMap: Map<string, any[]> = new Map();
for (const key of fileContents.keys()) {
const split = key.replace("reports/", "").split("-");
const testType = split[0];
const framework = split[1];
const currentXML = fileContents.get(key);
let times: number[] = [];
if (currentXML["testsuite"]) {
// mocha reporter
const testCases = currentXML["testsuite"]["testcase"];
times = testCases.map((item) => Number(item["_attributes"].time));
} else if (currentXML["testsuites"]) {
// jest/vitest reporters
const testCases = currentXML["testsuites"]["testsuite"]["testcase"];
times = testCases.map((item) => Number(item["_attributes"].time));
}
const aggregationKey = `${framework} ${testType}`;
const valueWithinAggMap = aggregatedMap.get(aggregationKey);
if (valueWithinAggMap) {
aggregatedMap.set(aggregationKey, [...valueWithinAggMap, times]);
} else {
aggregatedMap.set(aggregationKey, [times]);
}
}
// map of the test type and the average time to execute
let finalMap: Map<string, number> = new Map();
for (const testType of aggregatedMap.entries()) {
let avgTimes = testType[1].map((arr) => {
let sum = 0;
for (const time of arr) {
sum += time;
}
return sum / arr.length;
});
let totalSum = 0;
for (const time of avgTimes) {
totalSum += time;
}
finalMap.set(testType[0], totalSum / avgTimes.length);
}
console.log(
"Map of Test Times (Seconds) for Each Test's Execution Time (NOT including startup and teardown) ",
finalMap
);
return finalMap;
};
const getAverageTestSuiteTimes = (
fileContents: Map<string, any>
): Map<string, number> => {
// jest: testsuites -> testsuite -> testcase x3
// vitest: testsuites -> testsuite -> testcase x3
// mocha: testsuite -> testcase x3
// key is `${framework} ${testType}`, value is array of arrays of times
let aggMap: Map<string, any[]> = new Map();
for (const key of fileContents.keys()) {
const split = key.replace("reports/", "").split("-");
const testType = split[0];
const framework = split[1];
const currentXML = fileContents.get(key);
let time: number = -1;
if (currentXML["testsuite"]) {
// mocha reporter
const testSuite = currentXML["testsuite"];
time = Number(testSuite["_attributes"].time);
} else if (currentXML["testsuites"]) {
// jest/vitest reporters
const testSuite = currentXML["testsuites"]["testsuite"];
time = Number(testSuite["_attributes"].time);
}
const aggregationKey = `${framework} ${testType}`;
const valueWithinAggMap = aggMap.get(aggregationKey);
if (valueWithinAggMap) {
aggMap.set(aggregationKey, [...valueWithinAggMap, time]);
} else {
aggMap.set(aggregationKey, [time]);
}
}
// map of the test type and the average time to execute
let finalMap: Map<string, number> = new Map();
for (const testType of aggMap.entries()) {
let sum = 0;
for (const time of testType[1]) {
sum += time;
}
finalMap.set(testType[0], sum / testType[1].length);
}
console.log(
"Map of Times (Seconds) for Each Test Suite's Execution (INCLUDING startup and teardown) ",
finalMap
);
return finalMap;
};
const readReports = () => {
const reportFiles = fs.readdirSync("./reports");
// key is file name, value is file contents
let fileContents: Map<string, any> = new Map();
for (const file of reportFiles) {
// I am well aware I am not handling errors gracefully, but this is being written fast ;D
fileContents.set(
`reports/${file}`,
JSON.parse(
xml2json(fs.readFileSync(`reports/${file}`).toString(), {
compact: true,
})
)
);
}
const averageTestCaseTimes = getAverageTestCaseTimes(fileContents);
const averageTestSuiteTimes = getAverageTestSuiteTimes(fileContents);
fs.writeFileSync(
"test_case_results.txt",
JSON.stringify(Object.fromEntries(averageTestCaseTimes), null, 2)
);
fs.writeFileSync(
"test_suite_results.txt",
JSON.stringify(Object.fromEntries(averageTestSuiteTimes), null, 2)
);
};
readReports();