-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples/benchmarks): add suite for pushing events to individual…
… sessions
- Loading branch information
1 parent
735ab1a
commit 87dbc35
Showing
6 changed files
with
146 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import {createServer as createRawHttpServer} from "node:http"; | ||
import {type Session, createSession} from "better-sse"; | ||
import type {Response as ExpressResponse} from "express"; | ||
import SSE, {type Client} from "sse"; | ||
// @ts-ignore | ||
import SseChannel from "sse-channel"; | ||
import {createEventSource} from "../lib/createEventSource"; | ||
import {Suite} from "./Suite"; | ||
|
||
export const suite = new Suite("Push events with sessions", async () => { | ||
await suite.addBenchmark("better-sse", async (server, port, listen) => { | ||
let session: Session; | ||
|
||
server.get("/sse", async (req, res) => { | ||
session = await createSession(req, res); | ||
}); | ||
|
||
await listen(); | ||
|
||
const eventSource = await createEventSource(port); | ||
|
||
let count = 0; | ||
|
||
return { | ||
run: () => { | ||
session.push(++count); | ||
}, | ||
teardown: () => eventSource.close(), | ||
}; | ||
}); | ||
|
||
await suite.addBenchmark("sse-channel", async (server, port, listen) => { | ||
const channel = new SseChannel({jsonEncode: true}); | ||
|
||
let res: ExpressResponse; | ||
|
||
server.get("/sse", (req, _res) => { | ||
res = _res; | ||
channel.addClient(req, res); | ||
}); | ||
|
||
await listen(); | ||
|
||
const eventSource = await createEventSource(port); | ||
|
||
let count = 0; | ||
|
||
return { | ||
run: () => { | ||
++count; | ||
|
||
channel.send( | ||
{ | ||
event: "message", | ||
data: count, | ||
id: count, | ||
}, | ||
[res] | ||
); | ||
}, | ||
teardown: () => eventSource.close(), | ||
}; | ||
}); | ||
|
||
await suite.addBenchmark("sse", async (server) => { | ||
const port = ++Suite.port; | ||
|
||
// `sse` package cannot attach to an Express instance directly, so wrap with a raw Node HTTP server | ||
const wrapper = createRawHttpServer(server); | ||
|
||
const sse = new SSE(wrapper); | ||
|
||
let client: Client; | ||
|
||
sse.on("connection", (_client) => { | ||
client = _client; | ||
}); | ||
|
||
await new Promise<void>((resolve) => wrapper.listen(port, () => resolve())); | ||
|
||
const eventSource = await createEventSource(port); | ||
|
||
let count = 0; | ||
|
||
return { | ||
run: () => { | ||
const stringified = (count++).toString(); | ||
|
||
client.send({ | ||
event: "message", | ||
data: stringified, | ||
id: stringified, | ||
}); | ||
}, | ||
teardown: () => eventSource.close(), | ||
}; | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters