如果将 URL 作为输入给出,谁能指导我应该编写什么 C# 代码来获取每个 URL 的页面加载时间?
或者
如果可能的话,请向我提供任何可以执行此操作的软件的链接。将多个 URL 作为输入并提供每个 URL 的页面加载时间的任何软件。
如果将 URL 作为输入给出,谁能指导我应该编写什么 C# 代码来获取每个 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);
}
}
如果我正确理解了这个问题,那么 Apache JMeter 可以这样做:http: //jmeter.apache.org/
它是可编写脚本的,您可以为负载测试设置各种场景。
尝试这个
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
我相信您对 Web 调试代理Fiddler很熟悉。Fiddler 中有很多您的应用程序不需要(如 UI),但是它们提供了一个可以包含在您的项目中的 .NET 库,它将为您提供您可能想要的所有 HTTPness。
FiddlerCore目前作为 .NET 类库提供,任何 .NET 应用程序都可以使用它。FiddlerCore 设计用于在没有用户界面(例如测试自动化)的情况下运行的特殊用途应用程序中,或者在没有用户界面的情况下运行的 UI 非常专业,以至于 Fiddler 插件不是一个合适的选项(例如 WPF 流量可视化)。
下载中包含一个示例应用程序,我在几分钟内对其进行了修改,它可以让您访问在统计选项卡下的提琴手 UI 中显示的相同信息。检查 fiddlercore dll 中的 Session.Timers 对象以获取这些值。
ClientConnected:15:03:05.017
ClientBeginRequest:15:03:05.056
ClientDoneRequest:15:03:05.076
确定网关:0ms
DNS 查找:3ms
TCP/IP 连接:20ms
HTTPS 握手:0ms
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
希望这会有所帮助,您将需要弄清楚如何创建自己的会话而不是使用代理,但这是产品的规定功能,不需要太多时间。
我正在使用它来保持内部网站的活动状态并测量/记录响应时间,这是一种保持活动状态的服务:
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
。
您将需要一个Stopwatch
,以及 aWebClient
或 a WebRequest
。