run_tests.js 5.8 KB

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