diff options
author | 2023-01-04 16:09:22 -0800 | |
---|---|---|
committer | 2023-01-04 18:55:16 -0800 | |
commit | f41bb3fb20e448f7bf96b58239687cd31a92be9d (patch) | |
tree | c22312aec29a577cf5d5fe66e8a40b4bfbece283 /bench/snippets | |
parent | e0557d70e35377537eeffad6f15c057ecf997a3e (diff) | |
download | bun-f41bb3fb20e448f7bf96b58239687cd31a92be9d.tar.gz bun-f41bb3fb20e448f7bf96b58239687cd31a92be9d.tar.zst bun-f41bb3fb20e448f7bf96b58239687cd31a92be9d.zip |
split server/client for tcp echo benchmark to better measure net.Socket perf
Diffstat (limited to 'bench/snippets')
-rw-r--r-- | bench/snippets/tcp-echo.bun.ts | 4 | ||||
-rw-r--r-- | bench/snippets/tcp-echo.node.mjs | 114 |
2 files changed, 60 insertions, 58 deletions
diff --git a/bench/snippets/tcp-echo.bun.ts b/bench/snippets/tcp-echo.bun.ts index 34250d65c..b2a8c26e6 100644 --- a/bench/snippets/tcp-echo.bun.ts +++ b/bench/snippets/tcp-echo.bun.ts @@ -36,7 +36,7 @@ setInterval(() => { const server = listen({ socket: handlers, - hostname: "localhost", + hostname: "0.0.0.0", port: 8080, data: { isServer: true, @@ -44,6 +44,6 @@ const server = listen({ }); const connection = await connect({ socket: handlers, - hostname: "localhost", + hostname: "0.0.0.0", port: 8080, }); diff --git a/bench/snippets/tcp-echo.node.mjs b/bench/snippets/tcp-echo.node.mjs index a854787cf..336f9d5e2 100644 --- a/bench/snippets/tcp-echo.node.mjs +++ b/bench/snippets/tcp-echo.node.mjs @@ -29,66 +29,68 @@ const handlers = { }, }; -if (net.createServer) { - 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"); - }); +if (process.env.IS_SERVER) { + if (net.createServer) { + 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); + setInterval(() => { + console.log("Wrote", counter, "messages"); + counter = 0; + }, 1000); - server.listen(8000); -} else { - const handlers = { - open(socket) { - if (!socket.data?.isServer) { - if (!socket.write(msg)) { - socket.data = { pending: msg }; + server.listen(8000); + } else { + 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; } - } - }, - 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; - } - }, - }; + }, + 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); + setInterval(() => { + console.log("Wrote", counter, "messages"); + counter = 0; + }, 1000); - const server = Bun.listen({ - socket: handlers, - hostname: "0.0.0.0", - port: 8000, - data: { - isServer: true, - }, - }); + const server = Bun.listen({ + socket: handlers, + hostname: "0.0.0.0", + port: 8000, + data: { + isServer: true, + }, + }); + } +} else { + const socket = net.connect({ host: "0.0.0.0", 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); } - -const socket = net.connect({ host: "0.0.0.0", 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); |