使用MiniProfiler跟踪MVC + EF + Bootstrap 2 权限管理系统的性能消耗
安裝MiniProfiler
在MVC + EF + Bootstrap 2 權(quán)限管理系統(tǒng)入門級(jí)(附源碼)文章中下載了它的源碼,調(diào)試模式下打開一個(gè)頁面都要再2.5秒以上,所以使用MiniProfiler、MiniProfiler.MVC4 、MiniProfiler.EF6組件進(jìn)行了分析。
首先,依次序安裝組件。然后,修改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();//設(shè)置MEF依賴注入容器 MefConfig.RegisterMef();//初始化EF6的性能監(jiān)控 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(); 之前,否則會(huì)報(bào)如下錯(cuò)誤:
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."
運(yùn)行站點(diǎn)有可能還會(huì)遇到這個(gè)錯(cuò)誤:
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.禁用這種類型的異常斷點(diǎn)(不可取)
2.刪除packages\MiniProfiler.3.2.0.157\lib\net40 下的MiniProfiler.PDB文件(我是這么做的)
3.禁用EF的數(shù)據(jù)庫變化跟蹤(未驗(yàn)證,應(yīng)該管用)
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.運(yùn)行站點(diǎn)打開登陸頁
Sql占了47.7%,點(diǎn)開可以查看執(zhí)行了哪些sql語句。
?
分析頁面耗時(shí)
首先,調(diào)試模式下運(yùn)行Debug和Release代碼,耗時(shí)差不多都很長,截圖如下:
?
? ??
? ?
然后,非調(diào)試模式(Ctrl+F5)運(yùn)行,截圖如下:
?
非調(diào)試模式(Ctrl+F5)的效率還是挺不錯(cuò)的,沒想到和調(diào)試模式(F5)差別會(huì)這么大。使用必應(yīng)搜了一下找到一個(gè)帖子 :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
?另外為了更細(xì)化的跟蹤某個(gè)方法的耗時(shí)可以在代碼中這么寫:
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 jobvar 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>();}}}再次訪問模塊管理時(shí)就可以看到AdminLayout的耗時(shí)了,好像耗時(shí)特別小時(shí)這里就不記錄
?
總之有這么一個(gè)組件確實(shí)可以量化執(zhí)行過程的耗時(shí)。對方法調(diào)用次數(shù)執(zhí)行分析更強(qiáng)大的工具還是DotTrace,如圖:
?
參考鏈接:
MiniProfiler(MiniProfiler.EF6監(jiān)控調(diào)試MVC5和EF6的性能)?
解決:MiniProfiler.EF出現(xiàn)System.InvalidOperationException”類型的異常在 EntityFramework.dll 中發(fā)生
Entity Framework 4.3. Invalid column name 'CreatedOn'
Miniprofiler breaks on missing CreatedOn column
?
轉(zhuǎn)載于:https://www.cnblogs.com/zeroes/p/miniProfiler.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的使用MiniProfiler跟踪MVC + EF + Bootstrap 2 权限管理系统的性能消耗的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库SQL语句学习笔记(4)-过滤数据
- 下一篇: 分布式存储系统设计(2)—— 数据分片