lau1944 / bunrest

An express-like API for bun server
MIT License
285 stars 31 forks source link

Add reusePort to listen SSLOptions #43

Closed MikkelSVDK closed 7 months ago

MikkelSVDK commented 8 months ago

This allows bunrest to function seamlessly in multithreaded workloads, leveraging the capability provided by Bun.spawn().

// spawn.ts
import { availableParallelism } from 'node:os'
import process from 'node:process'

const numCPUs = availableParallelism();

for (let i = 0; i < numCPUs; i++) {
  Bun.spawn(["bun", "index.ts"], {
    stdio: ["inherit", "inherit", "inherit"],
    env: { ...process.env },
  });
}
// index.ts
import server from 'bunrest'
const app = server()

app.get('/', (req, res) => {
  res.status(200).send('Hello, World!');
})

app.listen(3000, () => {
  console.log('App is listening on port 3000');
}, {
  reusePort: true
})
MikkelSVDK commented 8 months ago

Single instance

Running 10s test @ http://127.0.0.1:3000/
  2 threads and 20 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   160.32us  102.41us   6.45ms   91.88%
    Req/Sec    65.21k     3.65k   78.82k    64.85%
  1310390 requests in 10.10s, 161.21MB read
Requests/sec: 129740.12
Transfer/sec:     15.96MB

Multi

Running 10s test @ http://127.0.0.1:3000/
  2 threads and 20 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   106.91us  293.05us  14.91ms   95.28%
    Req/Sec   141.60k     8.43k  164.20k    77.50%
  2816621 requests in 10.00s, 346.51MB read
Requests/sec: 281655.17
Transfer/sec:     34.65MB