-
-
Save zousandian/390d03d2ca6655131376cf59fb2a2c03 to your computer and use it in GitHub Desktop.
TCP_NODELAY test
This file contains hidden or 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