1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.IO;
8 using System.Security.Cryptography;
9 using System.Text;
10 using System.Security.Permissions;
11 using System.Management;
12 using System.Data.SqlClient;
13
14 public partial class PerfTest : System.Web.UI.Page
15 {
16 static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("12345678");
17 static System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
18
19 uint LoopMax = 20000000;
20 uint StringAssignLoopMax = 7000000;
21 uint EmptyFuncLoopMax = 1500000;
22 uint StringIntLoopMax = 600000;
23 uint EncryptionLoopMax = 2000;
24 uint DateLoopMax = 3000;
25 uint SysVarArrayLoopMax = 10000;
26 uint FileReadLoopMax = 2000;
27 uint FileMetaLoopMax = 9000;
28 uint FileCopyLoopMax = 10000;
29 uint ObjectsLoopMax = 350000;
30 uint DBLoopMax = 1000;
31
32 DateTime StartTime;
33 DateTime EndTime;
34 String ElapsedTime;
35
36 protected void Page_Load(object sender, EventArgs e)
37 {
38
39 //------------------------------------------------------------------------------------------------+
40 Response.Write( "<table border='1' width='450px'><tr><td> Empty Loop Tests: ");
41 Response.Write( LoopMax.ToString("G") + " Iterations</td></tr></table><br />");
42
43 Response.Write( "<table border='0' width='450px'><tr><td> For Loop</td><td>");
44 StartTime = DateTime.Now;
45 int cnt=0;
46 for (; cnt < LoopMax; cnt++)
47 {
48 }
49
50 EndTime = DateTime.Now;
51 ElapsedTime = (EndTime - StartTime).ToString();
52 Response.Write(ElapsedTime.ToString() + "<br />");
53
54 Response.Write("</td></tr><tr><td> While</td><td>");
55
56 StartTime = DateTime.Now;
57 cnt = 0;
58 while (cnt < LoopMax)
59 {
60 cnt++;
61 }
62
63 EndTime = DateTime.Now;
64 ElapsedTime = (EndTime - StartTime).ToString();
65 Response.Write(ElapsedTime.ToString() + "<br />");
66 Response.Write("</td></tr><tr><td> Do While</td><td>");
67
68 StartTime = DateTime.Now;
69 cnt = 0;
70 do
71 {
72 cnt++;
73 } while (cnt < LoopMax);
74
75 EndTime = DateTime.Now;
76 ElapsedTime = (EndTime - StartTime).ToString();
77 Response.Write(ElapsedTime.ToString() + "<br />");
78 Response.Write("</td></tr></table><br />");
79
80 //------------------------------------------------------------------------------------------------+
81 string MyString = "";
82
83 Response.Write("<table border='1' width='450px'><tr><td> String Assignment Tests: ");
84 Response.Write(StringAssignLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
85 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
86 StartTime = DateTime.Now;
87 cnt = 0;
88 for (; cnt < StringAssignLoopMax; cnt++)
89 {
90 MyString = "This is a test of string assignment.";
91 }
92
93 EndTime = DateTime.Now;
94 ElapsedTime = (EndTime - StartTime).ToString();
95 Response.Write(ElapsedTime.ToString() + "<br />");
96
97 Response.Write("</td></tr><tr><td> While</td><td>");
98
99 StartTime = DateTime.Now;
100 cnt = 0;
101 while (cnt < StringAssignLoopMax)
102 {
103 MyString = "This is a test of string assignment.";
104 cnt++;
105 }
106
107 EndTime = DateTime.Now;
108 ElapsedTime = (EndTime - StartTime).ToString();
109 Response.Write(ElapsedTime.ToString() + "<br />");
110 Response.Write("</td></tr><tr><td> Do While</td><td>");
111
112 StartTime = DateTime.Now;
113 cnt = 0;
114 do
115 {
116 MyString = "This is a test of string assignment.";
117 cnt++;
118 } while (cnt < StringAssignLoopMax);
119
120 EndTime = DateTime.Now;
121 ElapsedTime = (EndTime - StartTime).ToString();
122 Response.Write(ElapsedTime.ToString() + "<br />");
123 Response.Write("</td></tr></table><br />");
124
125 //------------------------------------------------------------------------------------------------+
126
127
128 Response.Write("<table border='1' width='450px'><tr><td> Empty Function Call Tests: ");
129 Response.Write(EmptyFuncLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
130 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
131 StartTime = DateTime.Now;
132 cnt = 0;
133 for (; cnt < EmptyFuncLoopMax; cnt++)
134 {
135 MyFunction();
136 }
137
138 EndTime = DateTime.Now;
139 ElapsedTime = (EndTime - StartTime).ToString();
140 Response.Write(ElapsedTime.ToString() + "<br />");
141
142 Response.Write("</td></tr><tr><td> While</td><td>");
143
144 StartTime = DateTime.Now;
145 cnt = 0;
146 while (cnt < EmptyFuncLoopMax)
147 {
148 MyFunction();
149 cnt++;
150 }
151
152 EndTime = DateTime.Now;
153 ElapsedTime = (EndTime - StartTime).ToString();
154 Response.Write(ElapsedTime.ToString() + "<br />");
155 Response.Write("</td></tr><tr><td> Do While</td><td>");
156
157 StartTime = DateTime.Now;
158 cnt = 0;
159 do
160 {
161 MyFunction();
162 cnt++;
163 } while (cnt < EmptyFuncLoopMax);
164
165 EndTime = DateTime.Now;
166 ElapsedTime = (EndTime - StartTime).ToString();
167 Response.Write(ElapsedTime.ToString() + "<br />");
168 Response.Write("</td></tr></table><br />");
169
170 //------------------------------------------------------------------------------------------------+
171
172
173 Response.Write("<table border='1' width='450px'><tr><td> String and Integer Tests: ");
174 Response.Write(StringIntLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
175 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
176
177 cnt = 0;
178
179 string string1 = "abcdefghijklmno";
180 string string2;
181 string string3;
182 int x;
183
184 StartTime = DateTime.Now;
185
186 for (; cnt < StringIntLoopMax; cnt++)
187 {
188 string1 = "abcdefghijklmno";
189 x=cnt * 5;
190 x=x + x;
191 x=x/10;
192 string3 = string1 + Reverse(string1);
193 string2 = string1.Substring(9, 1) + string1.Substring( 1, 9);
194 string1 = string2.ToUpper();
195 }
196
197 EndTime = DateTime.Now;
198 ElapsedTime = (EndTime - StartTime).ToString();
199 Response.Write(ElapsedTime.ToString() + "<br />");
200 Response.Write("</td></tr></table><br />");
201
202
203 //------------------------------------------------------------------------------------------------+
204 Response.Write("<table border='1' width='450px'><tr><td> Encryption Tests: ");
205 Response.Write(EncryptionLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
206 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
207
208
209 string str = "This is a test string to see how fast your web server can process ASP functions";
210 byte[] MyBytes = encoding.GetBytes(str);
211 MD5 md5 = new MD5CryptoServiceProvider();
212 SHA1 sha1 = new SHA1CryptoServiceProvider();
213 DES des = new DESCryptoServiceProvider();
214
215
216
217 byte[] MD5Result;
218 byte[] Sha1Result;
219 string CheckSum;
220
221 cnt = 0;
222
223 StartTime = DateTime.Now;
224
225 for (; cnt < EncryptionLoopMax; cnt++)
226 {
227 MD5Result = md5.ComputeHash(MyBytes);
228 Sha1Result = sha1.ComputeHash(MD5Result);
229 CheckSum = GetChecksum(str);
230 str = Encrypt(CheckSum);
231 }
232
233 EndTime = DateTime.Now;
234 ElapsedTime = (EndTime - StartTime).ToString();
235 Response.Write(ElapsedTime.ToString() + "<br />");
236 Response.Write("</td></tr></table><br />");
237
238 //------------------------------------------------------------------------------------------------+
239
240 Response.Write("<table border='1' width='450px'><tr><td> Date Tests: ");
241 Response.Write(DateLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
242 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
243
244 cnt = 0;
245 string MyDateStr;
246
247 StartTime = DateTime.Now;
248
249 for (; cnt < DateLoopMax; cnt++)
250 {
251 DateTime NYD = new DateTime(1998, 1, 1, 0, 0, 0, 0);
252 MyDateStr = System.DateTime.Today.ToString() + ", News Years Day : " + NYD.ToString();
253 DateTime NextWeek = new System.DateTime().AddDays(7);
254 }
255
256 EndTime = DateTime.Now;
257 ElapsedTime = (EndTime - StartTime).ToString();
258 Response.Write(ElapsedTime.ToString() + "<br />");
259 Response.Write("</td></tr></table><br />");
260
261 //------------------------------------------------------------------------------------------------+
262
263 Response.Write("<table border='1' width='450px'><tr><td> Sys Var & Array Tests: ");
264 Response.Write(SysVarArrayLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
265 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
266
267 cnt = 0;
268 string tmpStr;
269
270 StartTime = DateTime.Now;
271
272 for (; cnt < SysVarArrayLoopMax; cnt++)
273 {
274 foreach (string item in Request.ServerVariables)
275 {
276 // Note there are fewer items in the collection that in thye &_SERVER collection 50/80
277 tmpStr = item.ToString();
278 }
279 }
280
281 EndTime = DateTime.Now;
282 ElapsedTime = (EndTime - StartTime).ToString();
283 Response.Write(ElapsedTime.ToString() + "<br />");
284 Response.Write("</td></tr></table><br />");
285
286 //------------------------------------------------------------------------------------------------+
287 Response.Write("<table border='1' width='450px'><tr><td> File System Tests: ");
288 Response.Write("</td></tr></table><br />");
289
290 Response.Write("<table border='0' width='450px'><tr><td> File Read Loop</td><td>");
291 StartTime = DateTime.Now;
292 for (cnt = 0; cnt < FileReadLoopMax; cnt++)
293 {
294 char[] buffer1 = new char[4096];
295 int index1 = 0;
296 int increment = 4096;
297 int charcount = 0;
298
299
300 FileStream fs = new FileStream("c:\\inetpub\\wwwroot\\perftest.php", FileMode.Open);
301 StreamReader sr = new StreamReader(fs);
302 do
303 {
304 charcount = sr.ReadBlock(buffer1, index1, increment);
305 } while (charcount >= increment);
306 fs.Close();
307
308 }
309
310 EndTime = DateTime.Now;
311 ElapsedTime = (EndTime - StartTime).ToString();
312 Response.Write(ElapsedTime.ToString());
313
314 Response.Write(" (" + FileReadLoopMax.ToString("G") + " Iterations)<tr><td> Metadata Read Loop</td><td>");
315
316 Boolean IsReadable;
317 Boolean IsWritable;
318 StartTime = DateTime.Now;
319 for (cnt = 0; cnt < FileMetaLoopMax; cnt++)
320 {
321 FileIOPermission f1 = new FileIOPermission(FileIOPermissionAccess.Read, "c:\\inetpub\\wwwroot\\perftest.php");
322 try
323 {
324 f1.Demand();
325 IsReadable = true;
326 }
327 catch (Exception s)
328 {
329 IsReadable = false;
330 }
331 FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Write, "c:\\inetpub\\wwwroot\\perftest.php");
332 try
333 {
334 f2.Demand();
335 IsWritable = true;
336 }
337 catch (Exception s)
338 {
339 IsWritable = false;
340 }
341
342 FileInfo fi = new FileInfo("c:\\inetpub\\wwwroot\\perftest.php");
343 long len = fi.Length;
344
345 DriveInfo[] allDrives = DriveInfo.GetDrives();
346 long cSpace = allDrives[0].AvailableFreeSpace;
347
348 }
349
350 EndTime = DateTime.Now;
351 ElapsedTime = (EndTime - StartTime).ToString();
352 Response.Write(ElapsedTime.ToString());
353 Response.Write(" (" + FileMetaLoopMax.ToString("G") + " Iterations)</td></tr>");
354 Response.Write(" <tr><td> File Copy Loop</td><td>");
355
356 StartTime = DateTime.Now;
357 for (cnt = 0; cnt < FileCopyLoopMax; cnt++)
358 {
359 string path = @"c:\\inetpub\\wwwroot\\perftest.php";
360 string path2 = @"c:\\temp\\junk.php";
361
362 File.Copy(path, path2, true);
363 }
364
365
366 EndTime = DateTime.Now;
367 ElapsedTime = (EndTime - StartTime).ToString();
368 Response.Write(ElapsedTime.ToString());
369 Response.Write(" (" + FileCopyLoopMax.ToString("G") + " Iterations)</td></tr>");
370 Response.Write("</table><br />");
371
372 //------------------------------------------------------------------------------------------------+
373
374 Response.Write("<table border='1' width='450px'><tr><td> Object Tests: ");
375 Response.Write(ObjectsLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
376 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
377
378 string foostr;
379 int fooint;
380 cnt = 0;
381 StartTime = DateTime.Now;
382
383 for (; cnt < ObjectsLoopMax; cnt++)
384 {
385 Foo bar = new Foo("Hello world!");
386 foostr = bar.do_foo();
387 fooint = bar.multiply(72, 12);
388 }
389
390 EndTime = DateTime.Now;
391 ElapsedTime = (EndTime - StartTime).ToString();
392 Response.Write(ElapsedTime.ToString() + "<br />");
393 Response.Write("</td></tr></table><br />");
394
395 //------------------------------------------------------------------------------------------------+
396
397 Response.Write("<table border='1' width='450px'><tr><td> Database Tests: ");
398 Response.Write(DBLoopMax.ToString("G") + " Iterations</td></tr></table><br />");
399 Response.Write("<table border='0' width='450px'><tr><td> For Loop</td><td>");
400
401 object strCity;
402 SqlConnection myConnection = new SqlConnection("user id=sa;" +
403 "password=P@ssw0rd;" +
404 "server=JHS-WS2008\\SQLEXPRESS;" +
405 "Trusted_Connection=no;" +
406 "database=perfdata");
407 cnt = 0;
408 StartTime = DateTime.Now;
409 SqlDataReader myReader = null;
410
411 for (; cnt < DBLoopMax; cnt++)
412 {
413 myConnection.Open();
414 SqlCommand myCommand = new SqlCommand("select * from city", myConnection);
415 myReader = myCommand.ExecuteReader();
416 while (myReader.Read())
417 {
418 strCity = myReader[0];
419 }
420 myConnection.Close();
421 }
422
423 EndTime = DateTime.Now;
424 ElapsedTime = (EndTime - StartTime).ToString();
425 Response.Write(ElapsedTime.ToString() + "<br />");
426 Response.Write("</td></tr></table><br />");
427
428
429 }
430
431 //---------------------------------------------------------------------------------------------------+
432 void MyFunction()
433 {
434 }
435
436 public string Reverse(string str)
437 {
438 // convert the string to char array
439
440 char[] charArray = str.ToCharArray();
441 int len = str.Length - 1;
442 /*
443 now this for is a bit unconventional at first glance because there
444 are 2 variables that we're changing values of: i++ and len--.
445 the order of them is irrelevant. so basicaly we're going with i from
446 start to end of the array. with len we're shortening the array by one
447 each time. this is probably understandable.
448 */
449 for (int i = 0; i < len; i++, len--)
450 {
451 /*
452 now this is the tricky part people that should know about it don't.
453 look at the table below to see what's going on exactly here.
454 */
455 charArray[i] ^= charArray[len];
456 charArray[len] ^= charArray[i];
457 charArray[i] ^= charArray[len];
458 }
459 string s = new string(charArray);
460 return s;
461 }
462
463 private static string GetChecksum(string strIn)
464 {
465 byte[] MyBytes = encoding.GetBytes(strIn);
466 SHA256Managed sha = new SHA256Managed();
467 byte[] checksum = sha.ComputeHash(MyBytes);
468 return BitConverter.ToString(checksum).Replace("-", String.Empty);
469
470 }
471
472 public static string Encrypt(string originalString)
473 {
474
475 if (String.IsNullOrEmpty(originalString))
476 {
477 throw new ArgumentNullException
478 ("The string which needs to be encrypted can not be null.");
479 }
480 DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
481 MemoryStream memoryStream = new MemoryStream();
482 CryptoStream cryptoStream = new CryptoStream(memoryStream,
483 cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
484 StreamWriter writer = new StreamWriter(cryptoStream);
485 writer.Write(originalString);
486 writer.Flush();
487 cryptoStream.FlushFinalBlock();
488 writer.Flush();
489 return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
490 }
491
492
493 }
494
495
496 class Foo {
497 private string z;
498 public Foo(string InVarvar) {
499 z = InVarvar;
500 }
501 public string do_foo() {
502 return z;
503 }
504 public int multiply(int var1, int var2) {
505 return (var1 * var2);
506 }
507 }
508