Skip to content

Instantly share code, notes, and snippets.

@peheje
Created April 4, 2017 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peheje/248bde863998f97c6de17d0ccadfbbed to your computer and use it in GitHub Desktop.
Save peheje/248bde863998f97c6de17d0ccadfbbed to your computer and use it in GitHub Desktop.
Node.js client-server timings.
const http = require("http");
const NanoTimer = require("nanotimer"); // Documentation: https://github.com/Krb686/nanotimer
function getNanoTime() {
const t = process.hrtime(); // Documentation: https://nodejs.org/api/process.html#process_process_hrtime_time
return t[0] * 1e9 + t[1]; // [seconds, nanoseconds]
}
const options = {
hostname: '127.0.0.1',
port: 3000,
path: '/'
};
const start_time_ns = getNanoTime();
const timer = new NanoTimer();
const total_requests = 1e6;
const max_pending_requests = 1e4;
let requests_left = total_requests;
let request_counter = 0;
let pending_request = 0;
console.log("Client PID: " + process.pid);
timer.setInterval(() => {
if (request_counter === total_requests) {
timer.clearInterval();
return;
}
if (pending_request === max_pending_requests) {
// max_pending exeeded, waiting for next interval
return;
}
if (request_counter % 1e4 === 0) {
console.log("request_counter", request_counter);
}
request_counter++;
pending_request++;
http.get(options, res => {
pending_request--;
requests_left--;
if (requests_left < 1) {
const duration_ns = getNanoTime() - start_time_ns;
console.log("requests_left", requests_left);
console.log("Stopping. " + total_requests + " requests took " + duration_ns / (1e6) + " ms");
}
}).on("error", err => {
console.log("error");
});
}, "", "1u");
/*
Limitations to TCP (I think) required me to change WAIT_TIME please read here.
http://stackoverflow.com/questions/1216267/ab-program-freezes-after-lots-of-requests-why
On MacOS I ran:
sysctl -w net.inet.ip.portrange.first=32768
sysctl -w net.inet.tcp.msl=1000
*/
const http = require("http");
const cluster = require("cluster");
const numInstances = 4;
const hostname = "127.0.0.1";
const port = 3000;
if (cluster.isMaster) {
for (let i = 0; i < numInstances; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log("worker with id: " + worker.process.pid + " died");
});
} else {
const server = http.createServer((req, res) => {
//console.log("worker with id: " + process.pid + " handles request");
res.statusCode = 200;
res.setHeader("Content-Type", "text/html");
res.end("<p>hello, world!</p>");
});
server.listen(port, hostname, () => {
console.log("Server PID: " + process.pid + " running at http://" + hostname + ":" + port + "/");
});
server.on("error", err => {
console.log(err);
});
}
process.on("uncaughtException", () => {
console.log(err);
});
@peheje
Copy link
Author

peheje commented Apr 4, 2017

I'm not sure I'm testing anything meaningful. I'm not claiming this is scientific test. Related to this thread: http://stackoverflow.com/questions/9290160/node-js-vs-net-performance#

Please comment if you see any errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment