run_tests.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ////////////////////////////////////////////////////
  2. //
  3. // Usage : node run_tests.js [nb user keys] [nb packets to send per user] [nb Requests Before Wait] [wait time in seconds]
  4. //
  5. // Example : node run_tests.js 5 50 20 10
  6. // It will generate 5 userKeys and will send 50 packet for each userKey.
  7. // The process will wait 10 secons every 20 requests
  8. //
  9. //
  10. var dgram = require('dgram'),
  11. util = require('util');
  12. var DEBUG = false;
  13. // Splice arguments
  14. var arguments = process.argv.splice(2);
  15. var nbUserKeys = parseInt(arguments[0]);
  16. var nbPacketsPerUser = parseInt(arguments[1]);
  17. var nbRequestsBeforeWait = parseInt(arguments[2]);
  18. var waitTime = parseInt(arguments[3]);
  19. if(!nbRequestsBeforeWait) nbRequestsBeforeWait = 10; // Default nbRequests before wait: 10;
  20. if(!waitTime) waitTime = 5; //Default wait time: 5 seconds
  21. var randomUserKeys = [];
  22. var keysPattern = "secretKey.${userKey}.monsite.home.clicks:${value}|${type}";
  23. //
  24. // ### function randomString (bits)
  25. // #### @bits {integer} The number of bits for the random base64 string returned to contain
  26. // randomString returns a pseude-random ASCII string which contains at least the specified number of bits of entropy
  27. // the return value is a string of length ⌈bits/6⌉ of characters from the base64 alphabet
  28. //
  29. var randomString = function (bits) {
  30. var chars, rand, i, ret;
  31. chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
  32. ret = '';
  33. // in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
  34. while (bits > 0) {
  35. // 32-bit integer
  36. rand = Math.floor(Math.random() * 0x100000000);
  37. // base 64 means 6 bits per character, so we use the top 30 bits from rand to give 30/6=5 characters.
  38. for (i = 26; i > 0 && bits > 0; i -= 6, bits -= 6) {
  39. ret += chars[0x3F & rand >>> i];
  40. }
  41. }
  42. return ret;
  43. };
  44. /**
  45. * Generate a number between 1 and maxValue included
  46. */
  47. var randomInt = function (maxValue) {
  48. return(Math.floor(Math.random() * maxValue) + 1);
  49. }
  50. /////////////////////////////////////////////////////////////////////////////
  51. // Generate userKeys
  52. for(var i=0; i<nbUserKeys; i++) {
  53. randomUserKeys.push(randomString( 16 * 8)); // Radom 16 characters userKey
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Send packets to statsd for each userKeys randomly
  57. // Open udp socket
  58. var client = dgram.createSocket("udp4");
  59. var send = function(host, port, message, index, closeSocket) {
  60. client.send(message, 0, message.length, port, host, function(err, bytes) {
  61. if(err) {
  62. client.close();
  63. process.stdout.write("\n");
  64. console.log("Error when sending packet n° " + index +" ! Exit...");
  65. process.exit(-1);
  66. }
  67. if(closeSocket) {
  68. process.stdout.write("\n");
  69. console.log('Done sending packets.');
  70. client.close();
  71. }
  72. //console.log("Packet sent: " + message);
  73. });
  74. }
  75. var statusMap = {};
  76. var availableUserNames = randomUserKeys.slice();
  77. if(DEBUG) console.log("Usernames = " + util.inspect(availableUserNames));
  78. if(DEBUG) console.log("availableUserNames.length = " + availableUserNames.length);
  79. console.log("Start sending...");
  80. // Loop and send udp messages
  81. var canSend = true;
  82. var index = 0;
  83. var sendPackets = function () {
  84. if(DEBUG) console.log("");
  85. if(DEBUG) console.log("== Loop: " + index);
  86. // Get random userKey
  87. var maxValue = availableUserNames.length;
  88. var userKey = availableUserNames[randomInt(maxValue)-1];
  89. if(DEBUG) console.log(" = User Key: " + userKey);
  90. // Generate a random between 1 and 10 value for this userKey
  91. var value = randomInt(10);
  92. // Increment statusMap
  93. if(!statusMap[userKey]) {
  94. statusMap[userKey] = 1;
  95. }
  96. else {
  97. if(statusMap[userKey] < nbPacketsPerUser) { statusMap[userKey] = statusMap[userKey]+1; }
  98. }
  99. // Build packet message
  100. var str = keysPattern.replace('${userKey}', userKey);
  101. str = str.replace('${value}', value);
  102. str = str.replace('${type}', 'c');
  103. var message = new Buffer(str);
  104. if(DEBUG) console.log(" - packet: " + message);
  105. // Send packet to statsd for this userKey
  106. process.stdout.write(".");
  107. if(availableUserNames.length == 1 && statusMap[availableUserNames[0]] == (nbPacketsPerUser-1)) {
  108. send("localhost", 8125, message, index, true);
  109. } else {
  110. send("localhost", 8125, message, index, false);
  111. }
  112. // If all packet are were sent for this userKey remove it from availableUserKeys
  113. if(statusMap[userKey] == nbPacketsPerUser) {
  114. if(DEBUG) {
  115. process.stdout.write("\n");
  116. console.log(" - All packets sent to this userKey !")
  117. }
  118. // Find userKey pos in array
  119. var userKeyPos = -1;
  120. for(var keyIndex in availableUserNames) {
  121. if(availableUserNames[keyIndex] == userKey) {
  122. userKeyPos = keyIndex;
  123. }
  124. }
  125. if(DEBUG) console.log(" - Key pos: " + userKeyPos);
  126. // Remove userKey from array
  127. if(userKeyPos > -1) {
  128. if(availableUserNames.length == 1) {
  129. availableUserNames = [];
  130. process.stdout.write("\n");
  131. console.log("End loop.");
  132. console.log("-> Status map :\n" + util.inspect(statusMap));
  133. } else {
  134. availableUserNames.splice(userKeyPos, 1);
  135. }
  136. }
  137. }
  138. index ++;
  139. if(DEBUG) console.log(util.inspect(statusMap));
  140. if(DEBUG) console.log("");
  141. if(availableUserNames.length > 0 && index % nbRequestsBeforeWait == 0) {
  142. console.log(" | " + nbRequestsBeforeWait + " packets sent.");
  143. process.stdout.write("\n");
  144. console.log("- waiting " + waitTime + " seconds -\n");
  145. setTimeout(function(){ sendPackets(); }, waitTime*1000);
  146. } else if(availableUserNames.length > 0) {
  147. sendPackets();
  148. }
  149. if(index == nbUserKeys*nbPacketsPerUser) {
  150. process.exit(0);
  151. }
  152. }
  153. if(availableUserNames.length > 0) {
  154. sendPackets();
  155. for(var i=0; i<5000000; ++i){}
  156. }