forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-samples.js
More file actions
73 lines (62 loc) · 2.07 KB
/
parse-samples.js
File metadata and controls
73 lines (62 loc) · 2.07 KB
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
/* eslint-disable no-console */
import klawSync from "klaw-sync";
import path from "path";
import fs from "fs";
import javaParser from "../src/index";
import _ from "lodash";
const options = {
failFast: false,
printProgress: false,
printErrors: true
};
let printProgress = _.noop;
let printErrors = _.noop;
if (options.printProgress) {
printProgress = console.log;
}
if (options.printErrors) {
printErrors = console.error;
}
// const samplesDir = path.resolve(__dirname, "../samples/java-design-patterns");
const samplesDir = path.resolve(__dirname, "../samples/spring-boot");
const sampleFiles = klawSync(samplesDir, { nodir: true });
const javaSampleFiles = sampleFiles.filter(fileDesc =>
fileDesc.path.endsWith(".java")
);
const javaPathAndText = _.map(javaSampleFiles, fileDesc => {
const currJavaFileString = fs.readFileSync(fileDesc.path, "utf8");
const relativePath = path.relative(__dirname, fileDesc.path);
return { path: relativePath, text: currJavaFileString };
});
let success = 0;
let failed = 0;
const fullStartTime = new Date().getTime();
javaPathAndText.forEach(fileDesc => {
// TODO: read the files BEFORE the benchmark started to only bench the parsing speed...
const relativePath = fileDesc.path;
try {
const sampleStartTime = _.now();
javaParser.parse(fileDesc.text);
const sampleEndTime = _.now();
const totalSampleTime = sampleEndTime - sampleStartTime;
printProgress(
`Success parsing: <${relativePath}> - <${totalSampleTime}ms>`
);
success++;
} catch (e) {
printErrors(`Failed parsing: <${relativePath}>`);
if (options.failFast) {
throw e;
}
failed++;
printErrors(e.message);
}
});
const fullEndTime = new Date().getTime();
const fullTotalTime = fullEndTime - fullStartTime;
const totalFiles = success + failed;
const successPercentage = ((success / totalFiles) * 100).toFixed(3);
console.log(`Total number of files: <${totalFiles}>`);
console.warn(`Total number of failures: <${failed}>`);
console.log(`Total time: <${fullTotalTime}ms>`);
console.log(`Success Percentage: ${successPercentage}%`);