Skip to content

Commit aac290b

Browse files
authored
[Elysia] add compiled configuration, use cluster mode, optimize performance (#9251)
* feat(elysia): use cluster mode, static resource, add compiled mode * feat(elysia): use imperative for-loop instead of map to optimize performance
1 parent a329357 commit aac290b

12 files changed

+133
-102
lines changed

frameworks/TypeScript/elysia/benchmark_config.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@
6262
"display_name": "Elysia [smol] [PostgreSQL]",
6363
"notes": "",
6464
"versus": "nodejs"
65+
},
66+
"compiled": {
67+
"json_url": "/json",
68+
"plaintext_url": "/plaintext",
69+
"db_url": "/db",
70+
"query_url": "/queries?queries=",
71+
"update_url": "/updates?queries=",
72+
"fortune_url": "/fortunes",
73+
"port": 8080,
74+
"approach": "Realistic",
75+
"classification": "Micro",
76+
"database": "Postgres",
77+
"framework": "elysia",
78+
"language": "TypeScript",
79+
"flavor": "None",
80+
"orm": "Raw",
81+
"platform": "bun",
82+
"webserver": "None",
83+
"os": "Linux",
84+
"database_os": "Linux",
85+
"display_name": "Elysia [Compiled]",
86+
"notes": "",
87+
"versus": "nodejs"
6588
}
6689
}
6790
]
-1.42 KB
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM oven/bun:1.1
2+
3+
EXPOSE 8080
4+
5+
COPY . .
6+
7+
ENV NODE_ENV production
8+
9+
RUN bun install --production
10+
11+
ENV DATABASE postgres
12+
13+
RUN bun run compile
14+
15+
CMD ["./server"]

frameworks/TypeScript/elysia/elysia-postgres.dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ ENV NODE_ENV production
88

99
RUN bun install --production
1010

11-
RUN bun run build
12-
1311
ENV DATABASE postgres
1412

15-
CMD ["bun", "spawn.ts"]
13+
RUN bun run build
14+
15+
CMD ["bun", "./dist/index.js"]

frameworks/TypeScript/elysia/elysia-smol-postgres.dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ COPY . .
66

77
ENV NODE_ENV production
88

9-
RUN bun install --production
10-
11-
RUN bun run build
9+
RUN bun install
1210

1311
ENV DATABASE postgres
1412

13+
RUN bun run build
14+
1515
RUN sed -i 's/smol = false/smol = true/g' bunfig.toml
1616

17-
CMD ["bun", "spawn.ts"]
17+
CMD ["bun", "./dist/index.js"]

frameworks/TypeScript/elysia/elysia.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ RUN bun install --production
1010

1111
RUN bun run build
1212

13-
CMD ["bun", "spawn.ts"]
13+
CMD ["bun", "./dist/index.js"]

frameworks/TypeScript/elysia/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
"version": "0.0.1",
44
"module": "src/index.js",
55
"devDependencies": {
6-
"bun-types": "^1.1.23",
76
"typescript": "^5.5.4"
87
},
98
"scripts": {
109
"dev": "bun run --watch src/index.ts",
1110
"start": "bun run src/index.ts",
12-
"build": "bun build --compile --minify --outfile server src/index.ts"
11+
"build": "bun build --minify --target bun --outdir dist src/index.ts",
12+
"compile": "bun build --compile --minify --target bun --outfile server src/index.ts"
1313
},
1414
"dependencies": {
15-
"elysia": "^1.1.6",
15+
"elysia": "^1.1.12",
1616
"postgres": "^3.4.4"
1717
}
18-
}
18+
}

frameworks/TypeScript/elysia/spawn.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,63 @@
1-
import Elysia from 'elysia';
2-
import * as db from './postgres';
3-
import { Fortune } from './types';
1+
import { Elysia, t } from "elysia";
2+
import * as db from "./postgres";
3+
import { Fortune } from "./types";
44

5-
function rand () {
6-
return Math.ceil(Math.random() * 10000)
5+
function rand() {
6+
return Math.ceil(Math.random() * 10000);
77
}
88

9-
function parseQueriesNumber (q?: string) {
10-
return Math.min(parseInt(q || '1') || 1, 500)
9+
function parseQueriesNumber(q?: string) {
10+
return Math.min(parseInt(q || "1") || 1, 500);
1111
}
1212

13-
function renderTemplate (fortunes: Fortune[]) {
13+
function renderTemplate(fortunes: Fortune[]) {
1414
const n = fortunes.length;
1515

16-
let html = '';
16+
let html = "";
1717
for (let i = 0; i < n; i++) {
1818
html += `<tr><td>${fortunes[i].id}</td><td>${Bun.escapeHTML(
19-
fortunes[i].message
19+
fortunes[i].message,
2020
)}</td></tr>`;
2121
}
2222

2323
return `<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>${html}</table></body></html>`;
2424
}
2525

26-
const dbHandlers = new Elysia({
27-
name: 'db-handlers',
28-
})
29-
.onAfterHandle(({ set }) => {
30-
set.headers['server'] = 'Elysia';
26+
export const dbHandlers = new Elysia()
27+
.headers({
28+
server: "Elysia",
3129
})
32-
33-
.get('/db', () => db.find(rand()))
34-
35-
.get('/fortunes', async ({ set }) => {
30+
.get("/db", () => db.find(rand()))
31+
.get("/fortunes", async (c) => {
3632
const fortunes = await db.fortunes();
3733

3834
fortunes.push({
3935
id: 0,
40-
message: 'Additional fortune added at request time.',
36+
message: "Additional fortune added at request time.",
37+
});
38+
39+
fortunes.sort((a, b) => {
40+
if (a.message < b.message) return -1;
41+
42+
return 1;
4143
});
4244

43-
fortunes.sort((a, b) => (a.message < b.message ? -1 : 1));
45+
c.set.headers["content-type"] = "text/html; charset=utf-8";
4446

45-
set.headers['content-type'] = 'text/html; charset=utf-8';
4647
return renderTemplate(fortunes);
4748
})
48-
49-
.get('/queries', async ({ query }) => {
50-
const num = parseQueriesNumber(query.queries)
49+
.get("/queries", (c) => {
50+
const num = parseQueriesNumber(c.query.queries);
5151
const worldPromises = new Array(num);
5252

5353
for (let i = 0; i < num; i++) {
5454
worldPromises[i] = db.find(rand());
5555
}
5656

57-
return await Promise.all(worldPromises);
57+
return Promise.all(worldPromises);
5858
})
59-
60-
.get('/updates', async ({ query }) => {
61-
const num = parseQueriesNumber(query.queries)
59+
.get("/updates", async (c) => {
60+
const num = parseQueriesNumber(c.query.queries);
6261
const worldPromises = new Array(num);
6362

6463
for (let i = 0; i < num; i++) {
@@ -74,5 +73,3 @@ const dbHandlers = new Elysia({
7473
await db.bulkUpdate(worlds);
7574
return worlds;
7675
});
77-
78-
export default dbHandlers;
Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
import { Elysia } from 'elysia';
2-
import dbHandlers from './db-handlers';
1+
import cluster from "node:cluster";
2+
import os from "node:os";
3+
import process from "node:process";
34

4-
const app = new Elysia({
5-
serve: {
6-
reusePort: true,
7-
},
8-
})
9-
.get('/plaintext', ({ set }) => {
10-
set.headers['server'] = 'Elysia';
11-
return 'Hello, World!';
12-
})
5+
if (cluster.isPrimary) {
6+
console.log(`Primary ${process.pid} is running`);
137

14-
.get('/json', ({ set }) => {
15-
set.headers = {
16-
'content-type': 'application/json',
17-
'server': 'Elysia',
18-
};
8+
const numCPUs = os.availableParallelism();
9+
for (let i = 0; i < numCPUs; i++) {
10+
cluster.fork();
11+
}
1912

20-
return JSON.stringify({ message: 'Hello, World!' });
13+
cluster.on("exit", (worker) => {
14+
console.log(`worker ${worker.process.pid} died`);
15+
process.exit(1);
2116
});
22-
23-
if (Bun.env.DATABASE) {
24-
app.use(dbHandlers);
17+
} else {
18+
await import("./server");
19+
console.log(`Worker ${process.pid} started`);
2520
}
26-
27-
app.listen(8080);
28-
29-
console.info(`🦊 Elysia is running at ${app.server!.url}`);

0 commit comments

Comments
 (0)