//
/**
 * Created by tungtouch on 2/9/15.
 */

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var colors = require('colors');
var dgram = require('dgram');
var message = new Buffer("Some bytes hello world bo bo bo world HEHEHEHE ahhaha hohoho hehe:");
var client = dgram.createSocket("udp4");
var async = require('async');

var max = 1000;
var numCluster = 100;
var arr = [];

for (var i = 0; i < max; i++) {
    arr.push(i);
}
var a = 0;

//[2GB]128.199.126.250 [8GB]128.199.109.202


if (cluster.isMaster) {

    // Keep track of http requests
    var numReqs = 0;
    var totalReqs = 0;
    if(numReqs != 0) {
        setInterval(function() {
            console.log(colors.red("Number Request: ", numReqs), " | ", colors.yellow("Total Request: ", totalReqs));
            numReqs = 0;
        }, 1000);
    }
// Count requestes
    function messageHandler(msg) {
        if (msg.cmd && msg.cmd == 'notifyRequest') {
            numReqs += 1;
            totalReqs +=1;
        }
    }


    // Start workers and listen for messages containing notifyRequest

    for (var i = 0; i < numCluster; i++) {
        cluster.fork();
    }

    Object.keys(cluster.workers).forEach(function(id) {
        cluster.workers[id].on('message', messageHandler);
    });

} else {
    var q = async.queue(function(index, cb){
        //console.log("Index: ", index);
        setTimeout(function () {
            client.send(message, 0, message.length, 4444, "128.199.126.250", function (err) {
                //console.log("Request : ", a++);
                process.send({ cmd: 'notifyRequest' });
                if(err){
                    console.log('ERROR :', err);
                }
                cb(err);
            });
        }, 20);
    });

    q.push(arr);

}
@@ -609,24 +609,44 @@
 alReqs = 0;%0A
+    var second = 0;%0A
     if(numRe
@@ -713,16 +713,33 @@
 ole.log(
+%0A                
 colors.r
@@ -777,16 +777,32 @@
 , %22 %7C %22,
+%0A               
  colors.
@@ -841,51 +841,209 @@
 eqs)
-);%0A            numReqs = 0;%0A        %7D, 1000
+, %22 %7C %22,%0A                colors.blue(%22Seconds: %22, second++));%0A            numReqs = 0;%0A        %7D, 1000);%0A    %7Delse%7B%0A        console.log(color.blue(%22Result benchmark: %22, colors.bold(totalReqs / second))
 );%0A 

Update UDPefef