Click here to Skip to main content
15,891,864 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 359.8K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
#!/usr/bin/env node

var colors = require('colors'),
    fs = require('fs'),
    _ = require('underscore'),
    metrics = require('metrics'),

    // `node diff_multi_bench_output.js before.txt after.txt`
    before = process.argv[2],
    after = process.argv[3];

if (!before || !after) {
    console.log('Please supply two file arguments:');
    var n = __filename;
    n = n.substring(n.lastIndexOf('/', n.length));
    console.log('    ./' + n + ' multiBenchBefore.txt multiBenchAfter.txt');
    console.log('To generate multiBenchBefore.txt, run');
    console.log('    node multi_bench.js > multiBenchBefore.txt');
    console.log('Thank you for benchmarking responsibly.');
    return;
}

var before_lines = fs.readFileSync(before, 'utf8').split('\n'),
    after_lines = fs.readFileSync(after, 'utf8').split('\n');

console.log('Comparing before,', before.green, '(', before_lines.length,
    'lines)', 'to after,', after.green, '(', after_lines.length, 'lines)');

var total_ops = new metrics.Histogram.createUniformHistogram();

before_lines.forEach(function(b, i) {
    var a = after_lines[i];
    if (!a || !b || !b.trim() || !a.trim()) {
        // console.log('#ignored#', '>'+a+'<', '>'+b+'<');
        return;
    }

    b_words = b.split(' ').filter(is_whitespace);
    a_words = a.split(' ').filter(is_whitespace);

    var ops =
        [b_words, a_words]
        .map(function(words) {
            // console.log(words);
            return parseInt10(words.slice(-2, -1));
        }).filter(function(num) {
            var isNaN = !num && num !== 0;
            return !isNaN;
        });
    if (ops.length != 2) return

    var delta = ops[1] - ops[0];

    total_ops.update(delta);

    delta = humanize_diff(delta);
    console.log(
        // name of test
        command_name(a_words) == command_name(b_words)
            ? command_name(a_words) + ':'
            : '404:',
        // results of test
        ops.join(' -> '), 'ops/sec (∆', delta, ')');
});

console.log('Mean difference in ops/sec:', humanize_diff(total_ops.mean()));

function is_whitespace(s) {
    return !!s.trim();
}

function parseInt10(s) {
    return parseInt(s, 10);
}

// green if greater than 0, red otherwise
function humanize_diff(num) {
    if (num > 0) {
        return ('+' + num).green;
    }
    return ('' + num).red;
}

function command_name(words) {
    var line = words.join(' ');
    return line.substr(0, line.indexOf(','));
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions