'use strict'
/**
 * Created by tungtouch on 2/9/15.
 */

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

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 = 10000;
var arr = [];

var cacheIndex = 0;

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

console.log("Num CPU:", numCPUs);

var q = async.queue(function (index, cb) {
    setTimeout(function () {
        client.send(message, 0, message.length, 4444, "128.199.126.250", function (err) {
            cacheIndex++;
            console.log('Request:', index);
            if (cacheIndex < max) {
                console.log("Total request:", index, cacheIndex)
            }
            if (err) {
                console.log('ERROR :', err);
            }
            //cb(err);
        });
    }, 20);
});


if (cluster.isMaster) {
    // Fork workers.
    for (var i = 0; i < 80; i++) {
        cluster.fork();
    }

    cluster.on('exit', function (worker, code, signal) {
        console.log('worker ' + worker.process.pid + ' died');
    });
} else {
    // Workers can share any TCP connection
    // In this case its a HTTP server
    q.push(arr);
}
console.log("Starting Benchmark!");

@@ -362,29 +362,8 @@
 %5D;%0A%0A
-var cacheIndex = 0;%0A%0A
 for 
@@ -473,17 +473,16 @@
 function
- 
 (index, 
@@ -484,17 +484,16 @@
 dex, cb)
- 
 %7B%0A    se
@@ -609,34 +609,8 @@
 ) %7B%0A
-            cacheIndex++;%0A
     
@@ -633,142 +633,29 @@
 log(
-'
+%22
 Request
-:', index);%0A            if (cacheIndex %3C max) %7B%0A                console.log(%22Total request:%22, index, cacheIndex)%0A            %7D
+ : %22, index);
 %0A   
@@ -665,23 +665,21 @@
       if
- 
 (err)
- 
 %7B%0A      
@@ -743,18 +743,16 @@
         
-//
 cb(err);
@@ -782,16 +782,17 @@
 ;%0A%7D);%0A%0A%0A
+%0A
 if (clus
@@ -852,17 +852,18 @@
  0; i %3C 
-8
+10
 0; i++) 
@@ -926,17 +926,16 @@
 function
- 
 (worker,

Update UDPefef