Skip to content

Commit 9b5f3a7

Browse files
authored
refactor: use vitest for benchmarks (withastro#12605)
* refactor: use `vitest` for benchmarks * increase timeout * increase timeout * Restore correct label
1 parent 3072287 commit 9b5f3a7

17 files changed

+324
-147
lines changed

.github/workflows/continuous_benchmark.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
uses: CodSpeedHQ/action@513a19673a831f139e8717bf45ead67e47f00044 # v3.2.0
5151
timeout-minutes: 30
5252
with:
53-
run: pnpm benchmark codspeed
53+
working-directory: ./benchmark
54+
run: pnpm bench
5455
token: ${{ secrets.CODSPEED_TOKEN }}
5556

benchmark/bench/_util.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ export function calculateStat(numbers) {
1919
const max = Math.max(...numbers);
2020
return { avg, stdev, max };
2121
}
22+
23+
export async function makeProject(name) {
24+
console.log('Making project:', name);
25+
const projectDir = new URL(`../projects/${name}/`, import.meta.url);
26+
27+
const makeProjectMod = await import(`../make-project/${name}.js`);
28+
await makeProjectMod.run(projectDir);
29+
30+
console.log('Finished making project:', name);
31+
return projectDir;
32+
}

benchmark/bench/codspeed.bench.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { fileURLToPath } from 'node:url';
2+
import { exec } from 'tinyexec';
3+
import { beforeAll, bench, describe } from 'vitest';
4+
import { astroBin, makeProject } from './_util.js';
5+
let streamingApp;
6+
let nonStreamingApp;
7+
beforeAll(async () => {
8+
const render = await makeProject('render-bench');
9+
const root = fileURLToPath(render);
10+
await exec(astroBin, ['build'], {
11+
nodeOptions: {
12+
cwd: root,
13+
stdio: 'inherit',
14+
},
15+
});
16+
const entry = new URL('./dist/server/entry.mjs', `file://${root}`);
17+
const { manifest, createApp } = await import(entry);
18+
streamingApp = createApp(manifest, true);
19+
nonStreamingApp = createApp(manifest, false);
20+
}, 900000);
21+
22+
describe('Bench rendering', () => {
23+
bench('Rendering: streaming [true], .astro file', async () => {
24+
const request = new Request(new URL('http://exmpale.com/astro'));
25+
await streamingApp.render(request);
26+
});
27+
bench('Rendering: streaming [true], .md file', async () => {
28+
const request = new Request(new URL('http://exmpale.com/md'));
29+
await streamingApp.render(request);
30+
});
31+
bench('Rendering: streaming [true], .mdx file', async () => {
32+
const request = new Request(new URL('http://exmpale.com/mdx'));
33+
await streamingApp.render(request);
34+
});
35+
36+
bench('Rendering: streaming [false], .astro file', async () => {
37+
const request = new Request(new URL('http://exmpale.com/astro'));
38+
await nonStreamingApp.render(request);
39+
});
40+
bench('Rendering: streaming [false], .md file', async () => {
41+
const request = new Request(new URL('http://exmpale.com/md'));
42+
await nonStreamingApp.render(request);
43+
});
44+
bench('Rendering: streaming [false], .mdx file', async () => {
45+
const request = new Request(new URL('http://exmpale.com/mdx'));
46+
await nonStreamingApp.render(request);
47+
});
48+
});

benchmark/bench/codspeed.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

benchmark/index.js

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import mri from 'mri';
22
import fs from 'node:fs/promises';
33
import path from 'node:path';
4-
import {fileURLToPath, pathToFileURL} from 'node:url';
4+
import { fileURLToPath, pathToFileURL } from 'node:url';
5+
import { makeProject } from './bench/_util.js';
56

67
const args = mri(process.argv.slice(2));
78

@@ -14,7 +15,6 @@ Command
1415
memory Run build memory and speed test
1516
render Run rendering speed test
1617
server-stress Run server stress test
17-
codspeed Run codspeed test
1818
cli-startup Run CLI startup speed test
1919
2020
Options
@@ -30,7 +30,6 @@ const benchmarks = {
3030
render: () => import('./bench/render.js'),
3131
'server-stress': () => import('./bench/server-stress.js'),
3232
'cli-startup': () => import('./bench/cli-startup.js'),
33-
codspeed: () => import('./bench/codspeed.js')
3433
};
3534

3635
if (commandName && !(commandName in benchmarks)) {
@@ -39,26 +38,12 @@ if (commandName && !(commandName in benchmarks)) {
3938
}
4039

4140
if (commandName) {
42-
if (commandName === 'codspeed') {
43-
const render = await makeProject('render-bench');
44-
const rootRender = fileURLToPath(render);
45-
const bench = benchmarks[commandName];
46-
const benchMod = await bench();
47-
const payload = {
48-
render: {
49-
root: rootRender,
50-
output: await getOutputFile('render')
51-
},
52-
};
53-
await benchMod.run(payload);
54-
} else {
55-
// Run single benchmark
56-
const bench = benchmarks[commandName];
57-
const benchMod = await bench();
58-
const projectDir = await makeProject(args.project || benchMod.defaultProject);
59-
const outputFile = await getOutputFile(commandName);
60-
await benchMod.run(projectDir, outputFile);
61-
}
41+
// Run single benchmark
42+
const bench = benchmarks[commandName];
43+
const benchMod = await bench();
44+
const projectDir = await makeProject(args.project || benchMod.defaultProject);
45+
const outputFile = await getOutputFile(commandName);
46+
await benchMod.run(projectDir, outputFile);
6247
} else {
6348
// Run all benchmarks
6449
for (const name in benchmarks) {
@@ -70,21 +55,10 @@ if (commandName) {
7055
}
7156
}
7257

73-
export async function makeProject(name) {
74-
console.log('Making project:', name);
75-
const projectDir = new URL(`./projects/${name}/`, import.meta.url);
76-
77-
const makeProjectMod = await import(`./make-project/${name}.js`);
78-
await makeProjectMod.run(projectDir);
79-
80-
console.log('Finished making project:', name);
81-
return projectDir;
82-
}
83-
8458
/**
8559
* @param {string} benchmarkName
8660
*/
87-
async function getOutputFile(benchmarkName) {
61+
export async function getOutputFile(benchmarkName) {
8862
let file;
8963
if (args.output) {
9064
file = pathToFileURL(path.resolve(args.output));

benchmark/make-project/markdown-cc1.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export async function run(projectDir) {
88
await fs.rm(projectDir, { recursive: true, force: true });
99
await fs.mkdir(new URL('./src/pages/blog', projectDir), { recursive: true });
1010
await fs.mkdir(new URL('./src/content/blog', projectDir), { recursive: true });
11-
await fs.copyFile(new URL('./image.jpg', import.meta.url), new URL('./src/image.jpg', projectDir));
11+
await fs.copyFile(
12+
new URL('./image.jpg', import.meta.url),
13+
new URL('./src/image.jpg', projectDir),
14+
);
1215

1316
const promises = [];
1417

15-
1618
for (let i = 0; i < 10000; i++) {
1719
const content = `\
1820
# Article ${i}
@@ -24,11 +26,10 @@ ${loremIpsumMd}
2426
2527
`;
2628
promises.push(
27-
fs.writeFile(new URL(`./src/content/blog/article-${i}.md`, projectDir), content, 'utf-8')
29+
fs.writeFile(new URL(`./src/content/blog/article-${i}.md`, projectDir), content, 'utf-8'),
2830
);
2931
}
3032

31-
3233
await fs.writeFile(
3334
new URL(`./src/pages/blog/[...slug].astro`, projectDir),
3435
`\
@@ -46,7 +47,7 @@ const { Content } = await entry.render();
4647
<h1>{entry.data.title}</h1>
4748
<Content />
4849
`,
49-
'utf-8'
50+
'utf-8',
5051
);
5152

5253
await Promise.all(promises);
@@ -58,6 +59,6 @@ import { defineConfig } from 'astro/config';
5859
5960
export default defineConfig({
6061
});`,
61-
'utf-8'
62+
'utf-8',
6263
);
6364
}

benchmark/make-project/markdown-cc2.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ${loremIpsumMd}
2323
2424
`;
2525
promises.push(
26-
fs.writeFile(new URL(`./data/blog/article-${i}.md`, projectDir), content, 'utf-8')
26+
fs.writeFile(new URL(`./data/blog/article-${i}.md`, projectDir), content, 'utf-8'),
2727
);
2828
}
2929

@@ -39,7 +39,7 @@ ${loremIpsumMd}
3939
4040
export const collections = { blog }
4141
42-
`
42+
`,
4343
);
4444

4545
await fs.writeFile(
@@ -60,7 +60,7 @@ const { Content } = await render(entry);
6060
<h1>{entry.data.title}</h1>
6161
<Content />
6262
`,
63-
'utf-8'
63+
'utf-8',
6464
);
6565

6666
await Promise.all(promises);
@@ -71,6 +71,6 @@ const { Content } = await render(entry);
7171
import { defineConfig } from 'astro/config';
7272
7373
export default defineConfig({});`,
74-
'utf-8'
74+
'utf-8',
7575
);
7676
}

benchmark/make-project/mdx-cc1.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export async function run(projectDir) {
88
await fs.rm(projectDir, { recursive: true, force: true });
99
await fs.mkdir(new URL('./src/pages/blog', projectDir), { recursive: true });
1010
await fs.mkdir(new URL('./src/content/blog', projectDir), { recursive: true });
11-
await fs.copyFile(new URL('./image.jpg', import.meta.url), new URL('./src/image.jpg', projectDir));
11+
await fs.copyFile(
12+
new URL('./image.jpg', import.meta.url),
13+
new URL('./src/image.jpg', projectDir),
14+
);
1215

1316
const promises = [];
1417

15-
1618
for (let i = 0; i < 10000; i++) {
1719
const content = `\
1820
# Article ${i}
@@ -24,11 +26,10 @@ ${loremIpsumMd}
2426
2527
`;
2628
promises.push(
27-
fs.writeFile(new URL(`./src/content/blog/article-${i}.mdx`, projectDir), content, 'utf-8')
29+
fs.writeFile(new URL(`./src/content/blog/article-${i}.mdx`, projectDir), content, 'utf-8'),
2830
);
2931
}
3032

31-
3233
await fs.writeFile(
3334
new URL(`./src/pages/blog/[...slug].astro`, projectDir),
3435
`\
@@ -46,7 +47,7 @@ const { Content } = await entry.render();
4647
<h1>{entry.data.title}</h1>
4748
<Content />
4849
`,
49-
'utf-8'
50+
'utf-8',
5051
);
5152

5253
await Promise.all(promises);
@@ -61,6 +62,6 @@ import mdx from '@astrojs/mdx';
6162
export default defineConfig({
6263
integrations: [mdx()],
6364
});`,
64-
'utf-8'
65+
'utf-8',
6566
);
6667
}

benchmark/make-project/mdx-cc2.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ${loremIpsumMd}
2323
2424
`;
2525
promises.push(
26-
fs.writeFile(new URL(`./data/blog/article-${i}.mdx`, projectDir), content, 'utf-8')
26+
fs.writeFile(new URL(`./data/blog/article-${i}.mdx`, projectDir), content, 'utf-8'),
2727
);
2828
}
2929

@@ -39,7 +39,7 @@ ${loremIpsumMd}
3939
4040
export const collections = { blog }
4141
42-
`
42+
`,
4343
);
4444

4545
await fs.writeFile(
@@ -60,7 +60,7 @@ const { Content } = await render(entry);
6060
<h1>{entry.data.title}</h1>
6161
<Content />
6262
`,
63-
'utf-8'
63+
'utf-8',
6464
);
6565

6666
await Promise.all(promises);
@@ -75,6 +75,6 @@ import mdx from '@astrojs/mdx';
7575
export default defineConfig({
7676
integrations: [mdx()],
7777
});`,
78-
'utf-8'
78+
'utf-8',
7979
);
8080
}

0 commit comments

Comments
 (0)