forked from highlightjs/highlight.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf.js
executable file
·70 lines (62 loc) · 1.78 KB
/
perf.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
#!/usr/bin/env node
const execSync = require('child_process').execSync;
const fs = require('fs');
const { performance } = require('perf_hooks');
const build = () => {
console.log(`Starting perf tests, building hljs ... `);
// build node.js version of library with CJS and ESM libraries
execSync('npm run build', {
cwd: '.',
env: Object.assign(
process.env
)
});
};
const timeTest = (name, func) => {
process.stdout.write(` running ${name}...`);
const t0 = performance.now();
func();
const t1 = performance.now();
console.log(` done! [${((t1 - t0) / 1000).toFixed(2)}s elapsed]`);
};
const oneLanguageMarkupTests = (lang) => {
for (let i = 0; i < 50; i++) {
execSync('npx mocha ./test/markup', {
cwd: '.',
env: Object.assign(
process.env,
{ ONLY_LANGUAGES: lang }
)
});
}
};
const oneLanguageCheckAutoDetect = (lang) => {
for (let i = 0; i < 50; i++) {
execSync('node ./tools/checkAutoDetect.js', {
env: Object.assign(
process.env,
{ ONLY_LANGUAGES: lang }
)
});
}
};
const globalCheckAutoDetect = () => {
for (let i = 0; i < 5; i++) {
execSync('node ./tools/checkAutoDetect.js');
}
};
const highlightFile = (lang) => {
const source = fs.readFileSync(`./tools/sample_files/${lang}.txt`, { encoding: 'utf8' });
const hljs = require('../build.js');
for (let i = 0; i < 2000; i++) {
hljs.highlight(source, { language: lang });
}
};
const main = (lang) => {
build();
timeTest(`global checkAutoDetect`, globalCheckAutoDetect);
timeTest(`${lang}-only markup tests`, () => oneLanguageMarkupTests(lang));
timeTest(`${lang}-only checkAutoDetect`, () => oneLanguageCheckAutoDetect(lang));
timeTest(`highlight large file`, () => highlightFile(lang));
};
main('python');