run_tests.js 5.7 KB

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