aboutsummaryrefslogtreecommitdiff
path: root/bench/snippets/tcp-echo.node.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'bench/snippets/tcp-echo.node.mjs')
-rw-r--r--bench/snippets/tcp-echo.node.mjs114
1 files changed, 58 insertions, 56 deletions
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);