Skip to content

Commit

Permalink
feat: Add timeline benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed Nov 30, 2023
1 parent 15ef185 commit df5e8f7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions benchmarks/timelines.ts
Original file line number Diff line number Diff line change
@@ -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);
});
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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`
)}`
);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

0 comments on commit df5e8f7

Please sign in to comment.