惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
Recent Announcements
Recent Announcements
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
博客园_首页
The Cloudflare Blog
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
P
Proofpoint News Feed
Y
Y Combinator Blog
Jina AI
Jina AI
博客园 - 聂微东
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
F
Full Disclosure
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
T
Tailwind CSS Blog
J
Java Code Geeks
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
宝玉的分享
宝玉的分享
IT之家
IT之家
Hacker News: Ask HN
Hacker News: Ask HN
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs

博客园 - hemingchen

asp.net MVC控制器中返回JSON格式的数据时提示下载 Delphi XE2 新技术说明 (What's new in Delphi XE2)(转载) Microsoft.VisualStudio.Xaml 加载错误 解决 网页如何有效调用exe js parentElement - hemingchen - 博客园 Response.ContentType 详细列表 - hemingchen - 博客园 SQL 2005 开启OpenRowset/OpenDatasource的办法 常用正则表达式 - hemingchen - 博客园 XMLHTTP 保护自己的眼睛 转载 37个Ajax和CSS实现的Tab选项卡切换效果界面 SQL2005附加数据库"失败........无法更新数据库"xxx",因为数据库是只读 - hemingchen - 博客园 asp.net C# 下母版页使用 .net Access 新增记录后获取自动编号的值 JavaScript 脚本文件编码导致的问题 jquery json简单例子 jquery ajax xml jquery学习 使用面向对象的技术创建高级 Web 应用程序 文章来源:http://msdn.microsoft.com/zh-cn/magazine/cc163419.aspx本文讨论:
MiniProfiler
hemingchen · 2015-10-15 · via 博客园 - hemingchen

以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天我就来谈用mvc开发项目的调试和性能监控。EF框架自动给我生成sql语句,当我们的程序遇到性能问题的时候我们可以用MiniProfiler.EF来监控调试MVC和EF的性能,查看生成的sql语句、运行了哪些sql,以及所花的时间。MiniProfiler.EF,一个轻量级开源的mvc性能调试、监控组件MiniProfiler专门为EF定制的版本。下面通过一个具体一例子的说明怎么在我们的项目中用MiniProfiler.EF6监控调试MVC和EF的性能。下面的项目是基于我上面的一篇文章的,MVC5与EF6 Code First 第一个入门完整实例教程

1、安装MiniProfiler.EF6

nuget搜索框中输入MiniProfiler,将出现下面结果:

点击安装将把MiniProfiler.EF6相关的dll加到项目中。

2、添加MiniProfiler.EF相关代码到项目里面

1、在Global.asax加入MiniProfiler相关的监控代码

修改之后完整内容为:

  1. using System.Web.Mvc;
  2. using System.Web.Optimization;
  3. using System.Web.Routing;
  4. using StackExchange.Profiling;
  5. using StackExchange.Profiling.EntityFramework6;
  6. namespace MiniProfilerDemo
  7. {
  8. public class MvcApplication : System.Web.HttpApplication
  9. {
  10. protected void Application_Start()
  11. {
  12. AreaRegistration.RegisterAllAreas();
  13. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  14. RouteConfig.RegisterRoutes(RouteTable.Routes);
  15. BundleConfig.RegisterBundles(BundleTable.Bundles);
  16. MiniProfilerEF6.Initialize();
  17. }
  18. protected void Application_BeginRequest()
  19. {
  20. MiniProfiler.Start();
  21. }
  22. protected void Application_EndRequest()
  23. {
  24. MiniProfiler.Stop();
  25. }
  26. }
  27. }

其中是在Application_Start加入了MiniProfilerEF6.Initialize()和添加了Application_BeginRequest、Application_BeginRequest两个Application的事件函数,这个的作用分别是初始化MiniProfilerEF6和开始、结束MiniProfiler监控。

2、修改_Layout.cshtml视图文件

在Views\Shared\_Layout.cshtml文件的body前面加上一段代码,让监控展示在页面上。

  1. @StackExchange.Profiling.MiniProfiler.RenderIncludes()

如下图:

3、在Web.config加入代码

  1. <system.webServer>
  2. <handlers>
  3. <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
  4. </handlers>
  5. </system.webServer>

为了要在页面上显示MVC和EF的调试跟踪时间必须要加入上面的代码。如下图:

在system.webServer配置结点下的handlers结点,加入了一个名为MiniProfiler的handler。

3、查看运行结果

运行程序,查看页面如下图:

可以看到左角多了一个数字的区块,表示这个页面所花的毫秒数,点击上面的数字就可以弹出详细的时间跟踪信息,如下图:

可以看到这个页面运行了三个sql语句,sql所花时间为您673.4毫秒,占部时间为为12.5%。还可以点击这个sql的时间,将显示运行了哪些sql,如下图:

4、细微监控方法内部的时间

现在我们为ProductController加上监控,监控获取从数据库中获取Product记录所花的时间。我们把ProductController修改为:

  1. using MiniProfilerDemo.DAL;
  2. using System.linq;
  3. using System.Web.Mvc;
  4. using StackExchange.Profiling;
  5. using System.Collections.Generic;
  6. using MiniProfilerDemo.Models;
  7. namespace MiniProfilerDemo.Controllers
  8. {
  9. public class ProductController : Controller
  10. {
  11. public ActionResult Index()
  12. {
  13. using (EFDbContext db = new EFDbContext())
  14. {
  15. var profiler = MiniProfiler.Current;
  16. List<Product> m;
  17. using (profiler.Step("获取Product列表"))
  18. {
  19. m = db.Products.ToList();
  20. }
  21. return View(m);
  22. }
  23. }
  24. }
  25. }

重新生成项目,再次运行查看页面,如下图:

可以看到上面多了我们刚才手动加的“获取Product列表”条记录。这样可以细微监控方法内部的时间,方便、快速地帮我们找出我们的程序的瓶颈所在。

文章转载自:蓝狐软件工作室 » 跟蓝狐学MVC教程--MiniProfiler.EF6监控调试MVC5和EF6的性能