public
Created

This is a test to evaluate the speed of log4js compared to a straight write into a file stream.

  • Download Gist
testLoggerSpeed.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/*jslint node: true, maxlen: 100 */
'use strict';
 
var fs = require('fs'),
log4js = require('log4js'),
async = require('async'),
 
NB_ITERATION = 150000;
 
function fileStreamSpeedTest(callback) {
console.log('---------- fileStreamSpeedTest ----------');
 
var filename = 'fstream.log',
counter = NB_ITERATION;
 
fs.unlink(filename, function () {
var d1,
stream,
toWrite;
 
stream = fs.createWriteStream(
filename,
{ encoding: 'utf8', mode: 420, flags: 'a' }
);
stream.on('close', function () {
var diff3 = Date.now() - d1;
console.log('onClose: Time elapsed in writing file: ' + diff3 + 'ms');
 
// Now we are really done.
callback(null);
});
 
stream.on('drain', function () {
while (counter) {
var toWrite = '[' + new Date() + '] ' +
'[INFO] server - Message[' + counter + ']\n';
counter -= 1;
if (!stream.write(toWrite)) {
break;
}
}
if (!counter) {
stream.end();
}
});
 
d1 = Date.now();
console.log('Starting: ' + (new Date().toISOString()));
while (counter) {
toWrite = '[' + new Date().toISOString() + '] ' +
'[INFO] server - Message[' + counter + ']\n';
counter -= 1;
if (!stream.write(toWrite)) {
break;
}
}
});
}
 
 
function loggerSpeedTest(callback) {
console.log('---------- loggerSpeedTest ----------');
 
var filename = 'log4js.log',
counter = NB_ITERATION;
 
fs.unlink(filename, function () {
log4js.configure({
appenders: [
{
type: 'console',
category: ["console"]
},
{
type: 'dateFile',
filename: filename,
pattern: "-yyyy-MM-dd",
category: 'server'
}
]
});
 
var d1,
diff,
logger = log4js.getLogger('server'),
watchLastModif,
intervalLastModif,
timer;
 
console.log('Starting: ' + (new Date().toISOString()));
 
d1 = Date.now();
while (counter) {
logger.info('Message[' + counter + ']');
counter -= 1;
}
diff = Date.now() - d1;
console.log('Time elapsed calling logger.info(): ' + diff + 'ms');
 
fs.watchFile(filename, { interval: 5000 }, function (curr, prev) {
prev = prev || null;
watchLastModif = curr.mtime;
});
 
timer = setInterval(function () {
if (watchLastModif) {
if (watchLastModif === intervalLastModif) {
clearInterval(timer);
 
diff = watchLastModif.getTime() - d1;
console.log('Time elapsed to write the file: ' + diff + 'ms');
 
callback();
} else {
console.log('File is still moving: ' + watchLastModif);
}
intervalLastModif = watchLastModif;
}
}, 5000);
});
}
 
async.series([
fileStreamSpeedTest,
loggerSpeedTest
], function () {
console.log('All done !');
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.