forked from ukautz/pages
/
routing-simulator.html
479 lines (447 loc) · 19.1 KB
/
routing-simulator.html
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<!DOCTYPE html>
<html>
<head>
<title>Routing Sim</title>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function() {
var nodeMarginFac = 0.1,
procMarginFac = 0.1,
settings = {},
simInterval = null,
nodeList = [],
queueCount = 0,
requestCount = 0,
timeoutCount = 0,
tickTime = 100,
running = false,
assignMethod = null,
balancePostion = -1,
longestRequestTime = 0,
avgRequestTime = 0,
longestWaitTime = 0,
longestWaitTimePercent = 0,
avgRequestTimeTotalCount = 0,
avgRequestTimeTotalSum = 0,
avgWaitTimeTotalCount = 0,
avgWaitTimeTotalSum = 0;
var assignMethods = {
intelligent: function (requestTime) {
var procRes = null, minRequests = -1;
INTELLIGENT_NODE:
for (var n = 0; n < nodeList.length; n++) {
for (var p = 0; p < nodeList[n].procs.length; p++) {
var proc = nodeList[n].procs[p];
var requests = proc.requests.length;
if (requests == 0) {
procRes = proc;
break INTELLIGENT_NODE;
} else if (proc.lockDuration + requestTime >= settings.maxQueueTime) {
continue;
} else if (minRequests == -1 || requests < minRequests) {
procRes = proc;
minRequests = requests;
}
}
}
if (procRes != null) {
procRes.lockDuration += requestTime;
procRes.requests.push(requestTime);
return procRes;
} else {
return null;
}
},
forsight: function (requestTime) {
var procRes = null, minLockDuration = -1;
FORSIGHT_NODE:
for (var n = 0; n < nodeList.length; n++) {
for (var p = 0; p < nodeList[n].procs.length; p++) {
var proc = nodeList[n].procs[p];
if (proc.lockDuration + requestTime > settings.maxQueueTime) {
continue;
}
if (proc.lockDuration == 0) {
procRes = proc;
break FORSIGHT_NODE;
} else if (minLockDuration == -1 || proc.lockDuration < minLockDuration) {
procRes = proc;
minLockDuration = proc.lockDuration;
}
}
}
if (procRes != null) {
procRes.lockDuration += requestTime;
procRes.requests.push(requestTime);
return procRes;
} else {
return null;
}
},
balanced: function (requestTime) {
balancePostion ++;
if (balancePostion >= settings.nodes * settings.procsPerNode) {
balancePostion = 0;
}
var nodeNum = Math.floor(balancePostion / settings.procsPerNode);
var procNum = balancePostion % settings.procsPerNode;
var proc = nodeList[nodeNum].procs[procNum];
if (proc.lockDuration + requestTime <= settings.maxQueueTime) {
proc.lockDuration += requestTime;
proc.requests.push(requestTime);
return proc;
} else {
return null;
}
},
random: function (requestTime) {
var pos = Math.floor(Math.random() * settings.nodes * settings.procsPerNode);
var nodeNum = Math.floor(pos / settings.procsPerNode);
var procNum = pos % settings.procsPerNode;
var proc = nodeList[nodeNum].procs[procNum];
if (proc.lockDuration + requestTime <= settings.maxQueueTime) {
proc.lockDuration += requestTime;
proc.requests.push(requestTime);
return proc;
} else {
return null;
}
}
};
function readSettings() {
$(['nodes', 'procsPerNode', 'reqsPerSec', 'durationReqMin', 'durationReqMax', 'maxQueueTime']).each(function() {
var val = parseInt($('input[name='+ this+ ']').val());
if (val < 1) {
val = 1;
$('input[name='+ this+ ']').val(1);
}
settings[this] = val;
});
if (settings.durationReqMin > settings.durationReqMax) {
var max = settings.durationReqMin;
settings.durationReqMin = settings.durationReqMax;
settings.durationReqMax = max;
$('input[name=durationReqMin]').val(settings.durationReqMin);
$('input[name=durationReqMax]').val(settings.durationReqMax);
}
settings['timeFactor'] = parseFloat($('input[name=timeFactor]').val());
if (settings.timeFactor < 0.05) {
settings.timeFactor = 0.05;
$('input[name=timeFactor]').val(settings.timeFactor.toFixed(2));
}
settings['assignMethod'] = $('input[name=assignMethod]:checked').val();
assignMethod = assignMethods[settings.assignMethod];
console.debug(settings);
}
function initGraph() {
$('#sim').empty();
nodeList = [];
var nodeWidth = 99 / settings.nodes;
var nodeMargin = nodeWidth * nodeMarginFac;
var procWidth = 99 / settings.procsPerNode;
var procMargin = procWidth * procMarginFac;
for (var n = 0; n < settings.nodes; n++) {
var nodeHtml = $('<div class="node" />');
nodeHtml.css({
left: (n * nodeWidth + nodeMargin/2)+ '%',
width: (nodeWidth - nodeMargin)+ '%'
});
var procList = [];
for (var p = 0; p < settings.procsPerNode; p++) {
var procHtml = $('<div class="proc" />');
procHtml.css({
left: (p * procWidth + procMargin/2)+ '%',
width: (procWidth - procMargin)+ '%'
});
nodeHtml.append(procHtml);
procList.push({html: procHtml, lockDuration: 0, requests: [], num: p});
}
nodeList.push({procs: procList, num: n});
$('#sim').append(nodeHtml);
}
return false;
}
function runSim() {
if (simInterval != null) {
clearInterval(simInterval);
}
var lastTick = (new Date()).getTime() * settings.timeFactor;
var last = Math.floor(lastTick/1000);
simInterval = setInterval(function() {
var curTick = (new Date()).getTime() * settings.timeFactor;
var cur = Math.floor(curTick/1000);
var isRequest = false,
avgRequestTimeCur = 0;
if (cur > last) {
isRequest = true;
last = cur;
queueCount += settings.reqsPerSec;
requestCount += settings.reqsPerSec;
}
if (queueCount > 0) {
var procsFound = 0, sumRequestTime = 0;
for (var i = 0; i < queueCount; i++) {
var requestTime = Math.round(Math.random() * (settings.durationReqMax - settings.durationReqMin) + settings.durationReqMin);
sumRequestTime += requestTime;
if (requestTime > longestRequestTime) {
longestRequestTime = requestTime;
$('#longestRequestTime').text((longestRequestTime).toFixed(2));
}
var foundProc = assignMethod(requestTime);
if (foundProc !== null) {
if (foundProc.lockDuration > longestWaitTime) {
longestWaitTime = foundProc.lockDuration;
$('#longestWaitTime').text((longestWaitTime).toFixed(2));
}
procsFound ++;
} else {
break;
}
}
avgRequestTimeCur = sumRequestTime / queueCount;
if (avgRequestTimeCur > avgRequestTime) {
avgRequestTime = avgRequestTimeCur;
$('#maxAvgRequestTime').text(avgRequestTime.toFixed(2));
}
queueCount -= procsFound;
console.debug("QUEUE COUNT", queueCount, "AFTER PROCS FOUND", procsFound);
if (queueCount > 0) {
timeoutCount += queueCount;
queueCount = 0;
}
}
var diffTick = Math.round((curTick - lastTick)),
freeProcs = 0,
sumWaitTime = 0.0;
$(nodeList).each(function() {
var node = this;
$(node.procs).each(function() {
var proc = this;
if (proc.lockDuration == 0) {
proc.requests = [];
freeProcs ++;
return;
}
sumWaitTime += proc.lockDuration;
proc.lockDuration -= diffTick;
proc.requests[0] -= diffTick;
if (proc.lockDuration < 0) {
proc.lockDuration = 0;
}
if (proc.lockDuration == 0) {
proc.requests = [];
freeProcs ++;
} else {
while (proc.requests.length > 0 && proc.requests[0] <= 0) {
var overhead = proc.requests[0];
proc.requests.shift();
proc.requests[0] += overhead;
}
}
var ratio = proc.lockDuration / settings.maxQueueTime;
var bgColor = 'rgb('+ Math.ceil(ratio*155 + 100)+ ',100,100)';
proc.html.css({
height: (5+ 95 * (proc.lockDuration/settings.maxQueueTime))+ '%',
backgroundColor: bgColor
});
});
});
lastTick = curTick;
$('#timeoutCount').text(timeoutCount+ " ")
.append("<small>("+ (timeoutCount * 100 / requestCount).toFixed(2)+ '%</small>)');
$('#freeProcs').text(freeProcs);
var avgWaitTimeCur = sumWaitTime / (settings.nodes * settings.procsPerNode);
if (isRequest) {
// request
avgRequestTimeTotalCount ++;
avgRequestTimeTotalSum += avgRequestTimeCur;
var avgRequestTimeTotalAvg = avgRequestTimeTotalSum / avgRequestTimeTotalCount;
$('#avgRequestTime').text(avgRequestTimeCur.toFixed(2));
$('#avgRequestTimeTotal').text(avgRequestTimeTotalAvg.toFixed(2));
// wait
avgWaitTimeTotalCount ++;
avgWaitTimeTotalSum += avgWaitTimeCur;
var avgWaitTimeTotalAvg = avgWaitTimeTotalSum / avgWaitTimeTotalCount;
var waitTimePercent = avgWaitTimeCur * 100 / avgRequestTimeCur - 100;
var waitTimeTotalPercent = avgWaitTimeTotalAvg * 100 / avgRequestTimeTotalAvg - 100;
$('#avgWaitTime').text(avgWaitTimeCur.toFixed(2)+ " ")
.append("<small>(+"+ (waitTimePercent).toFixed(2)+ "%)</small>");
$('#avgWaitTimeTotal').text((avgWaitTimeTotalSum / avgWaitTimeTotalCount).toFixed(2)+ ' ')
.append('<small>(+'+ waitTimeTotalPercent.toFixed(2)+ '%)</small>');
if (waitTimePercent > longestWaitTimePercent) {
longestWaitTimePercent = waitTimePercent;
$('#longestWaitTimePercent').text('+'+ longestWaitTimePercent.toFixed(2)+ "%");
}
$('#requestCount').text(requestCount);
}
}, tickTime);
}
$('form button').click(function() {
if (running) {
clearInterval(simInterval);
running = false;
balancePostion = 0;
avgRequestTime = 0;
avgRequestTimeTotalCount = 0;
avgRequestTimeTotalSum = 0;
avgWaitTimeTotalCount = 0;
avgWaitTimeTotalSum = 0;
longestRequestTime = 0;
longestWaitTime = 0;
longestWaitTimePercent = 0;
requestCount = 0;
timeoutCount = 0;
$(this).text('Restart');
} else {
readSettings();
initGraph();
runSim();
running = true;
$(this).text('Stop');
}
});
});
</script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet">
<style type="text/css">
#sim {
position: relative;
height: 300px;
}
#sim .pre {
background: #666;
height: 100%;
}
#sim .pre * {
position: absolute;
bottom: 45%;
text-align: center;
width: 100%;
color: white;
}
#sim .node {
position: absolute;
bottom: 0;
background: #000;
height: 100%;
}
#sim .node:hover {
background: #336;
}
#sim .node .proc {
position: absolute;
bottom: 0;
background: #aaa;
min-height: 5%;
}
</style>
<body>
<div class="container">
<div class="row">
<div class="span12">
<h1>Balancing Simulater</h1>
</div>
<div class="span3">
<form>
<h4>Settings</h4>
<label># Nodes</label>
<input type="text" name="nodes" value="4">
<label># Process / Node</label>
<input type="text" name="procsPerNode" value="4">
<label># Requests / sec</label>
<input type="text" name="reqsPerSec" value="16">
<label># Duration request min ms</label>
<input type="text" name="durationReqMin" value="500">
<label># Duration request max ms</label>
<input type="text" name="durationReqMax" value="1500">
<label># Timeout ms</label>
<input type="text" name="maxQueueTime" value="10000">
<label># Time multiplier</label>
<input type="text" name="timeFactor" value="2.00">
</form>
</div>
<div class="span3">
<form>
<h4>Balancing method</h4>
<label class="radio">
<input type="radio" value="intelligent" name="assignMethod" checked="checked">
intelligent
<span class="help-block">
Distributes request to process with least requests in queue.
</span>
</label>
<label class="radio">
<input type="radio" value="forsight" name="assignMethod">
forsight
<span class="help-block">
Distributes requests to processed with the least request/response time ahead. Has knowledge of future requests/response durations.
</span>
</label>
<label class="radio">
<input type="radio" value="balanced" name="assignMethod">
round-robin
<span class="help-block">
Distributes requests equally balanced over all processes.
</span>
</label>
<label class="radio">
<input type="radio" value="random" name="assignMethod">
random
<span class="help-block">
Distributes requests to randomly selected processes.
</span>
</label>
<div class="form-actions">
<button class="btn btn-primary btn-large" type="button">Start</button>
</div>
</form>
</div>
<div class="span6">
<table class="table">
<thead>
<tr>
<th title="Currently free processe">Free Procs</th>
<th title="Average time of all currently simulated request">Avg Request Time</th>
<th title="AVerage time a user has to wait for all currently simulated requests">Avg Wait Time</th>
</tr>
</thead>
<tbody>
<tr>
<td id="freeProcs">0</td>
<td id="avgRequestTime">0</td>
<td id="avgWaitTime">0</td>
</tr>
</tbody>
</table>
<div id="sim"><div class="pre"><p>Sim not started</p></div></div>
<table class="table">
<thead>
<tr>
<th title="Amount of total simulated requests">Requests</th>
<th title="Amount and percentage of requests which were timed out">Timeouts</th>
<th title="The longest any request took">Longest Response Time</th>
<th title="The longest a user had to wait for his response in miliseconds">Longest Wait Time ms</th>
<th title="The biggest percentage added to a request due to wait">Longest Wait Time %</th>
<th title="Average time a requests takes over all requests">Avg Request Time</th>
<th title="The average time and percent a user has to wait">Avg Wait Time</th>
</tr>
</thead>
<tbody>
<tr>
<td id="requestCount">0</td>
<td id="timeoutCount">0</td>
<td id="longestRequestTime">0</td>
<td id="longestWaitTime">0</td>
<td id="longestWaitTimePercent">0</td>
<td id="avgRequestTimeTotal">0</td>
<td id="avgWaitTimeTotal">0</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>