安裝MiniProfiler
在MVC + EF + Bootstrap 2 權限管理系統入門級(附源碼)文章中下載了它的源碼,調試模式下打開一個頁面都要再2.5秒以上,所以使用MiniProfiler、MiniProfiler.MVC4 、MiniProfiler.EF6組件進行了分析。
首先,依次序安裝組件。然后,修改Global.aspx.cs 文件:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //自定義View ViewEngines.Engines.Clear(); ExtendedRazorViewEngine engine = new ExtendedRazorViewEngine(); engine.AddPartialViewLocationFormat("~/Areas/Common/Views/Shared/{0}.cshtml"); engine.AddPartialViewLocationFormat("~/Areas/Common/Views/Shared/{0}.vbhtml"); ViewEngines.Engines.Add(engine); //Model去除前后空格 ModelBinders.Binders.DefaultBinder = new TrimModelBinder(); //設置MEF依賴注入容器 MefConfig.RegisterMef(); //初始化EF6的性能監控 MiniProfilerEF6.Initialize(); //初始化DB DatabaseInitializer.Initialize(); } protected void Application_BeginRequest() { StackExchange.Profiling.MiniProfiler.Start(); } protected void Application_EndRequest() { StackExchange.Profiling.MiniProfiler.Stop(); }
MiniProfilerEF6.Initialize(); 一定要放在 DatabaseInitializer.Initialize(); 之前,否則會報如下錯誤:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code. Additional information: The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used."
運行站點有可能還會遇到這個錯誤:
An exception of type 'System.Data.SqlClient.SqlException' occurred in MiniProfiler.dll but was not handled in user code
Additional information: Invalid column name 'CreatedOn'.
解決方法是:
1.禁用這種類型的異常斷點(不可取)
2.刪除packages\MiniProfiler.3.2.0.157\lib\net40 下的MiniProfiler.PDB文件(我是這么做的)
3.禁用EF的數據庫變化跟蹤(未驗證,應該管用)
Found an answer for my question. Thanks all for replies. Database.SetInitializer<MyContext<Label>>(null); This fixes the problem and disables DB changes tracking in EF.
運行站點打開登陸頁
Sql占了47.7%,點開可以查看執行了哪些sql語句。
分析頁面耗時
首先,調試模式下運行Debug和Release代碼,耗時差不多都很長,截圖如下:
然后,非調試模式(Ctrl+F5)運行,截圖如下:
非調試模式(Ctrl+F5)的效率還是挺不錯的,沒想到和調試模式(F5)差別會這么大。使用必應搜了一下找到一個帖子 :Visual C++: Difference between Start with/without debugging in Release mode
里面的解釋是
The problem is that Windows drops in a special Debug Heap, if it detects that your program is running under a Debugger. This appears to happen at the OS level, and is independent of any Debug/Release mode settings for your compilation.
You can work around this 'feature' by setting an environment variable: _NO_DEBUG_HEAP=1
This same issue has been driving me nuts for a while; today I found the following, from whence this post is derived: http://blogs.msdn.com/b/larryosterman/archive/2008/09/03/anatomy-of-a-heisenbug.aspx
另外為了更細化的跟蹤某個方法的耗時可以在代碼中這么寫:
public AdminLayoutAttribute() { //TODO: Test //var userRole = new List<UserRole> { new UserRole { Id = 1, UserId = 1, RoleId = 1 } }; //var user = new User { Id = 1, LoginName = "admin", LoginPwd = "8wdJLK8mokI=", UserRole = userRole }; //SessionHelper.SetSession("CurrentUser", user); var user = SessionHelper.GetSession("CurrentUser") as User; if (user != null) { // using (StackExchange.Profiling.MiniProfiler.StepStatic("AdminLayout")) using (MiniProfiler.Current.Step("AdminLayout")) { // Enqueue a job var container = System.Web.HttpContext.Current.Application["Container"] as CompositionContainer; UserService = container.GetExportedValueOrDefault<IUserService>(); RoleService = container.GetExportedValueOrDefault<IRoleService>(); RoleModulePermissionService = container.GetExportedValueOrDefault<IRoleModulePermissionService>(); ModuleService = container.GetExportedValueOrDefault<IModuleService>(); ModulePermissionService = container.GetExportedValueOrDefault<IModulePermissionService>(); PermissionService = container.GetExportedValueOrDefault<IPermissionService>(); } } }
再次訪問模塊管理時就可以看到AdminLayout的耗時了,好像耗時特別小時這里就不記錄
總之有這么一個組件確實可以量化執行過程的耗時。對方法調用次數執行分析更強大的工具還是DotTrace,如圖:
參考鏈接:
MiniProfiler(MiniProfiler.EF6監控調試MVC5和EF6的性能)
解決:MiniProfiler.EF出現System.InvalidOperationException”類型的異常在 EntityFramework.dll 中發生
Entity Framework 4.3. Invalid column name 'CreatedOn'
Miniprofiler breaks on missing CreatedOn column