-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
213 lines (166 loc) · 7.74 KB
/
test.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import glob from 'glob';
import fs from 'fs';
import { performance } from 'perf_hooks';
import { parse } from 'sveltedoc-parser';
const GLOB_FILTER = 'files/**/*.svelte';
const writeLog = console.log;
const ICON_BASE_URL = 'https://github.com/alexprey/sveltedoc-ci/raw/master';
const ARROW_DOWN = {
green: '/assets/down-arrow--green.png',
yellow: '/assets/down-arrow--yellow.png',
red: '/assets/down-arrow--red.png'
}
const ARROW_UP = {
green: '/assets/up-arrow--green.png',
yellow: '/assets/up-arrow--yellow.png',
red: '/assets/up-arrow--red.png'
}
function renderStatsTable(artifacts) {
let output = '';
const appendLine = (line) => {
output += `${line}\n`;
};
const compareTwoValues = (value, prevValue, formatFn, options) => {
const opt = {
...{
lowerIsBetter: false,
deltaPercentThreshold: 0.01
},
...options
}
const isBetterDelta = (delta) => {
return opt.lowerIsBetter ? delta < 0.0 : delta > 0.0;
}
const isBaddestDelta = (delta) => {
return opt.lowerIsBetter ? delta > 0.0 : delta < 0.0;
}
if (value !== undefined && prevValue !== undefined) {
const delta = value - prevValue;
if (Math.abs(delta) > 0.001 && Math.abs(delta / value) > opt.deltaPercentThreshold) {
const color = isBetterDelta(delta) ? 'green' : 'red';
if (delta < 0.0) {
return `${formatFn(value)} (![](${ICON_BASE_URL}${ARROW_DOWN[color]}) ${formatFn(delta)})`;
}
if (delta > 0.0) {
return `${formatFn(value)} (![](${ICON_BASE_URL}${ARROW_UP[color]}) +${formatFn(delta)})`;
}
}
}
return formatFn(value);
};
const formatPercent = value => {
return `${(value * 100.0).toFixed(2)}%`;
};
const formatFloat = value => {
return `${value.toFixed(2)}`;
}
// Render header
appendLine('| Version | Error rate | Avg. Parse time (ms) | Avg. Speed (B/ms) |');
appendLine('|---------|------------|--------------------------|---------------|');
artifacts.forEach((artifact, index) => {
const prevArtifact = index < artifacts.length - 1
? artifacts[index + 1]
: null;
const stats = artifact.stats;
const errorRate = stats.errorsCount / stats.totalHandledFilesCount;
const prevErrorRate = prevArtifact
? prevArtifact.stats.errorsCount / prevArtifact.stats.totalHandledFilesCount
: undefined;
const avgExecutionTimeInMs = stats.totalExecutionTimeInMs / stats.totalHandledFilesCount;
const avgSpeed = stats.bytesHandled / stats.totalExecutionTimeInMs;
const prevAvgSpeed = prevArtifact
? prevArtifact.stats.bytesHandled / prevArtifact.stats.totalExecutionTimeInMs
: undefined;
appendLine(
`| [${artifact.packageVersion}](https://github.com/alexprey/sveltedoc-parser/releases/tag/${artifact.packageVersion}) ` +
`| ${compareTwoValues(errorRate, prevErrorRate, formatPercent, { lowerIsBetter: true })} ` +
`| ${formatFloat(avgExecutionTimeInMs)} ` +
`| ${compareTwoValues(avgSpeed, prevAvgSpeed, formatFloat, { deltaPercentThreshold: 0.05 })} |`
);
});
return output;
}
function handleTestStats(stats) {
const svelteDocParserPackageConfig = JSON.parse(fs.readFileSync('node_modules/sveltedoc-parser/package.json'));
const artifactOutput = {
artifactVersion: 1,
packageVersion: svelteDocParserPackageConfig.version,
stats: stats
};
console.log(svelteDocParserPackageConfig.version);
console.log(artifactOutput);
glob('output/*.json', (e, prevArtifactPathList) => {
const historyArtifacts = prevArtifactPathList.map(artifactPath => {
return JSON.parse(fs.readFileSync(artifactPath));
}).reduce((acc, artifact) => {
acc[artifact.packageVersion] = artifact;
return acc;
}, {});
historyArtifacts[artifactOutput.packageVersion] = artifactOutput;
const statsTable = renderStatsTable(Object.values(historyArtifacts).reverse());
console.log(statsTable);
fs.writeFileSync('output/overview.md', statsTable);
});
fs.mkdirSync('output', { recursive: true });
fs.writeFileSync(`output/${svelteDocParserPackageConfig.version}.json`, JSON.stringify(artifactOutput));
}
(async () => {
let filesHandled = 0;
let bytesHandled = 0;
let totalExecutionTimeInMs = 0;
let handledWithError = 0;
let typeScriptFilesCount = 0;
glob(GLOB_FILTER, async (e, files) => {
let batch = [];
for (let index = 0; index < files.length; index++) {
const filePath = files[index];
const fileContent = fs.readFileSync(filePath).toString();
if (!fileContent.includes('lang=\"ts\"') && !fileContent.includes('lang\'ts\'')) {
batch.push({
path: filePath,
content: fileContent
});
} else {
typeScriptFilesCount++;
}
if (batch.length > 30 || index === files.length - 1) {
const batchExecutionTimeStart = performance.now();
const tasks = batch.map(async (item) => {
try {
const output = await parse({
fileContent: item.content,
version: 3,
});
if (!output) {
//writeLog(`Failed [${item.path}] with message: Empty output`);
handledWithError++;
}
} catch(e) {
//writeLog(`Failed [${item.path}] with message: (${e.name}) ${e.message}`);
handledWithError++;
}
});
await Promise.all(tasks);
const batchExecutionTimeEnd = performance.now();
const batchDuration = batchExecutionTimeEnd - batchExecutionTimeStart;
const batchSize = batch.reduce((acc, item) => acc + item.content.length, 0);
writeLog(`Batch handled with ${batch.length} files (${batchSize} B) within ${batchDuration} ms (Avg.: ${(batchDuration / batch.length).toFixed(3)} ms) with speed ${(batchSize / batchDuration).toFixed(3)} B/ms`);
totalExecutionTimeInMs += batchDuration;
bytesHandled += batchSize;
filesHandled += batch.length;
batch = [];
}
}
writeLog(`Totally completed ${filesHandled} (of ${files.length}) and with ${handledWithError} errors (${(handledWithError / filesHandled * 100).toFixed(1)}%)!`);
writeLog(`Typescript files found ${typeScriptFilesCount}`);
writeLog(`All handled with ${filesHandled} files (${bytesHandled} B) within ${totalExecutionTimeInMs} ms (Avg.: ${(totalExecutionTimeInMs / filesHandled).toFixed(3)} ms) with speed ${(bytesHandled / totalExecutionTimeInMs).toFixed(3)}B/ms`);
handleTestStats({
totalFilesCount: files.length,
totalTypeScriptFilesCount: typeScriptFilesCount,
totalHandledFilesCount: filesHandled,
errorsCount: handledWithError,
totalExecutionTimeInMs: totalExecutionTimeInMs,
bytesHandled: bytesHandled
});
});
})();