server/benchmarks/timelines.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-11-30 05:16:58 +01:00
/**
* Usage: TOKEN=your_token_here bun benchmark:timeline <request_count>
*/
import chalk from "chalk";
2024-04-07 06:16:54 +02:00
import { config } from "config-manager";
2023-11-30 05:16:58 +01:00
const token = process.env.TOKEN;
const requestCount = Number(process.argv[2]) || 100;
if (!token) {
2024-04-07 07:30:49 +02:00
console.log(
`${chalk.red(
"✗",
)} No token provided. Provide one via the TOKEN environment variable.`,
);
process.exit(1);
2023-11-30 05:16:58 +01:00
}
const fetchTimeline = () =>
2024-04-08 05:28:18 +02:00
fetch(new URL("/api/v1/timelines/home", config.http.base_url), {
2024-04-07 07:30:49 +02:00
headers: {
Authorization: `Bearer ${token}`,
},
}).then((res) => res.ok);
2023-11-30 05:16:58 +01:00
const timeNow = performance.now();
const requests = Array.from({ length: requestCount }, () => fetchTimeline());
Promise.all(requests)
2024-04-07 07:30:49 +02:00
.then((results) => {
const timeTaken = performance.now() - timeNow;
if (results.every((t) => t)) {
console.log(`${chalk.green("✓")} All requests succeeded`);
} else {
console.log(
`${chalk.red("✗")} ${
results.filter((t) => !t).length
} requests failed`,
);
}
console.log(
`${chalk.green("✓")} ${
requests.length
} requests fulfilled in ${chalk.bold(
(timeTaken / 1000).toFixed(5),
)}s`,
);
})
.catch((err) => {
console.log(`${chalk.red("✗")} ${err}`);
process.exit(1);
});