调试ASP.NET MVC网站性能
使用MiniProfiler调试ASP.NET MVC网站性能
MVC MiniProfiler是 Stack Overflow 团队设计的一款对ASP.NET MVC的性能分析的小程序。可以对一个页面本身,及该页面通过直接引用、Ajax、Iframe形式访问的其它页面进行监控,监控内容包括数据库内容,并可以显示数据库访问的SQL(支持EF、EF CodeFirst等 )。并且以很友好的方式展现在页面上。
该Profiler的一个特别有用的功能是它与数据库框架的集成。除了.NET原生的 DbConnection类,profiler还内置了对实体框架(Entity Framework)以及LINQ to SQL的支持。任何执行的Step都会包括当时查询的次数和所花费的时间。为了检测常见的错误,如N+1反模式,profiler将检测仅有参数值存在差 异的多个查询。
MiniProfiler 是以Apache License V2.0协议发布的,你可以在NuGet找到。配置及使用可以看这里: http://code.google.com/p/mvc-mini-profiler
为建立快速的网站黄金参考标准,雅虎2007年为 网站提高速度的13个简易规则 。
Stack Overflow 用MVC Mini Profiler来促进开源,而在把每一页的右上角服务器渲染时间的简单行来迫使我们解决我们所有的性能衰退和遗漏。如果你在使用.NET开发应用,一定要使用上这个工具。
包括以下核心组件:
MiniProfiler MiniProfiler.EntityFramework如何安装?
一、环境准备 Visual Studio 2010 ASP.NET MVC项目 如果需要调试EF,建议升级到Entity Framework 4.2 二、安装推荐使用 NuGet 方式进行安装,参考文章《 使用 NuGet 管理项目库 》
第一步:在引用上右键选择“Manage NuGet Packages”第二步:Online搜索miniprofiler
MiniProfiler、MiniProfiler.EF、MiniProfiler.MVC3,同时会自动安装依赖组件:WebActivator, 同时也会自动在项目里面添加代码文件:MiniProfiler.cs
第三步:修改代码使MiniProfiler生效在global.cs的Application_Start事件里面增加代码: StackExchange.Profiling.MiniProfilerEF.Initialize();
修改View的layout文件,在head区域增加如下代码:@StackExchange.Profiling.MiniProfiler.RenderIncludes()
如果安装步骤一切顺利的话,打开站点的时候,就可以在左上角看到页面执行时间了,点开可以看到更详细的信息,如果有SQL的话,还会显示SQL语句信息,非常的方便。 页面上如果有ajax请求,也会同时显示到左上角。如果左上角显示红色提示,则表示可能存在性能问题需要处理:
点开SQL部分,可以看到详细的SQL语句
标记为duplicate的部分,代表在一次请求当中,重复执行了查询,可以优化。
问题:
1、在结合使用EF 4.3的时候发生如下错误:
Invalid object name 'dbo.__MigrationHistory'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at MvcMiniProfiler.Data.ProfiledDbCommand.ExecuteDbDataReader(CommandBehavior behavior) in \mvc-mini-profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:line 155
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
需要在EF 4.3上关闭数据库初始化策略:
public class SettingContext : DbContext
{
static SettingContext()
{
Database.SetInitializer<SettingContext>(null);
}
WinRT:WebAuthenticationBroker For OAuth认证
之前开发过 QQ互联的OAuth 的.NET/Mono/Windows Phone SDK,具体可以 QQ互联OAuth2.0 .NET SDK 发布以及网站QQ登陆示例代码 。到了Windows 8的Metro 程序如何使用QQ互联的API呢,今天做了一些实验性的代码验证。使用OAuth的最大挑战就是获得AccessToken,在OAuth的一个App AccessToken从本质上来说就是用户的验证登陆和用户的权限授权,获取到用户的accessToken后,就可以使用AccessToken 对REST API发送请求了。
微软在Metro/WinRT里头已经完整的包括上述OAuth验证的库,叫做WebAuthenticationBroker,他为我们完成大量的工作,例如下面的图,在WP7上使用一个WebBrower来玩吃的,Metro里头为们提供了无边框的浏览器窗口。
这个框框看起来不太好看,期望QQ互联能够检测到这里从Windows8的Metro UI发起的请求,展现更加Metro的UI。
我们来看下代码,不由得感叹这代码太漂亮了,在WebAuthenticationBroker上await,它返回一个对象,指示用户是否取消,发生了错误,还是成功了。
System.Uri StartUri = new Uri(TencentURL);
System.Uri EndUri = new Uri(this.TencentCallbackUrl.Text);
DebugPrint("Navigating to: " + FacebookURL);
WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
StartUri,
EndUri);
if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
OutputToken(WebAuthenticationResult.ResponseData.ToString());
}
else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
{
OutputToken("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString());
}
else
{
OutputToken("Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString());
}
}
这代码非常的简洁,一个很酷的机制,我们没有以任何方式附加到UI代码,它只是注入到自身的一个UI上。它的原理请看MSDN文档 How web authentication broker works ,相应的代码示例参考 Web authentication broker sample ,微软在这些例子里实现几个国外的例子: Facebook,twitter,Windows Live,Google ,Flickr服务,其中没有本地化的服务例子,今天我就在这些例子的基础上初步验证了QQ互联的API,同样的应该也适用于人人,新浪,阿里,百度等的开放平台。
作者: 自由、创新、研究、探索……
出处: http://shanyou.cnblogs.com/
版权:本文版权归作者和博客园共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
分类: Windows 8 , WinRT
绿色通道: 好文要顶 已关注 收藏该文
作者: Leo_wl
出处: http://www.cnblogs.com/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于调试ASP.NET MVC网站性能的详细内容...