网站托管挑战
请记住,如果您的网站托管在共享服务器上,那么机器配置选项都将不可用,因为像我们这样的许多人(小型公司和个人)使用共享服务器。
ASP.NET MVC 开销
当我的网站在20分钟内没有被访问过(并且Web应用程序已停止)时,它至少需要30秒的时间。这太糟糕了。
另一种测试性能的方法
还有一种测试ASP.NET MVC启动或其他内容的方法。在您可以直接访问它的网站上放置一个普通的HTML页面。
如果问题与 ASP.NET MVC 启动有关,则即使 Web 应用程序未启动,HTML 页面也会几乎立即呈现。
这就是我最初认识到问题出现在ASP.NET MVC启动中的方式。
我随时加载一个 HTML 页面,它加载非常快速。然后,在访问了该 HTML 页面之后,我会访问我的某个 ASP.NET MVC URL,然后我会收到 Chrome 消息“等待raddev.us...”
使用有用的脚本进行另一个测试
之后,我编写了一个 LINQPad(查看http://linqpad.net获取更多信息)脚本,每8分钟(小于应用程序卸载的时间-应该是20分钟)访问我的网站,并让它运行数小时。
脚本运行时,我访问我的网站,每次都能快速加载我的网站。这使我很有信心,我经历的缓慢很可能是由于 ASP.NET MVC 启动时间过长导致的。
获取 LinqPad 并运行以下脚本--仅更改 URL 为您自己的,让它运行,您可以轻松测试这一点。
祝好运。
注意:在 LinqPad 中,您需要按F4并添加对 System.Net 库的引用,以检索您的页面。
另外:确保将字符串URL变量更改为指向从ASP.NET MVC站点加载路由的URL,以便引擎运行。
System.Timers.Timer webKeepAlive = new System.Timers.Timer();
Int64 counter = 0;
void Main()
{
webKeepAlive.Interval = 5000;
webKeepAlive.Elapsed += WebKeepAlive_Elapsed;
webKeepAlive.Start();
}
private void WebKeepAlive_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
webKeepAlive.Stop();
try
{
String finalHtml = GetWebContent();
if (counter < 1)
{
Console.WriteLine(finalHtml);
}
counter++;
}
finally
{
webKeepAlive.Interval = 480000;
webKeepAlive.Start();
}
}
public String GetWebContent()
{
try
{
String URL = "http://YOURURL.COM";
WebRequest request = WebRequest.Create(URL);
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
html = sr.ReadToEnd();
}
Console.WriteLine (String.Format("{0} : success",DateTime.Now));
return html;
}
catch (Exception ex)
{
Console.WriteLine (String.Format("{0} -- GetWebContent() : {1}",DateTime.Now,ex.Message));
return "fail";
}
}