如何提高ASP ?NET MVC应用程序性能?


当前回答

我还要补充一点:

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. 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. 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. Cache Page or partial page: MVC provides some easy filter to cache page according to some parameters, so use it. Reduce Database calls: You can create a unique database request that returns multiple objects. Check on Dapper website. 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. 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).

其他回答

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

在您嚷嚷着优化客户端时,不要忘记数据库层。我们有一个应用程序在一夜之间从5秒加载到50秒。

在检查中,我们做了一大堆模式更改。一旦我们刷新统计数据,它突然变得像以前一样灵敏。

这不是一个惊天动地的优化,但我想我要把它扔在那里-使用CDN的jQuery,等等。

引用ScottGu自己的话:微软Ajax CDN使你能够显著提高ASP的性能。NET Web Forms和ASP。asp.net MVC应用程序使用ASP。NET AJAX或jQuery。这项服务是免费的,不需要任何注册,可以用于商业和非商业目的。

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

1:获取时间。在你知道放缓在哪里之前,这个问题太宽泛了,无法回答。我正在做的一个项目就有这样的问题;没有日志记录,甚至不知道某些事情需要多长时间;在我们将计时添加到项目中之前,我们只能猜测应用程序的慢部分。

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

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

4:有些人认为存储过程/ADO在速度上有优势。其他人则主张加快英孚教育的发展速度,并对教育层级及其目的进行更明确的划分。我曾见过SQL和使用scpros /Views进行数据检索和存储的变通方法时非常缓慢的设计。同时,你的测试难度也会增加。我们目前正在从ADO转换到EF的代码库的性能并不比旧的手卷模型差(在某些情况下更好)。

5:也就是说,考虑一下应用程序的预热。我们所做的帮助消除大部分EF性能问题的部分工作是添加一个特殊的预热方法。它不预编译任何查询或任何东西,但它有助于大量元数据的加载/生成。在处理Code First模型时,这一点可能更加重要。

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.

7:最小化到数据库的往返。保存你经常使用但实际上不会更改到. net缓存的东西。在可能的情况下尝试批处理插入/更新。

7.1:避免数据访问代码在你的Razor视图没有一个该死的好理由。要不是亲眼所见,我也不会这么说。在构建模型时,他们已经在访问数据了,为什么他们不把数据包括在模型中呢?

如果您正在运行您的ASP。NET MVC应用程序在Microsoft Azure (IaaS或PaaS)上,然后至少在第一次部署之前执行以下操作。

Scan your code with static code analyzer for any type of code debt, duplication, complexity and for security. Always enable the Application Insight, and monitor the performance, browsers, and analytics frequently to find the real-time issues in the application. Implement Azure Redis Cache for static and less frequent change data like Images, assets, common layouts etc. Always rely on APM (Application Performance Management) tools provided by Azure. See application map frequently to investigate the communication performance between internal parts of the application. Monitor Database/VM performance too. Use Load Balancer (Horizontal Scale) if required and within the budget. If your application has the target audience all over the globe, then use Azure Trafic Manager to automatically handle the incoming request and divert it to the most available application instance. Try to automate the performance monitoring by writing the alerts based on low performance.