diff options
Diffstat (limited to 'benchmark/bench')
-rw-r--r-- | benchmark/bench/_util.js | 2 | ||||
-rw-r--r-- | benchmark/bench/cli-startup.js | 11 | ||||
-rw-r--r-- | benchmark/bench/codspeed.js | 50 | ||||
-rw-r--r-- | benchmark/bench/memory.js | 7 | ||||
-rw-r--r-- | benchmark/bench/render.js | 14 | ||||
-rw-r--r-- | benchmark/bench/server-stress.js | 23 |
6 files changed, 81 insertions, 26 deletions
diff --git a/benchmark/bench/_util.js b/benchmark/bench/_util.js index 23c472604..b16a16e1c 100644 --- a/benchmark/bench/_util.js +++ b/benchmark/bench/_util.js @@ -14,7 +14,7 @@ export const astroBin = path.resolve(astroPkgPath, '../astro.js'); export function calculateStat(numbers) { const avg = numbers.reduce((a, b) => a + b, 0) / numbers.length; const stdev = Math.sqrt( - numbers.map((x) => Math.pow(x - avg, 2)).reduce((a, b) => a + b, 0) / numbers.length + numbers.map((x) => Math.pow(x - avg, 2)).reduce((a, b) => a + b, 0) / numbers.length, ); const max = Math.max(...numbers); return { avg, stdev, max }; diff --git a/benchmark/bench/cli-startup.js b/benchmark/bench/cli-startup.js index 6f2555499..9144797d7 100644 --- a/benchmark/bench/cli-startup.js +++ b/benchmark/bench/cli-startup.js @@ -1,6 +1,6 @@ import { fileURLToPath } from 'node:url'; -import { exec } from 'tinyexec'; import { markdownTable } from 'markdown-table'; +import { exec } from 'tinyexec'; import { astroBin, calculateStat } from './_util.js'; /** Default project to run for this benchmark if not specified */ @@ -8,9 +8,8 @@ export const defaultProject = 'render-default'; /** * @param {URL} projectDir - * @param {URL} outputFile */ -export async function run(projectDir, outputFile) { +export async function run(projectDir) { const root = fileURLToPath(projectDir); console.log('Benchmarking `astro --help`...'); @@ -28,7 +27,7 @@ export async function run(projectDir, outputFile) { printResult({ 'astro --help': helpStat, 'astro info': infoStat, - }) + }), ); console.log('='.repeat(10)); } @@ -45,7 +44,7 @@ async function benchmarkCommand(command, args, root) { for (let i = 0; i < 10; i++) { const start = performance.now(); - await exec(command, args, { nodeOptions: { cwd: root } }); + await exec(command, args, { nodeOptions: { cwd: root }, throwOnError: true }); durations.push(performance.now() - start); } @@ -69,6 +68,6 @@ function printResult(result) { ], { align: ['l', 'r', 'r', 'r'], - } + }, ); } diff --git a/benchmark/bench/codspeed.js b/benchmark/bench/codspeed.js new file mode 100644 index 000000000..2ad783e8a --- /dev/null +++ b/benchmark/bench/codspeed.js @@ -0,0 +1,50 @@ +import path from 'node:path'; +import { withCodSpeed } from '@codspeed/tinybench-plugin'; +import { Bench } from 'tinybench'; +import { exec } from 'tinyexec'; +import { renderPages } from '../make-project/render-default.js'; +import { astroBin } from './_util.js'; + +export async function run({ memory: _memory, render, stress: _stress }) { + const options = { + iterations: 10, + }; + const bench = process.env.CODSPEED ? withCodSpeed(new Bench(options)) : new Bench(options); + let app; + bench.add( + 'Rendering', + async () => { + console.info('Start task.'); + const result = {}; + for (const fileName of renderPages) { + const pathname = '/' + fileName.slice(0, -path.extname(fileName).length); + const request = new Request(new URL(pathname, 'http://exmpale.com')); + const response = await app.render(request); + const html = await response.text(); + if (!result[pathname]) result[pathname] = []; + result[pathname].push(html); + } + console.info('Finish task.'); + return result; + }, + { + async beforeAll() { + // build for rendering + await exec(astroBin, ['build'], { + nodeOptions: { + cwd: render.root, + stdio: 'inherit', + }, + }); + + const entry = new URL('./dist/server/entry.mjs', `file://${render.root}`); + const { manifest, createApp } = await import(entry); + app = createApp(manifest); + app.manifest = manifest; + }, + }, + ); + + await bench.run(); + console.table(bench.table()); +} diff --git a/benchmark/bench/memory.js b/benchmark/bench/memory.js index f1846204f..4f9153cc0 100644 --- a/benchmark/bench/memory.js +++ b/benchmark/bench/memory.js @@ -1,7 +1,7 @@ -import { exec } from 'tinyexec'; -import { markdownTable } from 'markdown-table'; import fs from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; +import { markdownTable } from 'markdown-table'; +import { exec } from 'tinyexec'; import { astroBin } from './_util.js'; /** @typedef {Record<string, import('../../packages/astro/src/core/config/timer').Stat>} AstroTimerStat */ @@ -26,6 +26,7 @@ export async function run(projectDir, outputFile) { ASTRO_TIMER_PATH: outputFilePath, }, }, + throwOnError: true, }); console.log('Raw results written to', outputFilePath); @@ -55,6 +56,6 @@ function printResult(output) { ], { align: ['l', 'r', 'r', 'r'], - } + }, ); } diff --git a/benchmark/bench/render.js b/benchmark/bench/render.js index aee04f2b5..02f75a73b 100644 --- a/benchmark/bench/render.js +++ b/benchmark/bench/render.js @@ -1,12 +1,12 @@ -import { exec } from 'tinyexec'; -import { markdownTable } from 'markdown-table'; import fs from 'node:fs/promises'; import http from 'node:http'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; +import { markdownTable } from 'markdown-table'; import { waitUntilBusy } from 'port-authority'; -import { calculateStat, astroBin } from './_util.js'; +import { exec } from 'tinyexec'; import { renderPages } from '../make-project/render-default.js'; +import { astroBin, calculateStat } from './_util.js'; const port = 4322; @@ -25,6 +25,7 @@ export async function run(projectDir, outputFile) { cwd: root, stdio: 'inherit', }, + throwOnError: true, }); console.log('Previewing...'); @@ -33,6 +34,7 @@ export async function run(projectDir, outputFile) { cwd: root, stdio: 'inherit', }, + throwOnError: true, }); console.log('Waiting for server ready...'); @@ -58,14 +60,14 @@ export async function run(projectDir, outputFile) { console.log('Done!'); } -async function benchmarkRenderTime() { +export async function benchmarkRenderTime(portToListen = port) { /** @type {Record<string, number[]>} */ const result = {}; for (const fileName of renderPages) { // Render each file 100 times and push to an array for (let i = 0; i < 100; i++) { const pathname = '/' + fileName.slice(0, -path.extname(fileName).length); - const renderTime = await fetchRenderTime(`http://localhost:${port}${pathname}`); + const renderTime = await fetchRenderTime(`http://localhost:${portToListen}${pathname}`); if (!result[pathname]) result[pathname] = []; result[pathname].push(renderTime); } @@ -95,7 +97,7 @@ function printResult(result) { ], { align: ['l', 'r', 'r', 'r'], - } + }, ); } diff --git a/benchmark/bench/server-stress.js b/benchmark/bench/server-stress.js index 18b31c71c..5bcaa6963 100644 --- a/benchmark/bench/server-stress.js +++ b/benchmark/bench/server-stress.js @@ -1,10 +1,10 @@ -import autocannon from 'autocannon'; -import { exec } from 'tinyexec'; -import { markdownTable } from 'markdown-table'; import fs from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; +import autocannon from 'autocannon'; +import { markdownTable } from 'markdown-table'; import { waitUntilBusy } from 'port-authority'; import pb from 'pretty-bytes'; +import { exec } from 'tinyexec'; import { astroBin } from './_util.js'; const port = 4321; @@ -24,12 +24,15 @@ export async function run(projectDir, outputFile) { cwd: root, stdio: 'inherit', }, + throwOnError: true, }); console.log('Previewing...'); - const previewProcess = execaCommand(`${astroBin} preview --port ${port}`, { - cwd: root, - stdio: 'inherit', + const previewProcess = await exec(astroBin, ['preview', '--port', port], { + nodeOptions: { + cwd: root, + stdio: 'inherit', + }, }); console.log('Waiting for server ready...'); @@ -58,7 +61,7 @@ export async function run(projectDir, outputFile) { /** * @returns {Promise<import('autocannon').Result>} */ -async function benchmarkCannon() { +export async function benchmarkCannon() { return new Promise((resolve, reject) => { const instance = autocannon( { @@ -75,7 +78,7 @@ async function benchmarkCannon() { instance.stop(); resolve(result); } - } + }, ); autocannon.track(instance, { renderResultsTable: false }); }); @@ -94,7 +97,7 @@ function printResult(output) { ], { align: ['l', 'r', 'r', 'r'], - } + }, ); const reqAndBytesTable = markdownTable( @@ -105,7 +108,7 @@ function printResult(output) { ], { align: ['l', 'r', 'r', 'r', 'r'], - } + }, ); return `${latencyTable}\n\n${reqAndBytesTable}`; |