Last active
November 27, 2021 11:38
-
-
Save silverwind/a7a702e05b3a69578f58 to your computer and use it in GitHub Desktop.
TCP_NODELAY test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const net = require("net"); | |
const port = 4000; | |
var time, times = []; | |
function now() { | |
let hrtime = process.hrtime(); | |
return hrtime[0] * 1e9 + hrtime[1]; | |
} | |
net.createServer(function(socket) { | |
let i = 0; | |
socket.setNoDelay(true); | |
time = now(); | |
while (++i <= 80) { | |
socket.write("."); | |
} | |
}).listen(port); | |
setInterval(function() { | |
let socket = new net.Socket(); | |
socket.on("data", function (data) { | |
console.log(data.toString()); | |
times.push(Math.round((now() - time) / 1000)); | |
socket.end(); | |
}).connect(port); | |
}, 100); | |
process.on("SIGINT", function () { | |
console.log("\n" + times.length + " data events, average delay: " + | |
Math.round(times.reduce(function (a, b) { | |
return a + b; | |
}) / times.length) + "µs"); | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment