Tuesday 9 May 2017

Node JS child process fork execute file with send message and without that (refer to send message)

I want test execute time for 10,000,000,000 record.

  • test for.js file, execute time: 200 sec

  • test index.js file

    1. if use Test 1 comment in child_process.js file, execute time is very long (more than execute for.js file)

    2. if use Test 2 comment in child_process.js file, execute time is very fast, execute time: 58 sec


now my question:

must different between data.js file in Test 1 and async.js file in Test 2 when call child_process.js with the same for loop?



index.js

var launcher = require('./child_process');

console.log('Launching cluster');

launcher.launch();

child_process.js

var child_process = require('child_process');
var numCPUs = require('os').cpus().length;

var arg = [
    {start: 0, end: 2500000000},
    {start: 2500000000, end: 5000000000},
    {start: 5000000000, end: 7500000000},
    {start: 7500000000, end: 10000000000}
];

exports = module.exports = {
    launch: function () {
        console.log('Before fork');
        for (var i = 0; i < numCPUs; i++) {
             /**
              * Test 1
              */
            //child_process.fork(__dirname + '/data.js', [arg[i].start, arg[i].end]);
             /**
              * Test 2
              */
            // var result = child_process.fork(__dirname + '/async.js', []);
            // result.send(arg[i]);
        }
        console.log('After fork');
    }
};

async.js

process.on('message', function (arg) {
    console.log('Range:', arg);
    var start = new Date().getTime();
    var str;

    for (var i = arg.start; i < arg.end; i++) {
        if ((Math.floor(Math.random() * 100) + 1)  % 2 === 0) {
            str = i;
        } else {
            str = i;
        }
    }

    console.log(new Date().getTime() - start);
});

data.js

var arg = process.argv;
arg.shift();
arg.shift();

var start = new Date().getTime();
var str;

console.log('Range:', arg[0], arg[1]);

for (var i = arg[0]; i < arg[1]; i++) {
    if ((Math.floor(Math.random() * 100) + 1)  % 2 === 0) {
        str = i;
    } else {
        str = i;
    }
}

console.log(new Date().getTime() - start);

for.js

var start = new Date().getTime();
var str;

for (var i = 0; i < 10000000000; i++) {
    if ((Math.floor(Math.random() * 100) + 1)  % 2 === 0) {
        str = i;
    } else {
        str = i;
    }
}

console.log(new Date().getTime() - start);



via pooya azarpour

No comments:

Post a Comment