如何提高ASP.NET MVC应用程序的性能?
如何你改善你的ASP.NET MVC应用程序的性能?
的改善可能来源的汇总列表是如下:
A compiled list of possible sources of improvement are below:
常规
- 请使用探查,发现在你的应用程序的内存泄漏和性能问题。我个人建议 dotTrace
- 运行在Release模式,而不是调试模式下你的网站,当生产,同时在性能分析。释放模式要快得多。调试模式可以隐藏自己的code性能问题。
缓存
- 使用
CompiledQuery.Compile()
避免递归
您的查询重新编译
前pressions - 缓存不是易发到变化
使用内容OutputCacheAttribute
节省了不必要的和行动
执行 - 为经常访问的非敏感信息,利用cookies
- 和过期 - 编写自定义
的ActionResult
方法,如果有必要 - 考虑使用
RouteName
来组织你的路线,然后用它来生成
你的链接,并尽量不要使用ex pression基于树的方法ActionLink的。 - 考虑实施一个路由解析缓存策略
- 将重复code里面的
PartialViews
,避免使它的 XXXX 的时间:如果你
最终调用相同的部分300倍,在同样的观点,有可能是什么
错。 Explanation和基准
- Use
CompiledQuery.Compile()
recursively avoiding recompilation of your query expressions - Cache not-prone-to-change
content using
OutputCacheAttribute
to save unnecessary and action executions - Use cookies for frequently accessed non sensitive information
- Utilize ETags and expiration - Write your custom
ActionResult
methods if necessary - Consider using the
RouteName
to organize your routes and then use it to generate your links, and try not to use the expression tree based ActionLink method. - Consider implementing a route resolution caching strategy
- Put repetitive code inside your
PartialViews
, avoid render it xxxx times: if you end up calling the same partial 300 times in the same view, probably there is something wrong with that. Explanation And Benchmarks
路由
-
使用
Url.RouteUrl(用户,新{用户名=为JoeUser})
指定路线。 ASP.NET MVC性能比较由鲁迪Benkovic
缓存路径使用这个辅助解决 UrlHelperCached
ASP.NET MVC性能比较由鲁迪Benkovic
Cache route resolving using this helper UrlHelperCached
ASP.NET MVC Perfomance by Rudi Benkovic
安全
- 使用窗体身份验证,请你将经常访问的敏感数据
身份验证票证
DAL
- 当访问通过LINQ 依靠IQueryable的 数据
- Leverage Repository模式
- 资料查询即尤伯杯探查
- 考虑二级缓存为您查询,并添加它们的范围和超时即NHibernate第二缓存
- When accessing data via LINQ rely on IQueryable
- Leverage the Repository pattern
- Profile your queries i.e. Uber Profiler
- Consider second level cache for your queries and add them an scope and a timeout i.e. NHibernate Second Cache
负载均衡
-
利用反向代理,为s $ P $垫在您的应用程序实例的客户端负载。 (堆栈溢出使用 HAProxy的(MSDN).
使用异步控制器来实施该行动依赖于外部资源的处理。
Use Asynchronous Controllers to implement actions that depend on external resources processing.
客户端
- 优化您的客户端,使用像 YSlow的一种工具
建议以提高性能 - 使用AJAX来更新你的UI组件,尽可能避免整个页面更新。
- 实施考虑发布 - 订阅架构-i.e. Comet-反对内容交付
重装总部设在超时。 - 移动的图表和图形生成逻辑到客户端,如果可能的。图形生成
是一个昂贵的活动。推迟到客户端服务器从
不必要的负担,并允许您使用图形本地工作没有做出新的
请求(即Flex图表, jqbargraph ,MoreJqueryCharts). - 使用CDN对脚本和媒体内容,以提高在客户端侧负载(即谷歌CDN 一>)
- 缩减大小 - 编译 - 你以提高你的脚本大小的JavaScript
- 保留cookie的体积小,因为cookie发送到在每次请求的服务器。
- 考虑使用 DNS和链接prefetching 时可能。
- Optimize your client side, use a tool like YSlow for suggestions to improve performance
- Use AJAX to update components of your UI, avoid a whole page update when possible.
- Consider implement a pub-sub architecture -i.e. Comet- for content delivery against reload based in timeouts.
- Move charting and graph generation logic to the client side if possible. Graph generation is a expensive activity. Deferring to the client side your server from an unnecessary burden, and allows you to work with graphs locally without make a new request (i.e. Flex charting, jqbargraph, MoreJqueryCharts).
- Use CDN's for scripts and media content to improve loading on the client side (i.e. Google CDN)
- Minify -Compile- your JavaScript in order to improve your script size
- Keep cookie size small, since cookies are sent to the server on every request.
- Consider using DNS and Link Prefetching when possible.
全局配置
-
如果您使用剃刀,添加以下code在你的Global.asax.cs,默认情况下,Asp.Net MVC以呈现一个aspx发动机和剃刀引擎。这仅使用RazorViewEngine。
If you use Razor, add the following code in your global.asax.cs, by default, Asp.Net MVC renders with an aspx engine and a razor engine. This only uses the RazorViewEngine.
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(新RazorViewEngine());
添加的gzip(HTTP COM pression)和静态缓存(图片,CSS,...)< system.webServer>
< urlCom pression doDynamicCom pression =真doStaticCom pression =真dynamicCom pressionBeforeCache =真/>
< /system.webServer>
Add gzip (HTTP compression) and static cache (images, css, ...) in your web.config
<system.webServer>
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
</system.webServer>