MiniProfiler用法(转)
来源:互联网 发布:淘宝假冒品牌申诉 编辑:程序博客网 时间:2024/06/07 14:55
转自:http://miniprofiler.com/
A simple but effective mini-profiler for .NET, Ruby, Go andNode.js.
Where do I get support?
Bugs, Feature Requests, Discussion and KB live at community.miniprofiler.com or on Stack Overflow. View Releases and feel free to submit Pull Requests and Issues on GitHub.
Where is the current Ruby documentation?
See Github and Sams blog post.
What does it profile?
MiniProfiler does not attach itself to every single method call; that would be too invasive and wouldn't focus on the biggest performance issues. Instead, it provides:
- An ADO.NET profiler, capable of profiling calls on raw ADO.NET (SQL Server, Oracle, etc), LINQ-to-SQL, EF (includingCode First), Lightspeed and a range of other data access scenarios
- A pragmatic
Step
instrumentation that you can add to code you want to explicitly profile
Simple. Fast. Pragmatic. Useful.
Getting Started on .Net:
Hook up your application with at least one of the following packages are available on nuget:
MiniProfiler
- The core. Needed for any profiling (all of the packages below list this as a dependency)MiniProfiler.EF6
- For profiling Entity Framework version 6+MiniProfiler.EF5
- For profiling Entity Framework versions 4 and 5- obsolete package. Please use one of the other EF packages above instead.MiniProfiler.EF
MiniProfiler.MVC3
- Profile MVC 3 websitesMiniProfiler.MVC4
- Profile MVC 4 and 5 websitesMiniProfiler.Raven
- ProfileRavenDb connections
PM> Install-Package MiniProfiler
Write out our css and javascript includes just before closing the body tag:
@using StackExchange.Profiling;<head> ..</head><body> ... @MiniProfiler.RenderIncludes()</body>
For requests that aren't being profiled, nothing will be written.
- Decide when you want to profile; perhaps for local requests, perhaps for special users (developers, admins, test team, whatever).
The sample project starts profiling in Global.asax.cs
Application_BeginRequest
:
using StackExchange.Profiling;... protected void Application_BeginRequest(){ if (Request.IsLocal) { MiniProfiler.Start(); } }
For the code you want to profile, use (regardless of whether the profiler
isnull
or not):
using StackExchange.Profiling;...var profiler = MiniProfiler.Current; // it's ok if this is nullusing (profiler.Step("Set page title")){ViewBag.Title = "Home Page";}using (profiler.Step("Doing complex stuff")){using (profiler.Step("Step A")){ // something more interesting hereThread.Sleep(100);}using (profiler.Step("Step B")){ // and hereThread.Sleep(250);}}
You can make this as granular or high-level as you like; passing a MiniProfiler
in as an optional argument (defaulting tonull
) to downstream methods works well.
Stop the profiler; the sample project does this in its Global.asax.cs
:
protected void Application_EndRequest(){ MiniProfiler.Stop();}
If all goes well, you'll see something like this:
Database profiling
The profiler includes powerful and comprehensive database profiling capabilities. To enable wrap your database connection with a profiling connection.
The built in database profiler supports any kind of DbConnection. It also supports Entity Framework and Linq-2-SQL.
Usage:
Use a factory to return your connection:
public static DbConnection GetOpenConnection(){ var cnn = CreateRealConnection(); // A SqlConnection, SqliteConnection ... or whatever // wrap the connection with a profiling connection that tracks timings return new StackExchange.Profiling.Data.ProfiledDbConnection(cnn, MiniProfiler.Current);}
partial class DBContext{ public static DBContext Get() { var conn = new StackExchange.Profiling.Data.ProfiledDbConnection(GetConnection(), MiniProfiler.Current); return new DBContext(conn); }}
Install the MiniProfiler.EF6 nuget and use one line of code to initialize for all connections in the app:
using StackExchange.Profiling;... protected void Application_Start(){ MiniProfilerEF6.Initialize();}
Install the MiniProfiler.EF5 nuget and use the following code to instantiate your connection:
using StackExchange.Profiling.Data;...public static MyModel Get(){var conn = new EFProfiledDbConnection(GetConnection(), MiniProfiler.Current);return ObjectContextUtils.CreateObjectContext<MyModel>(conn);}
Entity Framework - Code FirstMVC Mini Profiler can be hooked in to an EF code first project. To do so:
Install the Nuget MiniProfiler.EF5 package:
Run: MiniProfilerEF.Initialize(); // in Application_Start
That's it ... you are done!
Note: EF Code First will store table metadata in a table called: !EdmMetadata. This metadata uses the provider as part of the entity key. If you initialized your provider as a non-profiled provider, you will have to re-build this metadata. Deleting all the rows from !EdmMetadata may do the trick, alternatively some smarter providers are able to handle this transparently.
EF 4.1 Update 1
Note, that EF 4.1 Update 1 (the version currently on NuGet) has a breaking change which throws the following error when specifying a connection string:
The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
MiniProfiler attempts to resolve this issue by detecting the version of Entity Framework that it is running against. If this doesn't work (due to security exceptions), force the hack to be applied by replacing theInitialize()
call with:
MiniProfilerEF.Initialize_EF42(); // in Application_Start
However, this is currently unable to profile SqlCE or Oracle databases.
To work around this an additional parameter has been added to the Initialize
call:
- If you specify connection strings explicitly in your
web.config
, useMiniProfilerEF.Initialize();
. This will not profile SqlCE or Oracle databases - If you don't specify connection strings (automatically inferred) and want SqlCE support, use
MiniProfilerEF.Initialize(false);
.
Note, this should be a temporary solution until EF 4.2 is widely available.
Include the MiniProfiler.Raven nuget package and call MiniProfilerRaven.InitializeFor(store)
when you initialize your RavenDocumentStore
(please note that EmbeddableDocumentStore
is not supported).
var store = new DocumentStore();store.Initialize();// Initialize MiniProfilerMiniProfilerRaven.InitializeFor(store);
Various features
(N+1) and duplicate query detection===
The profiler is able to detect and highlight areas where you are executing the same query multiple times with different parameters. This allows you to quickly find queries you may be able to batch.
What about AJAX?
The profiler is also able to log all ajax calls:
You will get one profile timing per ajax call.
Abandoning a profiler session
Often at the beginning of a session you may not know if a user should or should not be allowed to profile, a common pattern for dealing with such issues is:
protected void Application_BeginRequest(){ MvcMiniProfiler.MiniProfiler.Start(); }protected void Application_AuthenticateRequest(Object sender, EventArgs e){ if(!CurrentUserIsAllowedToSeeProfiler()) {MvcMiniProfiler.MiniProfiler.Stop(discardResults: true); }}
See the Global.asax.cs
file in the Sample.MVC project for a demonstration of more config options that can be used.
Basic Troubleshooting
MiniProfiler isn't showing
- Run through the Getting Started steps again and double-check
- If your
web.config
has the settingrunAllManagedModulesForAllRequests=false
then add the following to yourweb.config
too:
<system.webServer> ... <handlers> <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" /> </handlers></system.webServer>
Where is MiniProfiler used?
MiniProfiler was designed by the team at Stack Overflow. It is in production use there and on the Stack Exchange family of sites.
Also used at:
- acturent.com
- jobseriously.com
- plane9.com
- GoDaddy.com
(if you are using this in production please let us know and we will add you to the list)
Frequently Asked Questions
If you have other questions, please ask them on Stack Overflow or community.miniprofiler.com.
- MiniProfiler用法(转)
- MiniProfiler 安装与设置
- MiniProfiler执行SQL语句
- MiniProfiler with ASP.NET WebForms
- MiniProfiler网站性能监测工具
- MiniProfiler 兼容 Entity Framework 6
- petapoco 使用 MiniProfiler Glimpse监控
- asp.net mvc3 route debug and MiniProfiler
- MiniProfiler 网站性能测试工具使用
- MiniProfiler在.Net MVC中使用
- MiniProfiler监控EF与.NET MVC项目
- 使用MiniProfiler调试ASP.NET MVC网站性能
- 使用MiniProfiler调试ASP.NET MVC网站性能
- 使用MiniProfiler调试ASP.NET MVC网站性能
- [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler
- asp.net mvc ef 性能监控调试工具 MiniProfiler
- 采用MiniProfiler监控EF与.NET MVC项目
- sprintf用法(转)
- 互联网思维:3个段子,9大分类,19条法则
- Querying Oracle BPM Process Instances
- 软件测试工具MonkeyTalk使用方法
- Qt下编译警告unused parameter ,参数未使用
- ATL学习笔记(2): ATL对象多线程访问临界锁的实现
- MiniProfiler用法(转)
- Hive学习之Hive数据库DDL
- 去掉多余的tableview line
- DSPLINK DEMO解析之SCALE【转】
- Postel ActiveX Control支持Postel的ActiveX控件
- PL/SQL 查询中文乱码问题
- 字符流 vs 字节流
- Android页面切换时的动画效果(overridePendingTransition)
- ATL学习笔记(3): QueryInterface功能的实现