为什么在session_start在Global.asax.cs中引起性能问题? [英] Why does Session_Start in Global.asax.cs cause performance problems?

查看:126
本文介绍了为什么在session_start在Global.asax.cs中引起性能问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个空的在session_start处理程序的Global.asax.cs它渲染网页浏览器时,会导致显著的打击。

When I create an empty Session_Start handler in Global.asax.cs it causes a significant hit when rendering pages to the browser.

如何重现:

创建一个空的ASP.NET MVC 3 Web应用程序(我使用MVC 3 RC2)。
然后用这个code添加一个Home控制器:

Create an empty ASP.NET MVC 3 web application (I am using MVC 3 RC2). Then add a Home controller with this code:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    return View();
  }
  public ActionResult Number(int id)
  {
    return Content(id.ToString());
  }
}

接下来创建一个视图主页/ Index.cshtml并放置在身体部分如下:

Next create a view Home/Index.cshtml and place the following in the BODY section:

@for (int n = 0; n < 20; n++)
{ 
  <iframe src="@Url.Content("~/Home/Number/" + n)" width=100 height=100 />
}

当你运行这个页面,你会看到的IFRAME 20出现在页面上,每个里面都有一个编号。所有我在这里做的是创建一个加载幕后20多页的页面。在继续之前,注意这些20页的加载速度(刷新页面几次重复负载)。

When you run this page, you'll see 20 IFRAMEs appear on the page, each with a number inside it. All I'm doing here is creating a page that loads 20 more pages behind the scenes. Before continuing, take note of how quickly those 20 pages load (refresh the page a few times to repeat the loads).

接下来去你的Global.asax.cs,并添加这个方法(是的,方法体为空):

Next go to your Global.asax.cs and add this method (yes, the method body is empty):

protected void Session_Start()
{
}

现在再次运行该页面。这个时候你会发现,20的IFRAME加载慢得多,除了约1秒一前一后。这很奇怪,因为我们实际上并没有在session_start做什么...它只是一个空方法。但是,这似乎是足以引起所有后续页面放缓。

Now run the page again. This time you'll notice that the 20 IFRAMEs load much slower, one after the other about 1 second apart. This is strange because we're not actually doing anything in Session_Start ... it's just an empty method. But this seems to be enough to cause the slowdown in all subsequent pages.

有谁知道为什么发生这种情况,并且更好的没有任何人有一个修复/解决方法吗?

Does anybody know why this is happening, and better yet does anybody have a fix/workaround?

更新

我发现(与F5运行)时,调试器附加这种行为只发生。如果你没有调试器附着(按Ctrl-F5)运行它,那么它似乎是确定。所以,也许它不是一个显著的问题,但它仍然是陌生的。

I've discovered that this behavior only occurs when the debugger is attached (running with F5). If you run it without the debugger attached (Ctrl-F5) then it seems to be ok. So, maybe it's not a significant problem but it's still strange.

推荐答案

TL;博士:如果你面临这样的问题与WebForms和不要求特定页面会话状态写访问,加入的EnableSessionState =只读你的 @Page 指令帮助。

tl;dr: If you face this problem with Webforms and don't require write access to session state in that particular page, adding EnableSessionState="ReadOnly" to your @Page directive helps.

显然,在session_start 独力ASP.NET是否存在等执行从同一个会话相继始发的所有请求。然而,这可以被固定在一页接一页的基础上,如果不需要向会话写访问(见下文)。

Apparently, the existance of Session_Start alone forces ASP.NET to execute all requests originating from the same Session sequentially. This, however, can be fixed on a page-by-page basis if you don't need write access to the session (see below).

我已经创建了自己的测试设置与Web表单,它使用一个ASPX页面提供图像。 1

I've created my own test setting with Webforms, which uses an aspx page to deliver images.1

下面是测试页面(纯HTML,启动该项目的页面):

Here's the test page (plain HTML, start page of the project):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
    <div>
        <img src="GetImage.aspx?text=A" />
        <img src="GetImage.aspx?text=B" />
        <img src="GetImage.aspx?text=C" />
        <img src="GetImage.aspx?text=D" />
        <img src="GetImage.aspx?text=E" />
        <img src="GetImage.aspx?text=F" />
        <img src="GetImage.aspx?text=G" />
        <img src="GetImage.aspx?text=H" />
        <img src="GetImage.aspx?text=I" />
        <img src="GetImage.aspx?text=J" />
        <img src="GetImage.aspx?text=K" />
        <img src="GetImage.aspx?text=L" />
    </div>
</body>
</html>

下面是aspx页面(GetImage.aspx):

Here's the aspx page (GetImage.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetImage.aspx.cs" Inherits="CsWebApplication1.GetImage" %>

而codebehind(GetImage.aspx.cs,使用的相关部分命名跳过):

public partial class GetImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Debug.WriteLine("Start: " + DateTime.Now.Millisecond);
        Response.Clear();
        Response.ContentType = "image/jpeg";

        var image = GetDummyImage(Request.QueryString["text"]);
        Response.OutputStream.Write(image, 0, image.Length);
        Debug.WriteLine("End: " + DateTime.Now.Millisecond);
    }

    // Empty 50x50 JPG with text written in the center
    private byte[] GetDummyImage(string text)
    {
        using (var bmp = new Bitmap(50, 50))
        using (var gr = Graphics.FromImage(bmp))
        {
            gr.Clear(Color.White);
            gr.DrawString(text,
                new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular, GraphicsUnit.Point),
                Brushes.Black, new RectangleF(0, 0, 50, 50),
                new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            using (var stream = new MemoryStream())
            {
                bmp.Save(stream, ImageFormat.Jpeg);
                return stream.ToArray();
            }
        }
    }
}

测试运行


  • 运行1 后,未修改:该页面加载速度快,输出窗口显示开始随机搭配结束 s,这意味着该请求得到并行处理。

  • Run 1, unmodified: The page loads fast, the output window shows a random mix of Start and Ends, which means that the requests get processed in parallel.

运行2 ,加空在session_start 的Global.asax (需要在浏览器中,一旦按下F5,不知道这是为什么):开始结束交替,呈现该请求得到sequentually处理。浏览器多次刷新显示,这有性能问题,即使在调试器没有连接。

Run 2, add empty Session_Start to global.asax (need to hit F5 once in the browser, don't know why this is): Start and End alternate, showing that the requests get processed sequentually. Refreshing the browser multiple times shows that this has performance issues even when the debugger is not attached.

生成3 ,像润2,但添加的EnableSessionState =只读 @Page 的指令 GetImage.aspx :调试输出显示多个开始 s前面第一个结束。我们又是平行的,而且我们有不错的表现。

Run 3, like Run 2, but add EnableSessionState="ReadOnly" to the @Page directive of GetImage.aspx: The debug output shows multiple Starts before the first End. We are parallel again, and we have good performance.

1 是的,我知道,这应该与一个ASHX处理程序来完成吧。这只是一个例子。

1 Yes, I know that this should be done with an ashx handler instead. It's just an example.

这篇关于为什么在session_start在Global.asax.cs中引起性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆