我正在尝试创建一个控制台应用程序来计算 sharePoint 网站页面的页面加载时间。我需要缓存和未缓存的页面加载时间。
到目前为止,我已经尝试使用System.Net.WebClient和System.Diagnostics.stopwatch来计算页面加载时间。但我仍然不确定做同样事情的最佳方法是什么。
使用 WebClient 的示例代码
class Program
{
static void Main(string[] args)
{
string urls = "https://www.google.com/";
using (WebClient client = new WebClient())
{
int i = 0;
for (i = 0; i < 2; i++)
{
//Get uncached data for the first time and next time get data from cache
if (i == 0)
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();// start System.Diagnostics.Stopwatch before downloading url
client.Credentials = new System.Net.NetworkCredential("Username", "Password", "Domain");
String result = client.DownloadString(urls);
stopwatch.Stop();
Console.WriteLine(String.Format("{0} = {1}", urls, stopwatch.Elapsed.TotalSeconds));
}
}
}
}