Difference between revisions of "Using Timers in Dot.NET"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (Created page with '<pre> using System; using System.Diagnostics; using System.Net; class Program { const int _max = 5; static void Main(string[] args) { try { ...') |
PeterHarding (talk | contribs) |
||
| Line 60: | Line 60: | ||
</pre> | </pre> | ||
[[Category:C Sharp]] | |||
[[Category:Dot.NET]] | [[Category:Dot.NET]] | ||
[[Category:Microsoft]] | [[Category:Microsoft]] | ||
Latest revision as of 21:26, 16 October 2021
using System;
using System.Diagnostics;
using System.Net;
class Program
{
const int _max = 5;
static void Main(string[] args)
{
try
{
// Get url.
string url = args[0];
// Report url.
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("... PageTimeTest: times loads of web page over network");
Console.ResetColor();
Console.WriteLine("Testing: {0}", url);
// Fetch page.
using (WebClient client = new WebClient())
{
// Set gzip.
client.Headers["Accept-Encoding"] = "gzip";
// Download.
// ... Do an initial run to prime the cache.
byte[] data = client.DownloadData(url);
// Start timing.
Stopwatch stopwatch = Stopwatch.StartNew();
// Iterate.
for (int i = 0; i < Math.Min(100, _max); i++)
{
data = client.DownloadData(url);
}
// Stop timing.
stopwatch.Stop();
// Report times.
Console.WriteLine("Time required: {0} ms", stopwatch.Elapsed.TotalMilliseconds);
Console.WriteLine("Time per page: {0} ms", stopwatch.Elapsed.TotalMilliseconds / _max);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
Console.WriteLine("[Done]");
Console.ReadLine();
}
}
}