forked from klpatil/CacheTuner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheTuner.aspx
479 lines (418 loc) · 17 KB
/
CacheTuner.aspx
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
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Sitecore.Configuration" %>
<%--Author : Kiran Patil
Version : 1.1.0.0--%>
<script language="C#" runat="server">
/// <summary>
/// Total of Max Size
/// </summary>
public long MaxSizeTotal
{
get { return (ViewState["maxSizeTotal"] == null) ? 0 : Convert.ToInt64(ViewState["maxSizeTotal"]); }
set { ViewState["maxSizeTotal"] = value; }
}
/// <summary>
/// Total of Delta
/// </summary>
public long DeltaTotal
{
get { return (ViewState["DeltaTotal"] == null) ? 0 : Convert.ToInt64(ViewState["DeltaTotal"]); }
set { ViewState["DeltaTotal"] = value; }
}
/// <summary>
/// Total of Count
/// </summary>
public long CountTotal
{
get { return (ViewState["CountTotal"] == null) ? 0 : Convert.ToInt64(ViewState["CountTotal"]); }
set { ViewState["CountTotal"] = value; }
}
private void InitializeComponent()
{
this.btnrefresh.Click += new EventHandler(this.btnrefresh_Click);
this.btnDownloadCSV.Click += new EventHandler(this.btnDownloadCSV_Click);
base.Load += new EventHandler(this.Page_Load);
}
protected override void OnInit(EventArgs e)
{
this.InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// This function will be used
/// to be get called on page_load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// This function will be used to
/// Refresh data
/// </summary>
/// <param name="sender">Button</param>
/// <param name="e">EventArgs</param>
private void btnrefresh_Click(object sender, EventArgs e)
{
try
{
// Disable CSV Download
btnDownloadCSV.Enabled = true;
// Reset Cache List
this.ResetCacheList();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
/// <summary>
/// This function will be used to download CSV
/// </summary>
/// <param name="sender">Button</param>
/// <param name="e">EventArgs</param>
private void btnDownloadCSV_Click(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Cache_Tuning_Suggestion"
+ DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") + ".csv");
Response.Charset = "";
Response.ContentType = "application/text";
StringBuilder sb = new StringBuilder();
// Headers
sb.Append("Name,");
sb.Append("Count,");
sb.Append("Size,");
sb.Append("Delta,");
sb.Append("MaxSize,");
sb.Append("Severity,"); sb.Append("Suggestion");
//append new line
sb.Append("\r\n");
Sitecore.Caching.ICacheInfo[] allCaches = Sitecore.Caching.CacheManager.GetAllCaches();
Array.Sort(allCaches, new Sitecore.Caching.CacheComparer());
// Reset counters
CountTotal = 0;
DeltaTotal = 0;
MaxSizeTotal = 0;
foreach (Sitecore.Caching.ICacheInfo cache in allCaches)
{
string str = "size_" + cache.Id.ToShortID();
long @int = Sitecore.MainUtil.GetInt(base.Request.Form[str], 0);
long count = cache.Count;
CountTotal += count;
long size = cache.Size;
long maxSize = cache.MaxSize;
// Sum of Max Size
MaxSizeTotal += maxSize;
long delta = size - @int;
DeltaTotal += delta;
double thresholdValue = 0;
if (maxSize > 0)
thresholdValue = ((double)size / (double)maxSize) * 100;
string severityLevel = "NORMAL";
string description = "NA";
// If ThresholdValue is grater than 80%
// OR If Delta value changes
// It's an ALERT
// It's an ALERT
if (thresholdValue > 80)
{
severityLevel = "ALERT";
description = @"Time to tune this cache! Reason : 80% exceeded. New Cache Size should be (following 50 % Increment rule)
:" + Sitecore.StringUtil.GetSizeString((maxSize + (maxSize * 50) / 100)) + ". % of Usage : " + thresholdValue.ToString();
}
// OR If Delta value changes
else if (size != @int)
{
severityLevel = "ALERT";
description = @"Time to tune this cache! Reason : Delta fluctuation. New Cache Size should be (following 50 % Increment rule)
:" + Sitecore.StringUtil.GetSizeString((maxSize + (maxSize * 50) / 100)) + ". % of Usage : " + thresholdValue.ToString();
}
else if (thresholdValue >= 50)
{
severityLevel = "WARNING";
description = "50% of this cache is being utilized. Its not a big reason to worry. But good to keep an eye on this.";
}
else
{
severityLevel = "NORMAL";
}
sb.Append(cache.Name);
sb.Append(",");
sb.Append(count.ToString());
sb.Append(",");
sb.Append(Sitecore.StringUtil.GetSizeString(size));
sb.Append(",");
sb.Append(Sitecore.StringUtil.GetSizeString(delta));
sb.Append(",");
sb.Append(Sitecore.StringUtil.GetSizeString(maxSize));
sb.Append(",");
sb.Append(severityLevel);
sb.Append(",");
sb.Append(description);
//append new line
sb.Append("\r\n");
}
// Final line -- for summary and total data
Sitecore.Caching.CacheStatistics statistics = Sitecore.Caching.CacheManager.GetStatistics();
sb.Append("Total");
sb.Append(",");
sb.Append(CountTotal.ToString());
sb.Append(",");
sb.Append(Sitecore.StringUtil.GetSizeString(statistics.TotalSize));
sb.Append(",");
sb.Append(Sitecore.StringUtil.GetSizeString(DeltaTotal));
sb.Append(",");
sb.Append(Sitecore.StringUtil.GetSizeString(MaxSizeTotal));
sb.Append(",");
sb.Append("NA");
sb.Append(",");
sb.Append("NA");
//append new line
sb.Append("\r\n");
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
catch (System.Threading.ThreadAbortException)
{
// Ignore this exception
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
/// <summary>
/// This function will be used to
/// Reset Cache List
/// </summary>
private void ResetCacheList()
{
Sitecore.Caching.ICacheInfo[] allCaches = Sitecore.Caching.CacheManager.GetAllCaches();
Array.Sort(allCaches, new Sitecore.Caching.CacheComparer());
HtmlTable table = tblCacheStats;
Sitecore.Web.HtmlUtil.AddRow(table, new string[] { string.Empty, "Name", "Count", "Size", "Delta", "MaxSize",
"Severity","Suggestion" });
table.Rows[0].Style.Add(HtmlTextWriterStyle.BackgroundColor, "#DDEAF9");
table.Rows[0].Style.Add(HtmlTextWriterStyle.Color, "black");
table.Rows[0].Style.Add(HtmlTextWriterStyle.BorderCollapse, "collapse");
table.Rows[0].Style.Add("border", "1px solid red");
table.Rows[0].Style.Add(HtmlTextWriterStyle.TextAlign, "center");
table.Rows[0].Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
//table.Rows[0].Cells[0].Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
// Reset counters
CountTotal = 0;
DeltaTotal = 0;
MaxSizeTotal = 0;
foreach (Sitecore.Caching.ICacheInfo cache in allCaches)
{
string str = "size_" + cache.Id.ToShortID();
long @int = Sitecore.MainUtil.GetInt(base.Request.Form[str], 0);
long count = cache.Count;
CountTotal += count;
long size = cache.Size;
long maxSize = cache.MaxSize;
// Sum of Max Size
MaxSizeTotal += maxSize;
long delta = size - @int;
DeltaTotal += delta;
double thresholdValue = 0;
if (maxSize > 0)
thresholdValue = ((double)size / (double)maxSize) * 100;
string severityLevel = "NORMAL";
string description = "NA";
string backGroundColor = "white";
// If ThresholdValue is grater than 80%
// It's an ALERT
if (thresholdValue > 80)
{
severityLevel = "ALERT";
description = @"Time to tune this cache! Reason : 80% exceeded. New Cache Size should be (following 50 % Increment rule)
:" + Sitecore.StringUtil.GetSizeString((maxSize + (maxSize * 50) / 100)) + ". % of Usage : " + thresholdValue.ToString();
backGroundColor = "red";
}
// OR If Delta value changes
else if (size != @int)
{
severityLevel = "ALERT";
description = @"Time to tune this cache! Reason : Delta fluctuation. New Cache Size should be (following 50 % Increment rule)
:" + Sitecore.StringUtil.GetSizeString((maxSize + (maxSize * 50) / 100)) + ". % of Usage : " + thresholdValue.ToString();
backGroundColor = "red";
}
else if (thresholdValue >= 50)
{
severityLevel = "WARNING";
description = "50% of this cache is being utilized. Its not a big reason to worry. But good to keep an eye on this.";
backGroundColor = "orange";
}
else
{
severityLevel = "NORMAL";
backGroundColor = "lightgreen";
}
// Add Row data
HtmlTableRow row = Sitecore.Web.HtmlUtil.AddRow(table,
new string[] { string.Empty, cache.Name,
count.ToString(),
Sitecore.StringUtil.GetSizeString(size),
Sitecore.StringUtil.GetSizeString(delta),
Sitecore.StringUtil.GetSizeString(maxSize),
severityLevel,
description});
// 3rd and 4th column should be righ aligned
for (int i = 2; i < row.Cells.Count; i++)
{
row.Cells[i].Align = "right";
// We just need Count Size and Delta to
// be right aligned
if (i == 5)
break;
}
row.BgColor = backGroundColor;
// Hidden Cache Size -- for delta value
HtmlInputHidden child = new HtmlInputHidden();
child.ID = str;
child.Value = size.ToString();
row.Cells[0].Controls.Add(child);
}
this.UpdateTotals(allCaches);
}
/// <summary>
/// This function will be used
/// to Update Totals
/// </summary>
/// <param name="allCaches">All Caches</param>
private void UpdateTotals(Sitecore.Caching.ICacheInfo[] allCaches)
{
Sitecore.Caching.CacheStatistics statistics = Sitecore.Caching.CacheManager.GetStatistics();
HtmlTableRow row = Sitecore.Web.HtmlUtil.AddRow(tblCacheStats,
new string[] { string.Empty, "<strong>Total</strong>",
"<strong>"+CountTotal.ToString()+"</strong>",
"<strong>"+Sitecore.StringUtil.GetSizeString(statistics.TotalSize)+"</strong>",
"<strong>"+Sitecore.StringUtil.GetSizeString(DeltaTotal)+"</strong>",
"<strong>"+Sitecore.StringUtil.GetSizeString(MaxSizeTotal)+"</strong>",
"<strong>NA</strong>",
"<strong>NA</strong>"});
row.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#DDEAF9");
row.Style.Add(HtmlTextWriterStyle.Color, "black");
row.Style.Add(HtmlTextWriterStyle.BorderCollapse, "collapse");
row.Style.Add("border", "1px solid red");
row.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
row.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
}
</script>
<html>
<head>
<title>Cache Tuner</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">
body
{
font-size: medium;
color: #000;
font-family: "Lucida Grande" , "Calibri" , helvetica, sans-serif;
scrollbar-3dlight-color: #DDEAF9;
scrollbar-arrow-color: #1C3C82;
scrollbar-base-color: #DDEAF9;
scrollbar-darkshadow-color: #DDEAF9;
scrollbar-face-color: #DDEAF9;
scrollbar-highlight-color: black;
scrollbar-shadow-color: black;
font-size: small;
background: #F7F7F7 none repeat scroll 0 0;
}
.button
{
-moz-border-radius-bottomleft: 7px;
-moz-border-radius-bottomright: 7px;
-moz-border-radius-topleft: 7px;
-moz-border-radius-topright: 7px;
background-color: #DDEAF9;
border: 1px solid #AAB7C6;
display: inline-block;
margin: 0 5px 10px 0;
padding: 3px;
border-color: darkgreen;
}
.data
{
border-collapse: collapse;
margin: 10px 0 0;
width: 100%;
border-color: darkgreen;
}
.data td
{
border: 1px solid darkgreen;
padding: 5px;
}
#content
{
-moz-border-radius-bottomleft: 7px;
-moz-border-radius-bottomright: 7px;
-moz-border-radius-topleft: 7px;
-moz-border-radius-topright: 7px;
background-color: #FFFFFF;
border: 2px solid #FFFFFF;
color: black;
line-height: 1.5em;
}
#shortnotice
{
background-color: #FFDDDD;
border: 1px solid #FFDDDD;
font-weight: bold;
width: 97%;
}
</style>
</head>
<body>
<form id="Form1" method="post" runat="server">
<table id="tableData" cellspacing="1" cellpadding="1" border="1" class="data">
<tr>
<td style="height: 36px" colspan="3" align="center">
<div id="shortnotice">
Just a note : Delta fluctuation will always come first time. So, you can just ignore
it first time!</div>
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="btnrefresh" runat="server" Text="Refresh" CssClass="button"></asp:Button>
<asp:Button ID="btnDownloadCSV" runat="server" Text="Export to CSV" Visible="true"
CssClass="button" Enabled="false"></asp:Button>
</td>
</tr>
<tr>
<td>
<div id="content">
<table runat="server" id="tblCacheStats" border="1" width="100%" cellpadding="4"
class="data">
</table>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>