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