2023-07-17 19:17:59 -05:00
|
|
|
import fs from 'node:fs/promises';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
2024-11-06 07:33:14 -05:00
|
|
|
import autocannon from 'autocannon';
|
|
|
|
import { markdownTable } from 'markdown-table';
|
2023-07-17 19:17:59 -05:00
|
|
|
import { waitUntilBusy } from 'port-authority';
|
2023-03-02 22:55:31 -05:00
|
|
|
import pb from 'pretty-bytes';
|
2024-11-06 07:33:14 -05:00
|
|
|
import { exec } from 'tinyexec';
|
2023-03-01 03:46:06 -05:00
|
|
|
import { astroBin } from './_util.js';
|
|
|
|
|
|
|
|
const port = 4321;
|
|
|
|
|
|
|
|
export const defaultProject = 'server-stress-default';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {URL} projectDir
|
|
|
|
* @param {URL} outputFile
|
|
|
|
*/
|
|
|
|
export async function run(projectDir, outputFile) {
|
|
|
|
const root = fileURLToPath(projectDir);
|
|
|
|
|
|
|
|
console.log('Building...');
|
2024-08-28 09:52:49 -05:00
|
|
|
await exec(astroBin, ['build'], {
|
|
|
|
nodeOptions: {
|
|
|
|
cwd: root,
|
|
|
|
stdio: 'inherit',
|
|
|
|
},
|
2024-11-04 09:49:33 -05:00
|
|
|
throwOnError: true,
|
2023-03-01 03:46:06 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
console.log('Previewing...');
|
2024-11-06 07:33:14 -05:00
|
|
|
const previewProcess = await exec(astroBin, ['preview', '--port', port], {
|
|
|
|
nodeOptions: {
|
|
|
|
cwd: root,
|
|
|
|
stdio: 'inherit',
|
|
|
|
},
|
2023-03-01 03:46:06 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
console.log('Waiting for server ready...');
|
|
|
|
await waitUntilBusy(port, { timeout: 5000 });
|
|
|
|
|
|
|
|
console.log('Running benchmark...');
|
|
|
|
const result = await benchmarkCannon();
|
|
|
|
|
|
|
|
console.log('Killing server...');
|
|
|
|
if (!previewProcess.kill('SIGTERM')) {
|
|
|
|
console.warn('Failed to kill server process id:', previewProcess.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Writing results to', fileURLToPath(outputFile));
|
|
|
|
await fs.writeFile(outputFile, JSON.stringify(result, null, 2));
|
|
|
|
|
|
|
|
console.log('Result preview:');
|
|
|
|
console.log('='.repeat(10));
|
|
|
|
console.log(`#### Server stress\n\n`);
|
2023-03-02 22:55:31 -05:00
|
|
|
console.log(printResult(result));
|
2023-03-01 03:46:06 -05:00
|
|
|
console.log('='.repeat(10));
|
|
|
|
|
|
|
|
console.log('Done!');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Promise<import('autocannon').Result>}
|
|
|
|
*/
|
2024-11-06 07:33:14 -05:00
|
|
|
export async function benchmarkCannon() {
|
2023-03-01 03:46:06 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const instance = autocannon(
|
|
|
|
{
|
|
|
|
url: `http://localhost:${port}`,
|
|
|
|
connections: 100,
|
|
|
|
duration: 30,
|
|
|
|
pipelining: 10,
|
|
|
|
},
|
|
|
|
(err, result) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
// @ts-expect-error untyped but documented
|
|
|
|
instance.stop();
|
|
|
|
resolve(result);
|
|
|
|
}
|
2024-11-06 07:33:14 -05:00
|
|
|
},
|
2023-03-01 03:46:06 -05:00
|
|
|
);
|
|
|
|
autocannon.track(instance, { renderResultsTable: false });
|
|
|
|
});
|
|
|
|
}
|
2023-03-02 22:55:31 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import('autocannon').Result} output
|
|
|
|
*/
|
|
|
|
function printResult(output) {
|
|
|
|
const { latency: l, requests: r, throughput: t } = output;
|
|
|
|
|
|
|
|
const latencyTable = markdownTable(
|
|
|
|
[
|
|
|
|
['', 'Avg', 'Stdev', 'Max'],
|
|
|
|
['Latency', `${l.average} ms`, `${l.stddev} ms`, `${l.max} ms`],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
align: ['l', 'r', 'r', 'r'],
|
2024-11-06 07:33:14 -05:00
|
|
|
},
|
2023-03-02 22:55:31 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
const reqAndBytesTable = markdownTable(
|
|
|
|
[
|
|
|
|
['', 'Avg', 'Stdev', 'Min', 'Total in 30s'],
|
|
|
|
['Req/Sec', r.average, r.stddev, r.min, `${(r.total / 1000).toFixed(1)}k requests`],
|
|
|
|
['Bytes/Sec', pb(t.average), pb(t.stddev), pb(t.min), `${pb(t.total)} read`],
|
|
|
|
],
|
|
|
|
{
|
|
|
|
align: ['l', 'r', 'r', 'r', 'r'],
|
2024-11-06 07:33:14 -05:00
|
|
|
},
|
2023-03-02 22:55:31 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
return `${latencyTable}\n\n${reqAndBytesTable}`;
|
|
|
|
}
|