Skip to content

Commit 62e64d5

Browse files
committed
Add performance testing scripts
1 parent b3587eb commit 62e64d5

File tree

4 files changed

+1792
-9
lines changed

4 files changed

+1792
-9
lines changed

test/markup/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,23 @@ describe('highlight() markup', async() => {
4646
const markupPath = utility.buildPath('markup');
4747

4848
if (!process.env.ONLY_EXTRA) {
49-
const languages = await fs.readdir(markupPath);
49+
let languages;
50+
if (process.env.ONLY_LANGUAGES) {
51+
languages = JSON.parse(process.env.ONLY_LANGUAGES);
52+
} else {
53+
languages = await fs.readdir(markupPath);
54+
}
5055
languages.forEach(testLanguage);
5156
}
5257

53-
const thirdPartyPackages = await getThirdPartyPackages();
54-
thirdPartyPackages.forEach(
55-
(pkg) => pkg.names.forEach(
56-
(name, idx) => testLanguage(name, { testDir: pkg.markupTestPaths[idx] })
57-
)
58-
);
58+
if (!process.env.ONLY_LANGUAGES) {
59+
const thirdPartyPackages = await getThirdPartyPackages();
60+
thirdPartyPackages.forEach(
61+
(pkg) => pkg.names.forEach(
62+
(name, idx) => testLanguage(name, { testDir: pkg.markupTestPaths[idx] })
63+
)
64+
);
65+
}
5966
});
6067

6168
it("adding dynamic tests...", async function() {}); // this is required to work

tools/checkAutoDetect.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ function testAutoDetection(language, index, languages) {
5858
});
5959
}
6060

61-
const languages = hljs.listLanguages()
62-
.filter(hljs.autoDetection);
61+
let languages;
62+
if (process.env.ONLY_LANGUAGES) {
63+
languages = JSON.parse(process.env.ONLY_LANGUAGES);
64+
} else {
65+
languages = hljs.listLanguages().filter(hljs.autoDetection);
66+
}
6367

6468
console.log('Checking auto-highlighting for ' + colors.grey(languages.length) + ' languages...');
6569
languages.forEach((lang, index) => {

tools/perf.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const execSync = require('child_process').execSync;
2+
const fs = require('fs');
3+
const { performance } = require('perf_hooks');
4+
5+
const timeTest = (name, func) => {
6+
process.stdout.write(`Starting ${name}, building hljs ... `);
7+
// execSync('npm run build', {'cwd': '..'});
8+
process.stdout.write(` running ...`);
9+
let t0 = performance.now();
10+
func()
11+
var t1 = performance.now();
12+
console.log(` done! [${((t1 - t0) / 1000).toFixed(2)}s elapsed]`);
13+
}
14+
15+
const oneLanguageMarkupTests = (lang) => {
16+
for (let i = 0; i < 100; i++) {
17+
execSync('npx mocha test/markup', {
18+
'cwd': '..',
19+
'env': Object.assign(
20+
process.env,
21+
{'ONLY_LANGUAGES': JSON.stringify([lang])}
22+
)
23+
});
24+
}
25+
}
26+
27+
const oneLanguageCheckAutoDetect = (lang) => {
28+
for (let i = 0; i < 100; i++) {
29+
execSync('node checkAutoDetect.js', {
30+
'env': Object.assign(
31+
process.env,
32+
{'ONLY_LANGUAGES': JSON.stringify([lang])}
33+
)
34+
});
35+
}
36+
}
37+
38+
const globalCheckAutoDetect = () => {
39+
for (let i = 0; i < 10; i++) {
40+
execSync('node checkAutoDetect.js');
41+
}
42+
}
43+
44+
const highlightFile = (lang) => {
45+
const source = fs.readFileSync(`./sample_files/${lang}.txt`, { encoding:'utf8' });
46+
const hljs = require('../build');
47+
for (let i = 0; i < 2000; i++) {
48+
hljs.highlight(source, {'language': lang});
49+
}
50+
}
51+
52+
const main = (lang, fileURL) => {
53+
timeTest(`${lang}-only markup tests`, () => oneLanguageMarkupTests(lang));
54+
timeTest(`${lang}-only checkAutoDetect`, () => oneLanguageCheckAutoDetect(lang));
55+
timeTest(`global checkAutoDetect`, globalCheckAutoDetect);
56+
timeTest(`highlight large file`, () => highlightFile(lang));
57+
}
58+
59+
main('python')

0 commit comments

Comments
 (0)