虚拟主机挑战
您必须记住,如果您像我们许多人(较小的公司和个人)一样托管在共享服务器上,则没有任何机器配置选项可用。
ASP.NET MVC 开销
如果我的网站在 20 分钟内没有被点击(并且网络应用程序已停止),它至少需要 30 秒。这很糟糕。
另一种测试性能的方法
还有另一种方法可以测试是您的 ASP.NET MVC 启动还是其他原因。在您的网站上放置一个普通的 HTML 页面,您可以直接点击它。
如果问题与 ASP.NET MVC 启动有关,那么即使 Web 应用程序尚未启动,HTML 页面也会几乎立即呈现。
这就是我第一次认识到问题出在 ASP.NET MVC 启动中的方式。
我在任何时候都加载了一个 HTML 页面,它的加载速度非常快。然后,在点击那个 HTML 页面后,我点击了一个 ASP.NET MVC URL,我会收到 Chrome 消息“Waiting for raddev.us...”
另一个有用脚本的测试
之后,我编写了一个 LINQPad(查看http://linqpad.net 了解更多信息)脚本,该脚本每 8 分钟访问一次我的网站(少于应用程序卸载的时间 - 应该是 20 分钟),然后我让它运行几个小时。
当脚本运行时,我访问了我的网站,每次我的网站都以极快的速度出现。这给了我一个好主意,我遇到的缓慢很可能是因为 ASP.NET MVC 启动时间。
获取 LinqPad,您可以运行以下脚本——只需将 URL 更改为您自己的并让它运行,您就可以轻松地进行测试。
祝你好运。
注意:在 LinqPad 中,您需要按 F4 并添加对 System.Net 的引用以添加将检索您的页面的库。
ALSO:确保将字符串 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
{
// ONLY the first time it retrieves the content it will print the string
String finalHtml = GetWebContent();
if (counter < 1)
{
Console.WriteLine(finalHtml);
}
counter++;
}
finally
{
webKeepAlive.Interval = 480000; // every 8 minutes
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";
}
}