Skip to content

Instantly share code, notes, and snippets.

@anton-ryzhov
Created March 23, 2012 20:38
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 anton-ryzhov/2174719 to your computer and use it in GitHub Desktop.
Save anton-ryzhov/2174719 to your computer and use it in GitHub Desktop.
node.js async test
Files:
mkfifo pipe pipe2
pv -L 3M /dev/zero > pipe
pv -L 3M /dev/zero > pipe2
ps -AL | grep node
Socket:
pv -L 1M /dev/zero | nc -l 1234
var lastcall, done,
fs = require('fs');
function showDate(s)
{
var now = new Date() - 0;
lastcall = lastcall ? lastcall : now;
console.log(s + ': ' + new Date() + ', ' + ' +' + (now - lastcall) + ' ' + (process.memoryUsage().rss/1024/1024) + ' Mb' + '\n');
lastcall = now;
}
function sleep(n)
{
var goal = new Date() - 0 + n*1000;
while ((new Date() - 0) < goal);
}
showDate('start');
done = 0;
fs.readFile('pipe', function (err, data) {
showDate('end read');
console.log('filelen ' + data.length + '\n');
done = 1;
});
fs.readFile('pipe2', function (err, data) {
showDate('end read2');
console.log('filelen ' + data.length + '\n');
done = 1;
});
showDate('start read');
function echo()
{
console.log(new Date() - 0);
if (!done)
setTimeout(echo, 500);
}
setTimeout(echo, 0);
var lastcall, done,
net = require('net');
function showDate(s)
{
var now = new Date() - 0;
lastcall = lastcall ? lastcall : now;
console.log(s + ': ' + new Date() + ', ' + ' +' + (now - lastcall) + ' ' + (process.memoryUsage().rss/1024/1024) + ' Mb' + '\n');
lastcall = now;
}
function sleep(n)
{
var goal = new Date() - 0 + n*1000;
while ((new Date() - 0) < goal);
}
showDate('start');
done = 0; len = 0; count = 0;
var client = net.createConnection(1234, function() { //'connect' listener
console.log('client connected');
//client.bufferSize = 5000;
});
client.on('data', function(data) {
len += data.toString().length;
count +=1;
});
client.on('end', function() {
console.log('client disconnected');
done = 1;
});
showDate('start read');
sleep(5)
function echo()
{
console.log(new Date() - 0);
console.log(count + ': ' + len);
count = 0; len = 0;
if (!done)
setTimeout(echo, 10);
}
setTimeout(echo, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment