Skip to content

Instantly share code, notes, and snippets.

@silverwind

silverwind/nodelay.js

Last active Aug 29, 2015
Embed
What would you like to do?
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