feat: Add timeline benchmark

This commit is contained in:
Jesse Wierzbinski 2023-11-29 18:16:58 -10:00
parent 15ef1851fc
commit df5e8f744b
No known key found for this signature in database
4 changed files with 74 additions and 1 deletions

View file

@ -23,10 +23,25 @@ This project aims to be a fully featured social network, with a focus on privacy
- [x] Full regex-based filters for posts, users and media
- [x] Custom emoji support
- [x] Automatic image conversion to WebP or other formats
- [x] Scripting-compatible CLI with JSON and CSV outputs
- [ ] Moderation tools
- [ ] Full Mastodon API support
- [ ] Outbound federation
## Benchmarks
> **Note**: These benchmarks are not representative of real-world performance, and are only meant to be used as a rough guide.
### Timeline Benchmarks
You may run the following command to benchmark the `/api/v1/timelines/home` endpoint:
```bash
TOKEN=token_here bun benchmark:timeline <request_count>
```
The `request_count` variable is optional and defaults to 100. `TOKEN` is your personal user token, used to login to the API.
## How do I run it?
### Requirements

56
benchmarks/timelines.ts Normal file
View file

@ -0,0 +1,56 @@
/**
* Usage: TOKEN=your_token_here bun benchmark:timeline <request_count>
*/
import { getConfig } from "@config";
import chalk from "chalk";
const config = getConfig();
const token = process.env.TOKEN;
const requestCount = Number(process.argv[2]) || 100;
if (!token) {
console.log(
`${chalk.red(
"✗"
)} No token provided. Provide one via the TOKEN environment variable.`
);
process.exit(1);
}
const fetchTimeline = () =>
fetch(`${config.http.base_url}/api/v1/timelines/home`, {
headers: {
Authorization: `Bearer ${token}`,
},
}).then(res => res.ok);
const timeNow = performance.now();
const requests = Array.from({ length: requestCount }, () => fetchTimeline());
Promise.all(requests)
.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);
});

View file

@ -12,6 +12,7 @@ import { client } from "~database/datasource";
import type { PrismaClientInitializationError } from "@prisma/client/runtime/library";
import { HookTypes, Server } from "~plugins/types";
const timeAtStart = performance.now();
const server = new Server();
const router = new Bun.FileSystemRouter({
@ -171,7 +172,7 @@ console.log(
`${chalk.green(``)} ${chalk.bold(
`Lysand started at ${chalk.blue(
`${config.http.bind}:${config.http.bind_port}`
)}`
)} in ${chalk.gray((performance.now() - timeAtStart).toFixed(0))}ms`
)}`
);

View file

@ -39,6 +39,7 @@
"lint": "eslint --config .eslintrc.cjs --ext .ts .",
"prisma": "bun run prisma.ts",
"generate": "bun prisma generate",
"benchmark:timeline": "bun run benchmarks/timelines.ts",
"cli": "bun run cli.ts"
},
"trustedDependencies": [