Mini-Profile的本意是用于Asp.NETMVC和Asp.nET程序Profile的简单工具,它本身不Hook到每一个线程,并不注重解决重要的Performance问题,相反:
. 适用与ADO.NET,LINQ to SQL.EF甚至EF-code first的性能监测
. 通过代码来Profile指定代码段的性能
我们来看一下Nop是怎么使用到Profiler的:
在Front web的_root.cshtml中
var displayMiniProfiler = EngineContext.Current.Resolve<Nop.Core.Domain.StoreInformationSettings>().DisplayMiniProfilerInPublicStore; }
理论上,后台应该在商店信息部分设置是否要显示性能分析数据(这个对于分析网站的性能是有帮助的,但如何仅针对管理员还需要进一步讨论,例如写数据到Log,或者Table,而不是显示在Page上):
public class StoreInformationSettings : ISettings { public string StoreName { get; set; } public string StoreUrl { get; set; } public bool StoreClosed { get; set; } public bool StoreClosedAllowForAdmins { get; set; } public string DefaultStoreTheme { get; set; } public bool AllowCustomerToSelectTheme { get; set; } public bool DisplayMiniProfilerInPublicStore { get; set; } }
在InstallationService当中,我们DisplayMiniProfilerInPublicStore = false,
<head>中注入Miniprofile的脚本以及输出内容的CSS
@if (displayMiniProfiler) { @MvcMiniProfiler.MiniProfiler.RenderIncludes(); }
在Global.asax.cs
AreaRegistration.RegisterAllAreas(); if (DataSettingsHelper.DatabaseIsInstalled() && EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore) { GlobalFilters.Filters.Add(new ProfilingActionFilter()); }
在Global.asax中,Web程序EndRequest中,Profile功能的关闭:
- protected void Application_EndRequest(object sender, EventArgs e)
- {
- if (DataSettingsHelper.DatabaseIsInstalled() &&
- EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore)
- {
- //stop as early as you can, even earlier with MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
- MiniProfiler.Stop();
- }
- }
使用步骤:
1. 下载MVC Mini profiler,添加引用
2. 一般而言,选择在Layout root文件中Head增加
@MvcMiniProfiler.MiniProfiler.RenderIncludes() </head>
3. 在Global.asax.cs Application_BeginRequest:
其实在这里可以增加逻辑判断,例如仅本地登陆启动Profile;特定用户Profile,Nop Commerce在这个方面可以改进一下:
if (Request.IsLocal) MvcMiniProfiler.MiniProfiler.Start();
。。。。 。。。。
protected void Application_End() { MvcMiniProfiler.MiniProfiler.Stop(); }
在后台某个View-Controller中:
using MvcMiniProfiler;
...
var profiler = MiniProfiler.Current; // it's ok if this is null
using (profiler.Step("Set page title"))
{
ViewBag.Title = "Home Page";
}
using (profiler.Step("Doing complexstuff"))
{
using (profiler.Step("Step A"))
{ // something more interestinghere
Thread.Sleep(100);
}
using (profiler.Step("Step B"))
{ // and here
Thread.Sleep(250);
}
}
更多信息,请参考:
http://code.google.com/p/mvc-mini-profiler/