获取更多"粒度"从MVC迷你探查 [英] Getting more "granularity" from the MVC Mini Profiler

查看:102
本文介绍了获取更多"粒度"从MVC迷你探查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这变成是对有用的人,我会很乐意把它变成一个社区维基的事情。

Should this turn out to be useful for anyone, I'll gladly turn it into a community wiki thing.

我有在MVC3应用程序有些慢的页面,因为很少的执行时间似乎在我的code的情况发生,我想看看我是否能找到更多关于什么花了这么长时间。这并不是说我成功了,但我一路上获得多一点智慧。

I have some slow pages in an MVC3 app, and since little of the execution time seemed to happen in my code, I wanted to see if I could find out more about what took so long. Not that I succeeded, but I gained a little more wisdom along the way.

这里有没有什么不明摆着一些MVC经验的人。基本上,我创建了自己ActionFilterAttribute,看起来像这样:

There is nothing here that isn't obvious to anyone with some MVC experience. Basically, I created my own ActionFilterAttribute that looks like this:

public class ProfilerAttribute : ActionFilterAttribute
{
    IDisposable actionStep = null;
    IDisposable resultStep = null;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        actionStep = MiniProfiler.Current.Step("OnActionExecuting " + ResultDescriptor(filterContext));
        base.OnActionExecuting(filterContext);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (actionStep != null)
        {
            actionStep.Dispose();
            actionStep = null;
        }
        base.OnActionExecuted(filterContext);
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        resultStep = MiniProfiler.Current.Step("OnResultExecuting " + ResultDescriptor(filterContext));
        base.OnResultExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (resultStep != null)
        {
            resultStep.Dispose();
            resultStep = null;
        }
        base.OnResultExecuted(filterContext);
    }

    private string ResultDescriptor(ActionExecutingContext filterContext)
    {
        return filterContext.ActionDescriptor.ControllerDescriptor.ControllerName + "." + filterContext.ActionDescriptor.ActionName;
    }

    private string ResultDescriptor(ResultExecutingContext filterContext)
    {
        var values = filterContext.RouteData.Values;

        return String.Format("{0}.{1}", values["controller"], values["action"]);
    }

这似乎运作良好,并在我的情况我已经了解到,大部分时间都在生活的ResultExecuting部分是实际花费,里面没有我的行动。

This seems to work well, and in my case I have learned that most of the time is actually spent in the ResultExecuting part of life, not inside my actions.

不过,我对这种做法的一些问题。

However, I have some questions about this approach.

1)这是一个做事的请求安全的方式?我猜测没有,因为在创建actionfilter只有一次,在RegisterGlobalFilters()方法中的Global.asax.cs。如果两个请求出现一次,actionStep和resultStep将毫无价值。这是真的?如果是这样,可以有人谁知道比我更有助于一个聪明的方法来处理呢?在本地计算机分析对我的作品,但可能没有那么多的部署与多人在同一时间发送请求的服务器上。

1) Is this a request-safe way of doing things? I am guessing no, since the actionfilter is created only once, in the RegisterGlobalFilters() method in Global.asax.cs. If two requests appear at once, actionStep and resultStep will be worthless. Is this true? If so, can someone who knows more than me contribute a clever way to handle this? Works for me during local machine profiling, but probably not so much deployed on a server with multiple people making requests at the same time.

2)是否有什么办法让更多的洞察结果执行的过程吗?或者我应该接受渲染视图等需要花费的时间?在我自己的应用我保证所有的数据库访问结束前,我的操作方法是在(在我的情况下使用NHibernate探查),我想保持我的看法苗条和简单;任何一种洞察什么减慢渲染下可能仍然是有用的,但。我猜使用迷你探查在我的模型对象将显示在这里,如果我的任何一部分慢code在这里执行。

2) Is there any way to get more insight into the result-executing process? Or should I just accept that rendering the view etc. takes the time it takes? In my own app I ensure that all database access is finished before my action method is over (using NHibernate Profiler in my case), and I like to keep my views slim and simple; Any kind of insight into what slows the rendering down could still be useful, though. I guess using the Mini Profiler in my model objects would show up here, if any slow code on my part was executed here.

3)ResultDescriptor方法可能是邪恶的,有毒。他们已经在我的测试中为我工作,但可能会需要的东西更强大的替代。我只是去与这给了我一半有用的事情的第一个版本。

3) The ResultDescriptor methods are probably evil and poisonous. They've worked for me in my tests, but would probably need to be replaced by something more robust. I just went with the first versions that gave me something halfway useful.

任何其他意见,这也将是非常欢迎的,即使他们是这是一个坏主意,死吧独。

Any other comments to this would also be very welcome, even if they are "This is a bad idea, go die alone".

推荐答案

这看起来像一个很酷的想法。我认为,这不是一个做事的请求,安全的方式。

This looks like a cool idea. I believe that it's NOT a request safe way of doing things.

您可以像这样把它链接到 HttpContext.Items

You could link it to HttpContext.Items like this

HttpContext.Items.Add("actionstep", actionStep);
HttpContext.Items.Add("resultstep", resultStep);

,然后检索它以类似的方式

And then retrieve it in similar fashion

actionStep = HttpContext.Items["actionstep"];
resultStep = HttpContext.Items["resultstep"];

显然,在你自己的支票将空值等等。

Obviously putting in your own checks for nulls and so forth.

的HttpContext 是为每个用户/请求的不同。

The HttpContext is different for each user/request.

要记住 HttpContext.Current.Session.SessionID 的事情,我有时会忘记它,它是当前HTTP请求的SessionID的(即它每次你改变按F5或以其他方式作出新的要求)。需要记住的另外重要的一点是,虽然在任何时间,所有的 HttpContext.Current.Session.SessionID 值一定是唯一的(即每个用户一个,或要求) ,他们可以重复使用,所以dno't认为他们是这些只使用一次的GUID每次。

The thing to remember about HttpContext.Current.Session.SessionID which I sometimes forget it that it is the SessionId of the current HTTP request (i.e. it changes each time you hit F5 or otherwise make a new request). The other important thing to remember is that, whilst at any on time, all HttpContext.Current.Session.SessionID values are necessarily unique (i.e. one for each user, or request), they can be reused, so dno't think of them as GUIDs which are only used once each.

这篇关于获取更多"粒度"从MVC迷你探查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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