diff options
author | 2022-10-25 00:44:25 -0700 | |
---|---|---|
committer | 2022-10-25 00:44:25 -0700 | |
commit | 02c920f4fd09ddc1a32cb2e92c6f391875415949 (patch) | |
tree | 4f524f5ce9d672fadb8740c68cc9b1a8410a714f /bench/snippets | |
parent | 1b50ecc52b55df0c00f991c8206d4ced84ad89b8 (diff) | |
download | bun-02c920f4fd09ddc1a32cb2e92c6f391875415949.tar.gz bun-02c920f4fd09ddc1a32cb2e92c6f391875415949.tar.zst bun-02c920f4fd09ddc1a32cb2e92c6f391875415949.zip |
TCP & TLS Socket API (#1374)
* TCP Socket API
* Wip
* Add snippet for StringDecoder
* Rename `close` to `stop`, replace `close` with `end`
* Add a tcp echo server test
* Some docs
* Update README.md
* Fix build
* Update README.md
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'bench/snippets')
-rw-r--r-- | bench/snippets/string-decoder.mjs | 28 | ||||
-rw-r--r-- | bench/snippets/tcp-echo.bun.ts | 49 | ||||
-rw-r--r-- | bench/snippets/tcp-echo.node.mjs | 52 |
3 files changed, 129 insertions, 0 deletions
diff --git a/bench/snippets/string-decoder.mjs b/bench/snippets/string-decoder.mjs new file mode 100644 index 000000000..8f7cb31fb --- /dev/null +++ b/bench/snippets/string-decoder.mjs @@ -0,0 +1,28 @@ +import { bench, run } from "mitata"; +import { StringDecoder } from "string_decoder"; + +var short = Buffer.from("Hello World!"); +var shortUTF16 = Buffer.from("Hello World 💕💕💕"); +var long = Buffer.from("Hello World!".repeat(1024)); +var longUTF16 = Buffer.from("Hello World 💕💕💕".repeat(1024)); +bench(`${short.length} ascii`, () => { + var decoder = new StringDecoder(); + decoder.write(short); +}); + +bench(`${short.length} utf8`, () => { + var decoder = new StringDecoder(); + decoder.write(shortUTF16); +}); + +bench(`${long.length} ascii`, () => { + var decoder = new StringDecoder(); + decoder.write(long); +}); + +bench(`${longUTF16.length} utf8`, () => { + var decoder = new StringDecoder(); + decoder.write(longUTF16); +}); + +await run(); diff --git a/bench/snippets/tcp-echo.bun.ts b/bench/snippets/tcp-echo.bun.ts new file mode 100644 index 000000000..34250d65c --- /dev/null +++ b/bench/snippets/tcp-echo.bun.ts @@ -0,0 +1,49 @@ +import { listen, connect } from "bun"; + +var counter = 0; +const msg = "Hello World!"; + +const handlers = { + open(socket) { + if (!socket.data?.isServer) { + if (!socket.write(msg)) { + socket.data = { pending: msg }; + } + } + }, + data(socket, buffer) { + if (!socket.write(buffer)) { + socket.data = { pending: buffer }; + return; + } + counter++; + }, + drain(socket) { + const pending = socket.data?.pending; + if (!pending) return; + if (socket.write(pending)) { + socket.data = undefined; + counter++; + return; + } + }, +}; + +setInterval(() => { + console.log("Wrote", counter, "messages"); + counter = 0; +}, 1000); + +const server = listen({ + socket: handlers, + hostname: "localhost", + port: 8080, + data: { + isServer: true, + }, +}); +const connection = await connect({ + socket: handlers, + hostname: "localhost", + port: 8080, +}); diff --git a/bench/snippets/tcp-echo.node.mjs b/bench/snippets/tcp-echo.node.mjs new file mode 100644 index 000000000..3362b5a3c --- /dev/null +++ b/bench/snippets/tcp-echo.node.mjs @@ -0,0 +1,52 @@ +import { createRequire } from "node:module"; +const net = createRequire(import.meta.url)("net"); + +const buffer = Buffer.from("Hello World!"); +var counter = 0; +const handlers = { + open() { + if (!socket.data?.isServer) { + if (!this.write(buffer)) { + socket.data = { pending: buffer }; + } + } + }, + data(buffer) { + if (!this.write(buffer)) { + this.data = { pending: buffer.slice() }; + return; + } + counter++; + }, + drain() { + const pending = this.data?.pending; + if (!pending) return; + if (this.write(pending)) { + this.data = undefined; + counter++; + return; + } + }, +}; + +const server = net.createServer(function (socket) { + socket.data = { isServer: true }; + socket.on("connection", handlers.open.bind(socket)); + socket.on("data", handlers.data.bind(socket)); + socket.on("drain", handlers.drain.bind(socket)); + socket.setEncoding("binary"); +}); + +setInterval(() => { + console.log("Wrote", counter, "messages"); + counter = 0; +}, 1000); + +server.listen(8000); + +const socket = net.connect({ host: "localhost", port: 8000 }, () => {}); +socket.on("connection", handlers.open.bind(socket)); +socket.on("data", handlers.data.bind(socket)); +socket.on("drain", handlers.drain.bind(socket)); +socket.setEncoding("binary"); +socket.write(buffer); |