Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@silverwind
Last active November 27, 2021 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save silverwind/a7a702e05b3a69578f58 to your computer and use it in GitHub Desktop.
Save silverwind/a7a702e05b3a69578f58 to your computer and use it in GitHub Desktop.
TCP_NODELAY test
"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