Skip to content

Commit

Permalink
Merge pull request #32 from MatthewWid/benchmarks
Browse files Browse the repository at this point in the history
Benchmarks setup
  • Loading branch information
MatthewWid authored Jan 15, 2022
2 parents 78e699e + c8fbd83 commit 0251ca2
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This section compares Better SSE with other server-sent events libraries for Nod

Better SSE was designed to be the easiest and most powerful library for using server-sent events, better than all other existing solutions.

## Features

TL/DR: Better SSE supports all\* the features that other competing libraries do and much more. It is TypeScript-first (but obviously can be used with JavaScript), very well tested, easier to scale and much more flexible than others whilst *still* having a simpler programming interface for both simple and complex use cases.

|Feature|[`better-sse`](https://www.npmjs.com/package/better-sse)|[`sse-channel`](https://www.npmjs.com/package/sse-channel)|[`sse`](https://www.npmjs.com/package/sse)|[`express-sse`](https://www.npmjs.com/package/express-sse)|[`server-sent-events`](https://www.npmjs.com/package/server-sent-events)|[`easy-server-sent-events`](https://www.npmjs.com/package/easy-server-sent-events)|[`sse-stream`](https://www.npmjs.com/package/sse-stream)|
Expand All @@ -23,3 +25,7 @@ TL/DR: Better SSE supports all\* the features that other competing libraries do
|Modify response status code||||||||

\* Except event history maintenance... [Coming soon](https://github.com/MatthewWid/better-sse/issues/16)!

## Performance

You can run benchmarks yourself that compare Better SSE with other libraries in the [`benchmarks` project](../examples) in the `examples` directory.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ This directory showcases examples of using Better SSE.
- [Channels](./channels)
- [Streams](./streams)
- [System Resource Monitor](./resource-monitor)
- [Benchmarks](./benchmarks)
85 changes: 85 additions & 0 deletions examples/benchmarks/benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import express, {Express} from "express";
import EventSource from "eventsource";
import {Suite} from "benchmark";
import {createSession, createChannel} from "better-sse";
// @ts-ignore
import SseChannel from "sse-channel";

(async () => {
const name = "Push events with channels.";

const suite = new Suite(name, {
async: true,
});

suite.on("start", () => {
console.log(`Benchmarking "${name}".`);
});

suite.on("cycle", (event: Event) => {
console.log(String(event.target));
});

suite.on("complete", () => {
process.exit(0);
});

await Promise.all([
(async () => {
let count = 0;
let server: Express;
const port = 8000;
const channel = createChannel();

suite.add("better-sse", () => {
channel.broadcast(++count);
});

server = express();

server.get("/", async (req, res) => {
const session = await createSession(req, res);

channel.register(session);
});

await new Promise<void>((resolve) => server.listen(port, resolve));

new EventSource(`http://localhost:${port}`);

await new Promise((resolve) =>
channel.once("session-registered", resolve)
);
})(),
(async () => {
let count = 0;
let server: Express;
const port = 8010;
const channel = new SseChannel({jsonEncode: true});

suite.add("sse-channel", () => {
++count;

channel.send({
event: "message",
data: count,
id: count,
});
});

server = express();

server.get("/", (req, res) => {
channel.addClient(req, res);
});

await new Promise<void>((resolve) => server.listen(port, resolve));

new EventSource(`http://localhost:${port}`);

await new Promise((resolve) => channel.once("connect", resolve));
})(),
]);

suite.run();
})();
1 change: 1 addition & 0 deletions examples/benchmarks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./benchmark";
63 changes: 63 additions & 0 deletions examples/package-lock.json

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

3 changes: 3 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
"start": "ts-node $INIT_CWD/index.ts"
},
"dependencies": {
"benchmark": "^2.1.4",
"better-sse": "^0.7.1",
"eventsource": "^1.1.0",
"express": "^4.17.1",
"node-os-utils": "^1.3.5",
"sse-channel": "^4.0.0",
"ts-node": "^10.4.0",
"ts-node-dev": "^1.1.8"
},
"devDependencies": {
"@types/benchmark": "^2.1.1",
"@types/express": "^4.17.11",
"@types/node": "^14.14.20",
"@types/node-os-utils": "^1.2.0"
Expand Down

0 comments on commit 0251ca2

Please sign in to comment.