如何改进ASP。净MVC应用程序的性能?

时间:2022-06-01 13:04:17

How do you improve your ASP.NET MVC application performance?

如何改进ASP。净MVC应用程序的性能?

16 个解决方案

#1


285  

A compiled list of possible sources of improvement are below:

以下是可能的改善来源的汇编清单:

General

一般

  • Make use of a profiler to discover memory leaks and performance problems in your application. personally I suggest dotTrace
  • 使用分析器发现应用程序中的内存泄漏和性能问题。我个人建议dotTrace
  • Run your site in Release mode, not Debug mode, when in production, and also during performance profiling. Release mode is much faster. Debug mode can hide performance problems in your own code.
  • 在生产环境中运行站点时,在发布模式下运行,而不是在调试模式下,在性能分析期间也要运行站点。发布模式要快得多。调试模式可以在您自己的代码中隐藏性能问题。

Caching

缓存

  • Use CompiledQuery.Compile() recursively avoiding recompilation of your query expressions
  • 使用CompiledQuery.Compile()递归地避免重新编译查询表达式
  • Cache not-prone-to-change content using OutputCacheAttribute to save unnecessary and action executions
  • 使用OutputCacheAttribute缓存非prto -change内容,以节省不必要的和操作的执行。
  • Use cookies for frequently accessed non sensitive information
  • 为经常访问的非敏感信息使用cookie。
  • Utilize ETags and expiration - Write your custom ActionResult methods if necessary
  • 利用ETags和expiration——如果需要的话,编写自定义ActionResult方法
  • 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.
  • 考虑使用RouteName来组织路由,然后使用它来生成链接,尽量不使用基于表达式树的ActionLink方法。
  • 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
  • 将重复的代码放在PartialViews中,避免渲染xxxx次:如果您最终在相同的视图中调用相同的部分300次,那么这可能有问题。解释和基准

Routing

路由

  • Use Url.RouteUrl("User", new { username = "joeuser" }) to specify routes. ASP.NET MVC Perfomance by Rudi Benkovic

    使用Url。RouteUrl(“User”,new {username = "joeuser"})指定路由。ASP。鲁迪·本科维奇的MVC风格

  • Cache route resolving using this helper UrlHelperCached ASP.NET MVC Perfomance by Rudi Benkovic

    使用这个帮助程序UrlHelperCached ASP来解析缓存路由。鲁迪·本科维奇的MVC风格

Security

安全

  • Use Forms Authentication, Keep your frequently accessed sensitive data in the authentication ticket
  • 使用表单身份验证,将经常访问的敏感数据保存在身份验证票据中

DAL

木豆

  • When accessing data via LINQ rely on IQueryable
  • 当通过LINQ访问数据时,依赖于IQueryable
  • Leverage the Repository pattern
  • 利用库模式
  • Profile your queries i.e. Uber Profiler
  • 描述您的查询,例如Uber Profiler
  • Consider second level cache for your queries and add them an scope and a timeout i.e. NHibernate Second Cache
  • 考虑查询的二级缓存,并为它们添加范围和超时,即NHibernate二级缓存

Load balancing

负载平衡

  • Utilize reverse proxies, to spread the client load across your app instance. (Stack Overflow uses HAProxy (MSDN).

    利用反向代理,将客户端负载分散到应用程序实例中。栈溢出使用HAProxy (MSDN)。

  • Use Asynchronous Controllers to implement actions that depend on external resources processing.

    使用异步控制器实现依赖于外部资源处理的操作。

Client side

客户端

  • Optimize your client side, use a tool like YSlow for suggestions to improve performance
  • 优化客户端,使用像YSlow这样的工具来获得改进性能的建议
  • Use AJAX to update components of your UI, avoid a whole page update when possible.
  • 使用AJAX更新UI组件,尽可能避免整个页面更新。
  • Consider implement a pub-sub architecture -i.e. Comet- for content delivery against reload based in timeouts.
  • 考虑实现一个发布子体系结构(即Comet),用于基于超时的内容交付和重载。
  • 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).
  • 如果可能的话,将图表和图形生成逻辑移动到客户端。图形生成是一项昂贵的活动。将服务器推迟到客户端,从而避免了不必要的负担,并允许您在本地使用图,而无需发出新的请求(例如Flex制图、jqbargraph、MoreJqueryCharts)。
  • Use CDN's for scripts and media content to improve loading on the client side (i.e. Google CDN)
  • 对于脚本和媒体内容使用CDN以改进客户端(即谷歌CDN)的加载。
  • Minify -Compile- your JavaScript in order to improve your script size
  • 缩小-编译-你的JavaScript,以提高你的脚本尺寸。
  • Keep cookie size small, since cookies are sent to the server on every request.
  • 保持小的cookie大小,因为每个请求都将cookie发送到服务器。
  • Consider using DNS and Link Prefetching when possible.
  • 考虑在可能的情况下使用DNS和链接预取。

Global configuration

全局配置

  • 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.

    如果您使用Razor,请在您的global.asax中添加以下代码。cs,默认情况下,Asp。Net MVC渲染使用一个aspx引擎和一个剃须刀引擎。这只使用RazorViewEngine。

    ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new RazorViewEngine());

    ViewEngines.Engines.Clear();ViewEngines.Engines。添加(新RazorViewEngine());

  • Add gzip (HTTP compression) and static cache (images, css, ...) in your web.config <system.webServer> <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/> </system.webServer>

    在web中添加gzip (HTTP压缩)和静态缓存(图像、css…)。配置 <系统。webserver> < system.webServer> .webServer

  • Remove unused HTTP Modules
  • 删除未使用的HTTP模块
  • Flush your HTML as soon as it is generated (in your web.config) and disable viewstate if you are not using it <pages buffer="true" enableViewState="false">
  • 在生成HTML(在web.config中)后立即刷新它,如果不使用它,则禁用viewstate

#2


10  

Code Climber and this blog entry provide detailed ways of increasing application's performance.

代码爬虫和这个博客条目提供了提高应用程序性能的详细方法。

Compiled query will increase performance of your application, but it has nothing in common with ASP.NET MVC. It will speed up every db application, so it is not really about MVC.

编译查询将提高应用程序的性能,但它与ASP没有任何共同之处。净MVC。它会加速每个db应用程序,所以它并不是真正的MVC。

#3


10  

The basic suggestion is to follow REST principles and the following points ties some of these principals to the ASP.NET MVC framework:

基本的建议是遵循REST原则,以下几点将这些原则与ASP联系起来。NET MVC框架:

  1. Make your controllers stateless - this is more of a 'Web performance / scalability' suggestion (as opposed to micro/machine level performance) and a major design decision that would affect your applications future - especially in case it becomes popular or if you need some fault tolerance for example.
    • Do not use Sessions
    • 不要使用会话
    • Do not use tempdata - which uses sessions
    • 不要使用tempdata——使用会话
    • Do not try to 'cache' everything 'prematurely'.
    • 不要试图“过早地”缓存所有内容。
  2. 让你的控制器无状态——这更多的是一个“Web性能/可伸缩性”建议(相对于微/机器级性能)和一个影响应用程序未来的重大设计决策——特别是在它变得流行或者需要一些容错的情况下。不要使用会话不要使用tempdata——它使用会话不要试图“缓存”所有的“过早”。
  3. Use Forms Authentication
    • Keep your frequently accessed sensitive data in the authentication ticket
    • 将经常访问的敏感数据保存在身份验证票据中
  4. 使用表单身份验证将经常访问的敏感数据保存在身份验证票据中
  5. Use cookies for frequently accessed non sensitive information
  6. 为经常访问的非敏感信息使用cookie。
  7. Make your resources cachable on the web
    • Utilize ETags
    • 利用etag
    • Use expiration
    • 使用过期
    • Write your custom ActionResult classes if necessary
    • 如果需要的话,编写您的自定义ActionResult类
    • Utilize reverse proxies
    • 利用反向代理
  8. 如果需要使用反向代理,请让您的资源可在web上使用ETags使用过期编写您的自定义ActionResult类。
  9. Compile your JavaScript. There is Closure compiler library to do it as well (sure there are others, just search for 'JavaScript compiler' too)
  10. 编译您的JavaScript。也有闭包编译器库(当然还有其他的,只需要搜索'JavaScript编译器')
  11. Use CDNs (Content Delivery Network) - especially for your large media files and so on.
  12. 使用CDNs(内容传递网络)-特别是对于你的大型媒体文件等等。
  13. Consider different types of storage for your data, for example, files, key/value stores, etc. - not only SQL Server
  14. 考虑不同类型的数据存储,例如文件、密钥/值存储等等——不仅仅是SQL Server
  15. Last but not least, test your web site for performance
  16. 最后,测试web站点的性能

#4


7  

This may seem obvious, but run your site in Release mode, not Debug mode, when in production, and also during performance profiling. Release mode is much faster. Debug mode can hide performance problems in your own code.

这看起来很明显,但是在发布模式下运行站点,而不是在生产环境中运行调试模式,在性能分析期间也是如此。发布模式要快得多。调试模式可以在您自己的代码中隐藏性能问题。

#5


6  

When accessing data via LINQ rely on IQueryable ...

当通过LINQ访问数据时,依赖于IQueryable…

Why use AsQueryable() instead of List()?

为什么使用可查询的()而不是List()?

... and leverge a good Repository pattern:

…利用一个好的存储库模式:

Loading Subrecords in the Repository Pattern

在存储库模式中加载子记录

This will optimize data access to ensure only the data needed is loaded and when only it is needed.

这将优化数据访问,以确保只加载所需的数据,并且只在需要的时候加载。

#6


6  

Not an earth-shattering optimization, but I thought I'd throw this out there - Use CDN's for jQuery, etc..

这不是一个惊天动地的优化,但我想我应该把它扔出去——使用CDN的jQuery,等等。

Quote from ScottGu himself: The Microsoft Ajax CDN enables you to significantly improve the performance of ASP.NET Web Forms and ASP.NET MVC applications that use ASP.NET AJAX or jQuery. The service is available for free, does not require any registration, and can be used for both commercial and non-commercial purposes.

引用ScottGu自己的话:Microsoft Ajax CDN使您能够显著提高ASP的性能。NET Web表单和ASP。使用ASP的MVC应用程序。净AJAX或jQuery。该服务是免费的,不需要任何注册,可以用于商业和非商业目的。

We even use the CDN for our webparts in Moss that use jQuery.

我们甚至在使用jQuery的Moss中的web部件上使用CDN。

#7


6  

Also if you use NHibernate you can turn on and setup second level cache for queries and add to queries scope and timeout. And there is kick ass profiler for EF, L2S and NHibernate - http://hibernatingrhinos.com/products/UberProf. It will help to tune your queries.

另外,如果您使用NHibernate,您可以打开并设置查询的二级缓存,并添加到查询范围和超时。还有EF、L2S和NHibernate的kick ass profiler——http://hibernatingrhinos.com/products/UberProf。它将有助于优化您的查询。

#8


4  

In addition to all the great information on optimising your application on the server side I'd say you should take a look at YSlow. It's a superb resource for improving site performance on the client side.

除了关于在服务器端优化应用程序的所有重要信息之外,我还建议您看看YSlow。它是提高客户端站点性能的极好资源。

This applies to all sites, not just ASP.NET MVC.

这适用于所有站点,而不仅仅是ASP。净MVC。

#9


4  

I will also add:

我还会添加:

  1. Use Sprites: Sprites are a great thing to reduce a request. You merge all your images into a single one and use CSS to get to good part of the sprite. Microsoft provides a good library to do it: Sprite and Image Optimization Preview 4.

    使用精灵:精灵是减少请求的好方法。您将所有的图像合并到一个单独的图像中,并使用CSS来获得该精灵的良好部分。微软提供了一个很好的程序库:Sprite和图像优化预览4。

  2. Cache Your server object: If you have some references lists or data which will change rarely, you can cache them into memory instead of querying database every time.

    缓存服务器对象:如果您有一些引用列表或数据很少变化,那么您可以将它们缓存到内存中,而不是每次都查询数据库。

  3. Use ADO.NET instead of Entity Framework: EF4 or EF5 are great to reduce development time, but it will be painful to optimize. It's more simple to optimize a stored procedure than Entity Framework. So you should use store procedures as much as possible. Dapper provides a simple way to query and map SQL with very good performance.

    使用ADO。NET而不是实体框架:EF4或EF5可以减少开发时间,但是优化是痛苦的。优化存储过程比优化实体框架更简单。所以你应该尽可能多地使用存储过程。Dapper提供了一种简单的查询和映射SQL的方法,具有非常好的性能。

  4. Cache Page or partial page: MVC provides some easy filter to cache page according to some parameters, so use it.

    缓存页面或部分页面:MVC提供了一些简单的过滤器来缓存页面,根据一些参数,所以使用它。

  5. Reduce Database calls: You can create a unique database request that returns multiple objects. Check on Dapper website.

    减少数据库调用:可以创建一个返回多个对象的唯一数据库请求。衣冠楚楚的网站上检查。

  6. Always have a clean architecture: Have a clean n-tiers architecture, even on a small project. It will help you to keep your code clean, and it will be easier to optimize it if needed.

    总是有一个干净的架构:有一个干净的n层架构,即使是一个小的项目。它将帮助您保持代码的整洁,并且在需要时更容易优化它。

  7. You can take a look at this template "Neos-SDI MVC Template" which will create a clean architecture for you with lots of performance improvements by default (check MvcTemplate website).

    您可以查看这个模板“Neos-SDI MVC模板”,它将为您创建一个干净的体系结构,并在默认情况下进行大量性能改进(请参阅MvcTemplate网站)。

#10


3  

One super easy thing to do is to think asynchronously when accessing the data you want for the page. Whether reading from a web service, file, data base or something else, use the async model as much as possible. While it won't necessarily help any one page be faster it will help your server perform better overall.

一件非常简单的事情是在访问页面所需的数据时进行异步思考。无论是从web服务、文件、数据库还是其他什么地方读取,都要尽可能多地使用异步模型。虽然它不一定能帮助任何一个页面更快,但它将帮助您的服务器更好地整体执行。

#11


2  

1: Get Timings. Until you know where the slowdown is, the question is too broad to answer. A project I'm working on has this precise problem; There's no logging to even know how long certain things take; we can only guess as to the slow parts of the app until we add timings to the project.

1:计时。除非你知道经济放缓在哪里,否则这个问题的答案太宽泛了。我正在做的一个项目就有这个问题;甚至不需要记录某些事情需要多长时间;我们只能猜测应用程序的缓慢部分,直到我们将时间添加到项目中。

2: If you have sequential operations, Don't be afraid to lightly multithread. ESPECIALLY if blocking operations are involved. PLINQ is your friend here.

2:如果你有顺序操作,不要害怕轻点多线程。特别是如果涉及到阻塞操作。PLINQ是你的朋友。

3: Pregenerate your MVC Views when Publishing... That will help with some of the 'first page hit'

3:发布时预生成MVC视图……这将有助于一些“第一页点击”

4: Some argue for the stored procedure/ADO advantages of speed. Others argue for speed of development of EF and a more clear seprataion of tiers and their purpose. I've seen really slow designs when SQL and the workarounds to use Sprocs/Views for data retrieval and storage. Also, your difficulty to test goes up. Our current codebase that we are converting from ADO to EF is not performing any worse (and in some cases better) than the old Hand-Rolled model.

4:有人认为存储过程/ADO具有速度优势。另一些人则认为,英孚的发展速度,以及更明确的层次和目的。当SQL和工作区使用Sprocs/ view进行数据检索和存储时,我见过设计非常缓慢。而且,你的测试难度也会增加。我们目前正在从ADO到EF的代码库,在某些情况下不会比旧的手动模式更糟糕(在某些情况下更好)。

5: That said, Think about application Warmup. Part of what we do to help eliminate most of our EF performance woes was to add a special warmup method. It doesn't precompile any queries or anything, but it helps with much of the metadata loading/generation. This can be even more important when dealing with Code First models.

话虽如此,想想应用程序热身吧。我们为消除大部分EF性能问题所做的部分工作是添加一个特殊的预热方法。它不预编译任何查询或任何东西,但是它有助于加载/生成元数据。在处理代码优先模型时,这一点可能更为重要。

6: As others have said, Don't use Session state or ViewState if possible. They are not necessarily performance optimizations that developers think about, but once you start writing more complex web applications, you want responsiveness. Session state precludes this. Imagine a long running query. You decide to open a new window and try a less complex one. Well, you may as well have waited with session state on, because the server will wait until the first request is done before moving to the next one for that session.

6:正如其他人所说,如果可能,不要使用会话状态或ViewState。它们不一定是开发人员考虑的性能优化,但是一旦您开始编写更复杂的web应用程序,您就需要响应性。会话状态排除了这一点。想象一个长时间运行的查询。你决定打开一个新窗口,尝试一个不那么复杂的窗口。当然,您也可以等待会话状态打开,因为服务器将等到第一个请求完成后,才会移动到下一个会话。

7: Minimize round trips to the database. Save stuff that you frequently use but will not realistically change to your .Net Cache. Try to batch your inserts/updates where possible.

7:尽量减少往返数据库的次数。保存您经常使用的内容,但不会实际更改。net缓存。尽可能批量插入/更新。

7.1: Avoid Data Access code in your Razor views without a damn good reason. I wouldn't be saying this if I hadn't seen it. They were already accessing their data when putting the model together, why the hell weren't they including it in the model?

7.1:在你的剃刀视图中避免数据访问代码,没有任何理由。如果我没看到的话,我就不会这么说了。当他们把模型放在一起的时候,他们已经在访问他们的数据了,为什么他们不把它包括在模型里呢?

#12


2  

Just wanted to add my 2 cents. The MOST effective way to optimize the URL route generation in an MVC application is... not generate them at all.

我只想加上2分。优化MVC应用程序中URL路由生成的最有效方法是……根本不生成它们。

Most of us more or less know how URLs are generated in our apps anyway, so simply using static Url.Content("~/Blahblah") instead of Url.Action() or Url.RouteUrl() where possible, beats all other methods by almost 20 times and even more.

我们大多数人或多或少都知道url是如何在应用程序中生成的,所以只要使用静态Url.Content(“~/Blahblah”)而不是Url.Action()或Url.RouteUrl(),只要可能的话,就可以比所有其他方法多出20倍甚至更多。

PS. I've ran a benchmark of couple of thousand iterations and posted results on my blog if interested.

我已经运行了几千次迭代的基准测试,如果有兴趣的话,可以在我的博客上发布结果。

#13


1  

In your clamour to optimize the client side, don't forget about the database layer. We had an application that went from 5 seconds to load up to 50 seconds overnight.

在优化客户端时,不要忘记数据库层。我们的应用程序从5秒加载到50秒。

On inspection, we'd made a whole bunch of schema changes. Once we refreshed the statistics, it suddenly became as responsive as before.

在检查时,我们进行了大量的模式更改。一旦我们刷新统计数据,它就会突然像以前一样响应。

#14


1  

  1. Implement Gzip.
  2. Gzip实现。
  3. Use asynchronous rendering for partial views.
  4. 对部分视图使用异步呈现。
  5. Minimize database hits.
  6. 减少数据库连接次数。
  7. Use a compiled query.
  8. 使用编译查询。
  9. Run a profiler and find out unnecessary hits. Optimize all stored procedures which are taking more than 1 second to return a response.
  10. 运行一个剖析器,找出不必要的打击。优化所有需要超过1秒才能返回响应的存储过程。
  11. Use caching.
  12. 使用缓存。
  13. Use bundling minification optimization.
  14. 使用捆绑缩小优化。
  15. Use HTML 5 utilities like session cache and local storage for readonly contents.
  16. 使用HTML 5实用工具,如会话缓存和本地存储只读内容。

#15


0  

Following are things to do

以下是要做的事情

  1. Kernel mode Cache
  2. 内核模式缓存
  3. Pipeline mode
  4. 管道模式
  5. Remove unused modules
  6. 删除未使用的模块
  7. runAllManagedModulesForAllRequests
  8. runAllManagedModulesForAllRequests
  9. Don't write in wwwroot
  10. 不要写在wwwroot
  11. Remove unused view engines and language
  12. 删除未使用的视图引擎和语言

#16


0  

Using Bundling and Minification also helps you improve the performance. It basically reduces the page loading time.

使用捆绑和缩小也可以帮助您提高性能。它基本上减少了页面加载时间。

#1


285  

A compiled list of possible sources of improvement are below:

以下是可能的改善来源的汇编清单:

General

一般

  • Make use of a profiler to discover memory leaks and performance problems in your application. personally I suggest dotTrace
  • 使用分析器发现应用程序中的内存泄漏和性能问题。我个人建议dotTrace
  • Run your site in Release mode, not Debug mode, when in production, and also during performance profiling. Release mode is much faster. Debug mode can hide performance problems in your own code.
  • 在生产环境中运行站点时,在发布模式下运行,而不是在调试模式下,在性能分析期间也要运行站点。发布模式要快得多。调试模式可以在您自己的代码中隐藏性能问题。

Caching

缓存

  • Use CompiledQuery.Compile() recursively avoiding recompilation of your query expressions
  • 使用CompiledQuery.Compile()递归地避免重新编译查询表达式
  • Cache not-prone-to-change content using OutputCacheAttribute to save unnecessary and action executions
  • 使用OutputCacheAttribute缓存非prto -change内容,以节省不必要的和操作的执行。
  • Use cookies for frequently accessed non sensitive information
  • 为经常访问的非敏感信息使用cookie。
  • Utilize ETags and expiration - Write your custom ActionResult methods if necessary
  • 利用ETags和expiration——如果需要的话,编写自定义ActionResult方法
  • 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.
  • 考虑使用RouteName来组织路由,然后使用它来生成链接,尽量不使用基于表达式树的ActionLink方法。
  • 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
  • 将重复的代码放在PartialViews中,避免渲染xxxx次:如果您最终在相同的视图中调用相同的部分300次,那么这可能有问题。解释和基准

Routing

路由

  • Use Url.RouteUrl("User", new { username = "joeuser" }) to specify routes. ASP.NET MVC Perfomance by Rudi Benkovic

    使用Url。RouteUrl(“User”,new {username = "joeuser"})指定路由。ASP。鲁迪·本科维奇的MVC风格

  • Cache route resolving using this helper UrlHelperCached ASP.NET MVC Perfomance by Rudi Benkovic

    使用这个帮助程序UrlHelperCached ASP来解析缓存路由。鲁迪·本科维奇的MVC风格

Security

安全

  • Use Forms Authentication, Keep your frequently accessed sensitive data in the authentication ticket
  • 使用表单身份验证,将经常访问的敏感数据保存在身份验证票据中

DAL

木豆

  • When accessing data via LINQ rely on IQueryable
  • 当通过LINQ访问数据时,依赖于IQueryable
  • Leverage the Repository pattern
  • 利用库模式
  • Profile your queries i.e. Uber Profiler
  • 描述您的查询,例如Uber Profiler
  • Consider second level cache for your queries and add them an scope and a timeout i.e. NHibernate Second Cache
  • 考虑查询的二级缓存,并为它们添加范围和超时,即NHibernate二级缓存

Load balancing

负载平衡

  • Utilize reverse proxies, to spread the client load across your app instance. (Stack Overflow uses HAProxy (MSDN).

    利用反向代理,将客户端负载分散到应用程序实例中。栈溢出使用HAProxy (MSDN)。

  • Use Asynchronous Controllers to implement actions that depend on external resources processing.

    使用异步控制器实现依赖于外部资源处理的操作。

Client side

客户端

  • Optimize your client side, use a tool like YSlow for suggestions to improve performance
  • 优化客户端,使用像YSlow这样的工具来获得改进性能的建议
  • Use AJAX to update components of your UI, avoid a whole page update when possible.
  • 使用AJAX更新UI组件,尽可能避免整个页面更新。
  • Consider implement a pub-sub architecture -i.e. Comet- for content delivery against reload based in timeouts.
  • 考虑实现一个发布子体系结构(即Comet),用于基于超时的内容交付和重载。
  • 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).
  • 如果可能的话,将图表和图形生成逻辑移动到客户端。图形生成是一项昂贵的活动。将服务器推迟到客户端,从而避免了不必要的负担,并允许您在本地使用图,而无需发出新的请求(例如Flex制图、jqbargraph、MoreJqueryCharts)。
  • Use CDN's for scripts and media content to improve loading on the client side (i.e. Google CDN)
  • 对于脚本和媒体内容使用CDN以改进客户端(即谷歌CDN)的加载。
  • Minify -Compile- your JavaScript in order to improve your script size
  • 缩小-编译-你的JavaScript,以提高你的脚本尺寸。
  • Keep cookie size small, since cookies are sent to the server on every request.
  • 保持小的cookie大小,因为每个请求都将cookie发送到服务器。
  • Consider using DNS and Link Prefetching when possible.
  • 考虑在可能的情况下使用DNS和链接预取。

Global configuration

全局配置

  • 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.

    如果您使用Razor,请在您的global.asax中添加以下代码。cs,默认情况下,Asp。Net MVC渲染使用一个aspx引擎和一个剃须刀引擎。这只使用RazorViewEngine。

    ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new RazorViewEngine());

    ViewEngines.Engines.Clear();ViewEngines.Engines。添加(新RazorViewEngine());

  • Add gzip (HTTP compression) and static cache (images, css, ...) in your web.config <system.webServer> <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/> </system.webServer>

    在web中添加gzip (HTTP压缩)和静态缓存(图像、css…)。配置 <系统。webserver> < system.webServer> .webServer

  • Remove unused HTTP Modules
  • 删除未使用的HTTP模块
  • Flush your HTML as soon as it is generated (in your web.config) and disable viewstate if you are not using it <pages buffer="true" enableViewState="false">
  • 在生成HTML(在web.config中)后立即刷新它,如果不使用它,则禁用viewstate

#2


10  

Code Climber and this blog entry provide detailed ways of increasing application's performance.

代码爬虫和这个博客条目提供了提高应用程序性能的详细方法。

Compiled query will increase performance of your application, but it has nothing in common with ASP.NET MVC. It will speed up every db application, so it is not really about MVC.

编译查询将提高应用程序的性能,但它与ASP没有任何共同之处。净MVC。它会加速每个db应用程序,所以它并不是真正的MVC。

#3


10  

The basic suggestion is to follow REST principles and the following points ties some of these principals to the ASP.NET MVC framework:

基本的建议是遵循REST原则,以下几点将这些原则与ASP联系起来。NET MVC框架:

  1. Make your controllers stateless - this is more of a 'Web performance / scalability' suggestion (as opposed to micro/machine level performance) and a major design decision that would affect your applications future - especially in case it becomes popular or if you need some fault tolerance for example.
    • Do not use Sessions
    • 不要使用会话
    • Do not use tempdata - which uses sessions
    • 不要使用tempdata——使用会话
    • Do not try to 'cache' everything 'prematurely'.
    • 不要试图“过早地”缓存所有内容。
  2. 让你的控制器无状态——这更多的是一个“Web性能/可伸缩性”建议(相对于微/机器级性能)和一个影响应用程序未来的重大设计决策——特别是在它变得流行或者需要一些容错的情况下。不要使用会话不要使用tempdata——它使用会话不要试图“缓存”所有的“过早”。
  3. Use Forms Authentication
    • Keep your frequently accessed sensitive data in the authentication ticket
    • 将经常访问的敏感数据保存在身份验证票据中
  4. 使用表单身份验证将经常访问的敏感数据保存在身份验证票据中
  5. Use cookies for frequently accessed non sensitive information
  6. 为经常访问的非敏感信息使用cookie。
  7. Make your resources cachable on the web
    • Utilize ETags
    • 利用etag
    • Use expiration
    • 使用过期
    • Write your custom ActionResult classes if necessary
    • 如果需要的话,编写您的自定义ActionResult类
    • Utilize reverse proxies
    • 利用反向代理
  8. 如果需要使用反向代理,请让您的资源可在web上使用ETags使用过期编写您的自定义ActionResult类。
  9. Compile your JavaScript. There is Closure compiler library to do it as well (sure there are others, just search for 'JavaScript compiler' too)
  10. 编译您的JavaScript。也有闭包编译器库(当然还有其他的,只需要搜索'JavaScript编译器')
  11. Use CDNs (Content Delivery Network) - especially for your large media files and so on.
  12. 使用CDNs(内容传递网络)-特别是对于你的大型媒体文件等等。
  13. Consider different types of storage for your data, for example, files, key/value stores, etc. - not only SQL Server
  14. 考虑不同类型的数据存储,例如文件、密钥/值存储等等——不仅仅是SQL Server
  15. Last but not least, test your web site for performance
  16. 最后,测试web站点的性能

#4


7  

This may seem obvious, but run your site in Release mode, not Debug mode, when in production, and also during performance profiling. Release mode is much faster. Debug mode can hide performance problems in your own code.

这看起来很明显,但是在发布模式下运行站点,而不是在生产环境中运行调试模式,在性能分析期间也是如此。发布模式要快得多。调试模式可以在您自己的代码中隐藏性能问题。

#5


6  

When accessing data via LINQ rely on IQueryable ...

当通过LINQ访问数据时,依赖于IQueryable…

Why use AsQueryable() instead of List()?

为什么使用可查询的()而不是List()?

... and leverge a good Repository pattern:

…利用一个好的存储库模式:

Loading Subrecords in the Repository Pattern

在存储库模式中加载子记录

This will optimize data access to ensure only the data needed is loaded and when only it is needed.

这将优化数据访问,以确保只加载所需的数据,并且只在需要的时候加载。

#6


6  

Not an earth-shattering optimization, but I thought I'd throw this out there - Use CDN's for jQuery, etc..

这不是一个惊天动地的优化,但我想我应该把它扔出去——使用CDN的jQuery,等等。

Quote from ScottGu himself: The Microsoft Ajax CDN enables you to significantly improve the performance of ASP.NET Web Forms and ASP.NET MVC applications that use ASP.NET AJAX or jQuery. The service is available for free, does not require any registration, and can be used for both commercial and non-commercial purposes.

引用ScottGu自己的话:Microsoft Ajax CDN使您能够显著提高ASP的性能。NET Web表单和ASP。使用ASP的MVC应用程序。净AJAX或jQuery。该服务是免费的,不需要任何注册,可以用于商业和非商业目的。

We even use the CDN for our webparts in Moss that use jQuery.

我们甚至在使用jQuery的Moss中的web部件上使用CDN。

#7


6  

Also if you use NHibernate you can turn on and setup second level cache for queries and add to queries scope and timeout. And there is kick ass profiler for EF, L2S and NHibernate - http://hibernatingrhinos.com/products/UberProf. It will help to tune your queries.

另外,如果您使用NHibernate,您可以打开并设置查询的二级缓存,并添加到查询范围和超时。还有EF、L2S和NHibernate的kick ass profiler——http://hibernatingrhinos.com/products/UberProf。它将有助于优化您的查询。

#8


4  

In addition to all the great information on optimising your application on the server side I'd say you should take a look at YSlow. It's a superb resource for improving site performance on the client side.

除了关于在服务器端优化应用程序的所有重要信息之外,我还建议您看看YSlow。它是提高客户端站点性能的极好资源。

This applies to all sites, not just ASP.NET MVC.

这适用于所有站点,而不仅仅是ASP。净MVC。

#9


4  

I will also add:

我还会添加:

  1. Use Sprites: Sprites are a great thing to reduce a request. You merge all your images into a single one and use CSS to get to good part of the sprite. Microsoft provides a good library to do it: Sprite and Image Optimization Preview 4.

    使用精灵:精灵是减少请求的好方法。您将所有的图像合并到一个单独的图像中,并使用CSS来获得该精灵的良好部分。微软提供了一个很好的程序库:Sprite和图像优化预览4。

  2. Cache Your server object: If you have some references lists or data which will change rarely, you can cache them into memory instead of querying database every time.

    缓存服务器对象:如果您有一些引用列表或数据很少变化,那么您可以将它们缓存到内存中,而不是每次都查询数据库。

  3. Use ADO.NET instead of Entity Framework: EF4 or EF5 are great to reduce development time, but it will be painful to optimize. It's more simple to optimize a stored procedure than Entity Framework. So you should use store procedures as much as possible. Dapper provides a simple way to query and map SQL with very good performance.

    使用ADO。NET而不是实体框架:EF4或EF5可以减少开发时间,但是优化是痛苦的。优化存储过程比优化实体框架更简单。所以你应该尽可能多地使用存储过程。Dapper提供了一种简单的查询和映射SQL的方法,具有非常好的性能。

  4. Cache Page or partial page: MVC provides some easy filter to cache page according to some parameters, so use it.

    缓存页面或部分页面:MVC提供了一些简单的过滤器来缓存页面,根据一些参数,所以使用它。

  5. Reduce Database calls: You can create a unique database request that returns multiple objects. Check on Dapper website.

    减少数据库调用:可以创建一个返回多个对象的唯一数据库请求。衣冠楚楚的网站上检查。

  6. Always have a clean architecture: Have a clean n-tiers architecture, even on a small project. It will help you to keep your code clean, and it will be easier to optimize it if needed.

    总是有一个干净的架构:有一个干净的n层架构,即使是一个小的项目。它将帮助您保持代码的整洁,并且在需要时更容易优化它。

  7. You can take a look at this template "Neos-SDI MVC Template" which will create a clean architecture for you with lots of performance improvements by default (check MvcTemplate website).

    您可以查看这个模板“Neos-SDI MVC模板”,它将为您创建一个干净的体系结构,并在默认情况下进行大量性能改进(请参阅MvcTemplate网站)。

#10


3  

One super easy thing to do is to think asynchronously when accessing the data you want for the page. Whether reading from a web service, file, data base or something else, use the async model as much as possible. While it won't necessarily help any one page be faster it will help your server perform better overall.

一件非常简单的事情是在访问页面所需的数据时进行异步思考。无论是从web服务、文件、数据库还是其他什么地方读取,都要尽可能多地使用异步模型。虽然它不一定能帮助任何一个页面更快,但它将帮助您的服务器更好地整体执行。

#11


2  

1: Get Timings. Until you know where the slowdown is, the question is too broad to answer. A project I'm working on has this precise problem; There's no logging to even know how long certain things take; we can only guess as to the slow parts of the app until we add timings to the project.

1:计时。除非你知道经济放缓在哪里,否则这个问题的答案太宽泛了。我正在做的一个项目就有这个问题;甚至不需要记录某些事情需要多长时间;我们只能猜测应用程序的缓慢部分,直到我们将时间添加到项目中。

2: If you have sequential operations, Don't be afraid to lightly multithread. ESPECIALLY if blocking operations are involved. PLINQ is your friend here.

2:如果你有顺序操作,不要害怕轻点多线程。特别是如果涉及到阻塞操作。PLINQ是你的朋友。

3: Pregenerate your MVC Views when Publishing... That will help with some of the 'first page hit'

3:发布时预生成MVC视图……这将有助于一些“第一页点击”

4: Some argue for the stored procedure/ADO advantages of speed. Others argue for speed of development of EF and a more clear seprataion of tiers and their purpose. I've seen really slow designs when SQL and the workarounds to use Sprocs/Views for data retrieval and storage. Also, your difficulty to test goes up. Our current codebase that we are converting from ADO to EF is not performing any worse (and in some cases better) than the old Hand-Rolled model.

4:有人认为存储过程/ADO具有速度优势。另一些人则认为,英孚的发展速度,以及更明确的层次和目的。当SQL和工作区使用Sprocs/ view进行数据检索和存储时,我见过设计非常缓慢。而且,你的测试难度也会增加。我们目前正在从ADO到EF的代码库,在某些情况下不会比旧的手动模式更糟糕(在某些情况下更好)。

5: That said, Think about application Warmup. Part of what we do to help eliminate most of our EF performance woes was to add a special warmup method. It doesn't precompile any queries or anything, but it helps with much of the metadata loading/generation. This can be even more important when dealing with Code First models.

话虽如此,想想应用程序热身吧。我们为消除大部分EF性能问题所做的部分工作是添加一个特殊的预热方法。它不预编译任何查询或任何东西,但是它有助于加载/生成元数据。在处理代码优先模型时,这一点可能更为重要。

6: As others have said, Don't use Session state or ViewState if possible. They are not necessarily performance optimizations that developers think about, but once you start writing more complex web applications, you want responsiveness. Session state precludes this. Imagine a long running query. You decide to open a new window and try a less complex one. Well, you may as well have waited with session state on, because the server will wait until the first request is done before moving to the next one for that session.

6:正如其他人所说,如果可能,不要使用会话状态或ViewState。它们不一定是开发人员考虑的性能优化,但是一旦您开始编写更复杂的web应用程序,您就需要响应性。会话状态排除了这一点。想象一个长时间运行的查询。你决定打开一个新窗口,尝试一个不那么复杂的窗口。当然,您也可以等待会话状态打开,因为服务器将等到第一个请求完成后,才会移动到下一个会话。

7: Minimize round trips to the database. Save stuff that you frequently use but will not realistically change to your .Net Cache. Try to batch your inserts/updates where possible.

7:尽量减少往返数据库的次数。保存您经常使用的内容,但不会实际更改。net缓存。尽可能批量插入/更新。

7.1: Avoid Data Access code in your Razor views without a damn good reason. I wouldn't be saying this if I hadn't seen it. They were already accessing their data when putting the model together, why the hell weren't they including it in the model?

7.1:在你的剃刀视图中避免数据访问代码,没有任何理由。如果我没看到的话,我就不会这么说了。当他们把模型放在一起的时候,他们已经在访问他们的数据了,为什么他们不把它包括在模型里呢?

#12


2  

Just wanted to add my 2 cents. The MOST effective way to optimize the URL route generation in an MVC application is... not generate them at all.

我只想加上2分。优化MVC应用程序中URL路由生成的最有效方法是……根本不生成它们。

Most of us more or less know how URLs are generated in our apps anyway, so simply using static Url.Content("~/Blahblah") instead of Url.Action() or Url.RouteUrl() where possible, beats all other methods by almost 20 times and even more.

我们大多数人或多或少都知道url是如何在应用程序中生成的,所以只要使用静态Url.Content(“~/Blahblah”)而不是Url.Action()或Url.RouteUrl(),只要可能的话,就可以比所有其他方法多出20倍甚至更多。

PS. I've ran a benchmark of couple of thousand iterations and posted results on my blog if interested.

我已经运行了几千次迭代的基准测试,如果有兴趣的话,可以在我的博客上发布结果。

#13


1  

In your clamour to optimize the client side, don't forget about the database layer. We had an application that went from 5 seconds to load up to 50 seconds overnight.

在优化客户端时,不要忘记数据库层。我们的应用程序从5秒加载到50秒。

On inspection, we'd made a whole bunch of schema changes. Once we refreshed the statistics, it suddenly became as responsive as before.

在检查时,我们进行了大量的模式更改。一旦我们刷新统计数据,它就会突然像以前一样响应。

#14


1  

  1. Implement Gzip.
  2. Gzip实现。
  3. Use asynchronous rendering for partial views.
  4. 对部分视图使用异步呈现。
  5. Minimize database hits.
  6. 减少数据库连接次数。
  7. Use a compiled query.
  8. 使用编译查询。
  9. Run a profiler and find out unnecessary hits. Optimize all stored procedures which are taking more than 1 second to return a response.
  10. 运行一个剖析器,找出不必要的打击。优化所有需要超过1秒才能返回响应的存储过程。
  11. Use caching.
  12. 使用缓存。
  13. Use bundling minification optimization.
  14. 使用捆绑缩小优化。
  15. Use HTML 5 utilities like session cache and local storage for readonly contents.
  16. 使用HTML 5实用工具,如会话缓存和本地存储只读内容。

#15


0  

Following are things to do

以下是要做的事情

  1. Kernel mode Cache
  2. 内核模式缓存
  3. Pipeline mode
  4. 管道模式
  5. Remove unused modules
  6. 删除未使用的模块
  7. runAllManagedModulesForAllRequests
  8. runAllManagedModulesForAllRequests
  9. Don't write in wwwroot
  10. 不要写在wwwroot
  11. Remove unused view engines and language
  12. 删除未使用的视图引擎和语言

#16


0  

Using Bundling and Minification also helps you improve the performance. It basically reduces the page loading time.

使用捆绑和缩小也可以帮助您提高性能。它基本上减少了页面加载时间。