任何人都可以指導我編寫什麼C#代碼來獲取每個URL的頁面加載時間(如果URL是作爲輸入提供的)?同時檢查幾個URL的頁面加載時間
或
如果可能的話請爲我提供指向那些軟件的鏈接。任何將多個URL作爲輸入併爲每個URL提供頁面加載時間的軟件。
任何人都可以指導我編寫什麼C#代碼來獲取每個URL的頁面加載時間(如果URL是作爲輸入提供的)?同時檢查幾個URL的頁面加載時間
或
如果可能的話請爲我提供指向那些軟件的鏈接。任何將多個URL作爲輸入併爲每個URL提供頁面加載時間的軟件。
你想測量第一個請求被回答的時間,或者你想要包括下載風格和外部腳本以及客戶端渲染嗎?
第一個可以簡單地通過使用WebClient來解決。
WebClient client = new WebClient();
// Add a user agent header in case the
// requested URI contains a query.
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Stream data = client.OpenRead (@"your-url-here");
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd();
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
data.Close();
reader.Close();
而對於後者,把WebBrowser窗體上,您創建消耗DocumentCompleted事件的方法:
// in your form declarations
Stopwatch _stopwatch = new Stopwatch();
String _url = @"your-url-here";
// in the button-click or whenever you want to start the test
_stopwatch.Start();
this.WebBrowser1.Navigate(_url);
// The DocumentCompled event handler
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url == _url)
{
_stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", _stopwatch.Elapsed);
}
}
您可以向我提供代碼嗎?畢竟,它是一個+100回購問題! – Vaibhav
@vaibhav答案已更新。 – CodeCaster
實際上,您提供的第一個代碼很好,但我所談論的URL需要對其訪問進行身份驗證(我連接到特定的VPN,然後使用某些憑據打開瀏覽器以訪問該URL),但我不認爲也需要注意那個? – Vaibhav
您需要Stopwatch
,以及WebClient
或WebRequest
。
我建議YSlow的,是檢查網站的性能非常有用的工具,YSlow
我使用這種方式來保持內部網站積極措施/測井響應時間,種了永葆服務:
static void Main(string[] args)
{
LoggingManager.ConfigureAtStartup();
ErrorLogger.LogInformation("STARTED");
try
{
if (args.Length < 1)
{
ErrorLogger.LogInformation("No parameters provided...");
return;
}
int pingTimeoutMilliseconds = Convert.ToInt32(ConfigurationManager.AppSettings["pingTimeoutMilliseconds"]);
var urls = args[0].Split(';');
foreach (string url in urls)
{
if (string.IsNullOrWhiteSpace(url))
{
continue;
}
ErrorLogger.LogInformation(String.Format("Pinging url: {0}", url));
using (var client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
var stopW = new Stopwatch();
stopW.Start();
string result = client.DownloadString(url);
var elapsed = stopW.ElapsedMilliseconds;
stopW.Stop();
if (elapsed > pingTimeoutMilliseconds)
{
ErrorLogger.LogWarning(String.Format("{0} - took: {1} milliseconds to answer!", url, elapsed.ToString("N0")));
ErrorLogger.LogInformation(String.Format("Response was: {0} chars long", result.Length.ToString("n0")));
}
}
}
}
catch(Exception exc)
{
ErrorLogger.LogError(exc);
}
finally
{
ErrorLogger.LogInformation("COMPLETED");
LoggingManager.ShutdownOnExit();
}
}
ErrorLogger
是我在Log4Net
附近製作的一個小包裝。
如果我理解正確的問題,然後阿帕奇JMeter的可以這樣做:http://jmeter.apache.org/
它是可編寫腳本的,您可以設置各種負載測試場景。
我確定您熟悉Web調試代理Fiddler。 Fiddler中有很多你不需要的東西(比如UI),但是它們提供了一個.NET庫,可以包含在你的項目中,它將爲你提供所有你想要的HTTP性能。
FiddlerCore當前是作爲.NET類庫提供的,因此可以由任何.NET應用程序使用 。 FiddlerCore被設計爲 在專用應用程序中使用,該應用程序可以使用用戶界面(例如測試自動化)運行,也可以使用專用 的UI,因爲Fiddler Addon不是合適的選項(例如WPF流量可視化)。
下載中包含一個示例應用程序,我在幾分鐘內修改了這些示例應用程序,使您可以訪問統計信息選項卡下小提琴UI中顯示的相同信息。檢查fiddlercore dll中的Session.Timers對象以獲取這些值。
ClientConnected:15:03:05.017
ClientBeginRequest:15:03:05.056
ClientDoneRequest:15時03分05秒。076
確定網關:0毫秒
DNS查找:3ms的
TCP/IP連接:20毫秒
HTTPS握手:0毫秒
ServerConnected:15:03:05.151
FiddlerBeginRequest:15:03:05.152
ServerGotRequest:15 :03:05.157
ServerBeginResponse:15:03:05.292
ServerDoneResponse:15:03:05.314
ClientBeginResponse:15:03:05.331
ClientDoneResponse:15:03:05.333
總體消逝:00:00:00.2770277
希望這有助於你將需要弄清楚如何,而不是創建自己的會話中使用代理服務器,但是這是產品的規定特性,應該不需要太多時間。
試試這個
var URLs = new[]
{
"http://www.google.com",
"http://www.microsoft.com",
"http://www.slashdot.org"
};
var tasks = URLs.Select(
url => Task.Factory.StartNew(task =>
{
using (var client = new WebClient())
{
var t = (string)task;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
String result = client.DownloadString(t);
stopwatch.Stop();
Console.WriteLine(String.Format("{0} = {1}", url, stopwatch.Elapsed));
}
}, url)
).ToArray();
Task.WaitAll(tasks);
有了這個,我得到
http://www.microsoft.com = 00:00:05.1784172 milliseconds
http://www.slashdot.org = 00:00:09.9922422 milliseconds
http://www.google.com = 00:00:10.8720623 milliseconds
請考慮到,結果可能有所不同了很多:服務器突然高負荷,你的路由器有高CPU負載等方面有哪些你試圖達到? – RvdK
你想創建http://loads.in/? O_o – AlphaMale
當我給出幾個URL作爲輸入時,我只想得到一個代碼,通過該代碼我可以得到「頁面加載時間」(正如我們在Internet Explorer中的HTTPCheck工具中獲得的) – Vaibhav