简体   繁体   English

企业Web应用程序的.NET性能提示

[英].NET performance tips for enterprise web applications

For enterprise web apps, every little bit counts. 对于企业Web应用程序,每一点点都很重要。

What performance tips can you share to help programmers program more efficiently? 您可以分享哪些性能提示来帮助程序员更有效地编程?

To start it off: 开始吧:

  1. Use StringBuilders over strings since strings are Mutable (they get re-created every time you modify them). 对字符串使用StringBuilders,因为字符串是可变的(每次修改它们时都会重新创建它们)。

  2. Avoid using Datasets as they are very bloated, use SqlReader instead. 避免使用数据集,因为它们非常臃肿,请改用SqlReader。

The points made in the question are micro-optimisations. 问题中提出的观点是微观优化。 I disagree with the very premise that "every little bit helps" - particularly if it comes at the expense of readability. 我不同意“每一点点都有帮助”的前提 - 特别是如果以牺牲可读性为代价。

You see, if you can read and understand your code really easily, that means you can make architectural changes easily. 您知道,如果您能够轻松阅读和理解您的代码,那意味着您可以轻松地进行体系结构更改。 Those are where the really big wins are, not the micro-optimisation. 那些是真正的大赢家,而不是微优化。 The more you try to tune the heck out of every line of code, the harder it will be refactor the whole design. 你试图调整每行代码的次数越多,就越难以重构整个设计。

So my tips are: 所以我的提示是:

  • Write the most readable code you can 尽可能编写最易读的代码
  • Don't optimise the implementation prematurely - but think about architectural performance issues early 不要过早地优化实施 - 但要尽早考虑架构性能问题
  • Don't make changes in the name of performance until you've got hard numbers to let you tell whether or not you're improving things 在你用硬数字来告诉你是否改进之前,不要改变表演的名称
  • Use a profiler to help spot bottlenecks 使用分析器来帮助发现瓶颈

None of this is specific to web-apps, so far. 到目前为止,这些都不是特定于网络应用程序的。 For web apps (and server-side in general): 对于Web应用程序(以及一般的服务器端):

  • Unless you really know you'll never need more than one server, make sure your code can scale horizontally. 除非您确实知道您永远不需要多台服务器,否则请确保您的代码可以横向扩展。 If you can afford to do so, start with two servers (or more) so you can iron out any issues (sessions etc) early. 如果您有能力这样做,请两台服务器(或更多) 开始 ,这样您就可以尽早解决任何问题(会话等)。 This also helps with rolling upgrades etc. 这也有助于滚动升级等。

EDIT: I didn't address the database at all. 编辑:我根本没有解决数据库。 Kyle's answer is good on that front. 凯尔在这方面的答案很好。 Make sure your database can scale too, if possible :) 如果可能,请确保您的数据库也可以扩展:)

the biggest gains you're going to see in (almost) any application is tuning your database. 你几乎可以在任何应用程序中看到的最大收益是调整你的数据库。

Coding ... 编码......

  • Are you selecting a dozen columns when you only need 2? 当你只需要2个时,你选择了十几个列吗?
  • Are you grabbing all the results to perform a SUM? 你是否抓住所有结果来执行SUM?
  • Are you grabbing 1,000 records to display 10? 您是否抓取1,000条记录显示10?
  • Are you firing off a hundred queries every page? 你每个页面都会发出一百个查询吗?

Database ... 数据库......

  • Do you have indexes on your tables? 你的桌子上有索引吗?
  • Are they the right indexes? 它们是正确的索引吗?
  • Have you grabbed some sample queries using SQL Profiler and checked out their execution plans in Query Analyser? 您是否使用SQL事件探查器获取了一些示例查询并在查询分析器中检出了他们的执行计划?
  • Are you seeing TABLE SCAN - BAD! 你看到TABLE SCAN - BAD!
  • Are you seeing INDEX SEEK - GOOD! 你看到INDEX SEEK - 好!

And if all else fails, cache the shit out of it and throw more hardware at the problem! 如果所有其他方法都失败了,请将其中的狗屎缓存出来并为问题投入更多硬件! :) :)

We deal with this every day. 我们每天都在处理这个问题。

We DO cache some datasets that are used A LOT. 我们会缓存一些使用过的数据集。 We have a fairly complex data-layer caching mechanism that works well for us. 我们有一个相当复杂的数据层缓存机制,对我们很有用。

Lazy evaluation for just about everything. 几乎所有事情的懒惰评估。

Page- and partial-caching for user controls 用户控件的页面缓存和部分缓存

We do not use session state at all so we've disabled it altogether. 我们根本不使用会话状态,所以我们完全禁用它。

Configure websites to run as a known- low-prived user. 将网站配置为以已知低端用户身份运行。

Connect to SQL Server as the same low-prived user. 以同一个低级别用户身份连接到SQL Server。 This helps with connection pooling - all connections are essentially the same. 这有助于连接池 - 所有连接基本相同。

NO ad-hoc SQL. 没有ad-hoc SQL。 Stored procs ONLY. 仅存储过程。 Helps with performand AND SQL injection. 帮助执行performand和SQL注入。

string.Concat() instead of string + string + ... or StringBuilder string.Concat()而不是string + string + ...或StringBuilder

Except for manwood nobody mentioned ViewState and it's quite surprising. 除了曼伍德,没有人提到ViewState,这是非常令人惊讶的。 I would vote ViewState Management as the single most important considerations for performance improvment. 我会将ViewState Management视为性能提升的最重要考虑因素。

My list: 我的列表:

  1. Manage View State aggressively 积极管理View State
  2. UpdatePanel is evil ;) Make Juridicious use UpdatePanel是邪恶的;)使用Juridicious
  3. Leverage JavaScript frameworks such as jQuery 利用JavaScript框架,如jQuery
  4. Watch your server roundtrips 观察您的服务器往返
  5. Use Async pages for IO bound operations 使用异步页面进行IO绑定操作
  6. Caching at various level is equally important (Page level, data etc.) 各级缓存同样重要(页面级别,数据等)
  7. Using Ajax to fetch data "on demand" and cache locally as XML (XML data islands etc.) 使用Ajax“按需”获取数据并在本地缓存为XML(XML数据岛等)
  8. Consider async processing for long running operations (you can either use database based job queues and have them processed through a windows service. An ajax request could monitor the row for completion and update UI using balloons) 考虑长时间运行操作的异步处理(您可以使用基于数据库的作业队列并通过Windows服务处理它们.ajax请求可以监视行以完成并使用气球更新UI)

Edit: [added 6-8] 编辑:[添加6-8]

Microsoft has published a book called Improving .NET Application Performance and Scalability . Microsoft发布了一本名为“ 提高.NET应用程序性能和可伸缩性”的书 This is a must read book. 这是一本必读的书。

  • access database as little as possible 尽可能少地访问数据库
  • access web.config as little as possible 尽可能少地访问web.config
  • as manwood says make good use of cache. 正如曼伍德所说,充分利用缓存。 I could also suggest to read this very good article about kernel mode caching 我还可以建议阅读有关内核模式缓存非常好的文章
  • avoid locking if you can 如果可以,请避免锁定
  • some things (like sorting of data) can be done client side nowdays(see Jquery) 现在客户端可以完成一些事情(比如数据排序)(参见Jquery)
  • here is good article to read 是一篇很好的文章

Aside from the database another very imporant thing to watch ... 除了数据库,另一个非常重要的事情是看...

page size and number of requests. 页面大小和请求数。 This should go without saying, but ASP.NET is notoriously bad at filling your pages with a bunch of crap output (driving up the size) and creating a million external script files (number of requests). 这应该是不言而喻的,但是ASP.NET充斥着大量的垃圾输出(增加大小)并创建了一百万个外部脚本文件(请求数量),这是非常糟糕的。

  • Post back as rarely as possible. 尽可能少地回邮。 Use DHTML & JavaScript to manipulate the page when users are making a complex set of critera choices. 当用户制作一组复杂的critera选项时,使用DHTML和JavaScript来操作页面。 Don't post back to make changes in a page in response to every little user setting. 不要回发以在页面中进行更改以响应每个小用户设置。
  • Use ASP.NET controls are sparingly as possible. 尽可能少地使用ASP.NET控件。 Use plain html as much as possible. 尽可能使用普通的HTML。 All ASP.NET controls come at a cost because of view state and control state. 由于视图状态和控件状态,所有ASP.NET控件都需要付出代价。 Plain HTML does not have this overhead. 纯HTML没有这种开销。 I once did a web app for Citibank which consisted of one main query page. 我曾经为花旗银行做了一个网络应用程序,它包含一个主查询页面。 This page was moderately complex. 这个页面中等复杂。 It had only one ASP.NET control on it. 它只有一个ASP.NET控件。 It was a button that posted back to manufacture a custom Excel sheet, loaded with user selected data. 这是一个贴回来制作自定义Excel工作表的按钮,该工作表加载了用户选择的数据。
  • Use the MVC framework rather than ASP.NET. 使用MVC框架而不是ASP.NET。 Viewstate and control state are out of the picture here, if you use Brail or NVelocity. 如果您使用Brail或NVelocity,则Viewstate和控制状态不在此处。
  • Run Ants profiler by Redgate software on your back-end code. 在您的后端代码上运行Redgate软件的Ants剖析器。 Make sure your postback event is as short and sweet as possible. 确保您的回发活动尽可能简短。
  • If a page derives data from a table that is refreshed once every 24 hours or once per week, don't write a comon ASP.NET page to query the data each time a user makes a request. 如果页面从每24小时刷新一次或每周刷新一次的表中导出数据,则不要在每次用户发出请求时编写一个comon ASP.NET页面来查询数据。 If the data is static, make the page static also. 如果数据是静态的,也要使页面保持静态。 You can generate static pages on an NT Scheduler basis using XML literals & Linq to XML classes. 您可以使用XML文本和Linq到XML类在NT Scheduler的基础上生成静态页面。 This is the biggest speed up I can give you. 这是我能给你的最大速度。
  • Make good use of the Cache object for any data that doesn't change very often but gets used alot. 充分利用Cache对象的任何数据不会经常变化,但会被大量使用。 If you need to keep your cache objects in line with your database, have a look at SqlCacheDependency. 如果需要使缓存对象与数据库保持一致,请查看SqlCacheDependency。
  • Disable ViewState wherever it isn't required 在不需要的地方禁用ViewState

examine the log and minimize the amount of HTML being served per request. 检查日志并最小化每个请求所服务的HTML数量。 viewstate and bloated third party controls can ruin your application. viewstate和臃肿的第三方控件可能会破坏您的应用程序。 For example we used for a long time the grid from infragistics. 例如,我们长时间使用了来自infragistics的网格。 very very capable but even in its stripped form it made pages about 60-90k + a lot of java script. 非常有能力,但即使在它的剥离形式,它使页面大约60-90k +很多java脚本。 this severely limited the number of requests we could server, even on an internal gigabit connection. 这严重限制了我们可以服务的请求数量,即使在内部千兆位连接上也是如此。

In my experience, the following make a big difference: 根据我的经验,以下内容产生了很大的不同:

  • Use SQL Server Profiler to identify slow running queries. 使用SQL Server Profiler识别运行缓慢的查询。 In particular use the tuning template to see if you missed out on any indexes. 特别是使用调优模板来查看是否错过了任何索引。
  • Cache sensibly (Caching Application block) 合理缓存(缓存应用程序块)
  • Keep an eye on ViewState 密切关注ViewState
  • Use Fiddler to check page size etc. 使用Fiddler检查页面大小等。

If your web servers are being pounded by huge number of simultaneous requests, and each page requests appear to take longer and longer to service, you may want to consider converting to asynchronous page processing model . 如果您的Web服务器受到大量并发请求的攻击,并且每个页面请求似乎需要更长时间的服务,您可能需要考虑转换为异步页面处理模型

Scalable Apps with Asynchronous Programming in ASP.NET ASP.NET中使用异步编程的可扩展应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM