Skip to content

Commit

Permalink
feat(examples/benchmarks): add suite for pushing events to individual…
Browse files Browse the repository at this point in the history
… sessions
  • Loading branch information
MatthewWid committed Jan 15, 2025
1 parent 735ab1a commit 87dbc35
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 16 deletions.
3 changes: 2 additions & 1 deletion examples/benchmarks/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {suite as suiteChannelBroadcast} from "./suites/channel-broadcast";
import {suite as suiteSessionPush} from "./suites/session-push";

Promise.all([suiteChannelBroadcast.setup()]).then((suites) => {
Promise.all([suiteSessionPush.setup()]).then((suites) => {
for (const suite of suites) {
suite.run();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/benchmarks/lib/createEventSource.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventSource } from "eventsource";
import {EventSource} from "eventsource";

const createEventSource = (port: number, path = "/sse") => {
const url = `http://localhost:${port}${path}`;
Expand All @@ -10,4 +10,4 @@ const createEventSource = (port: number, path = "/sse") => {
});
};

export { createEventSource };
export {createEventSource};
20 changes: 9 additions & 11 deletions examples/benchmarks/suites/channel-broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

import { createChannel, createSession } from "better-sse";
import {createChannel, createSession} from "better-sse";
// @ts-ignore
import EasySse from "easy-server-sent-events";
// @ts-ignore
import SseChannel from "sse-channel";
import { createClientPool } from "../lib/createClientPool";
import { Suite } from "./Suite";
import {createClientPool} from "../lib/createClientPool";
import {Suite} from "./Suite";

export const suite = new Suite("Broadcast events with channels", async () => {
const numberOfClients = 10;

await suite.addBenchmark("better-sse", async (server, port, listen) => {
let count = 0;

const channel = createChannel();

server.get("/sse", async (req, res) => {
Expand All @@ -22,6 +18,8 @@ export const suite = new Suite("Broadcast events with channels", async () => {

await listen();

let count = 0;

return {
run: () => {
channel.broadcast(++count);
Expand All @@ -33,7 +31,7 @@ export const suite = new Suite("Broadcast events with channels", async () => {
await suite.addBenchmark("sse-channel", async (server, port, listen) => {
let count = 0;

const channel = new SseChannel({ jsonEncode: true });
const channel = new SseChannel({jsonEncode: true});

server.get("/sse", (req, res) => {
channel.addClient(req, res);
Expand All @@ -58,9 +56,7 @@ export const suite = new Suite("Broadcast events with channels", async () => {
await suite.addBenchmark(
"easy-server-sent-events",
async (server, port, listen) => {
let count = 0;

const { SSE, send } = EasySse({ endpoint: "/sse" });
const {SSE, send} = EasySse({endpoint: "/sse"});

server.get("/sse", (req, res, next) => {
SSE(req, res, next);
Expand All @@ -69,6 +65,8 @@ export const suite = new Suite("Broadcast events with channels", async () => {

await listen();

let count = 0;

return {
run: () => {
send("all", "message", ++count);
Expand Down
98 changes: 98 additions & 0 deletions examples/benchmarks/suites/session-push.ts
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(),
};
});
});
33 changes: 32 additions & 1 deletion examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"express": "^4.21.1",
"node-os-utils": "^1.3.7",
"pem": "^1.14.8",
"sse": "^0.0.8",
"sse-channel": "^4.0.0",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0"
Expand All @@ -24,6 +25,7 @@
"@types/express": "^5.0.0",
"@types/node": "^20.16.12",
"@types/node-os-utils": "^1.3.4",
"@types/pem": "^1.14.4"
"@types/pem": "^1.14.4",
"@types/sse": "^0.0.0"
}
}

0 comments on commit 87dbc35

Please sign in to comment.