简体   繁体   English

ASP.NET页面性能

[英]ASP.NET Page performance

We have a rather complex page that loads the user controls dynamically (some of them are nested). 我们有一个相当复杂的页面,动态加载用户控件(其中一些是嵌套的)。 It is a very slow performing page. 这是一个非常慢的页面。

Could adding the controls dynamically be adding to the bottleneck? 动态添加控件是否会增加瓶颈? Would it help if we add the control in the .NET cache object and not use LoadControl if it already exists in the cache? 如果我们在.NET缓存对象中添加控件而不使用LoadControl(如果它已经存在于缓存中)会有帮助吗?

Any other tips/strategies on making the page faster? 有关使页面更快的任何其他提示/策略?

We once improved an ASP.Net project performance by an order of magnitude. 我们曾将ASP.Net项目的性能提高了一个数量级。 Here is a list of the things we tried: 以下列出了我们尝试的内容:

  • Set SessionState to false or ReadOnly when possible. 尽可能将SessionState设置为false或ReadOnly。 Note that the method for setting session state is different for ASPages, web services, and applications. 请注意,为ASPages,Web服务和应用程序设置会话状态的方法是不同的。
  • Store session state in-process if possible. 如果可能,在进程中存储会话状态。
  • Set EnableViewState to false when possible. 尽可能将EnableViewState设置为false。
  • Use caching when possible. 尽可能使用缓存。
  • Use HTML controls instead of server-side controls if you do not need access to the control from the server. 如果您不需要从服务器访问控件,请使用HTML控件而不是服务器端控件。
  • Avoid round trips (submitting data to the server and reloading the page) when possible. 尽可能避免往返(将数据提交到服务器并重新加载页面)。 Note that you only need to round-trip to read data from, or write data to, the server. 请注意,您只需要往返从服务器读取数据或将数据写入服务器。 Validation and feedback can be done client-side. 验证和反馈可以在客户端完成。 Use the IsPostBack property in Page_Load to avoid regenerating data on postback. 使用Page_Load中的IsPostBack属性可避免在回发时重新生成数据。
  • Use the StringBuilder class for repetitive concatenation. 使用StringBuilder类进行重复串联。
  • Use try/catch blocks only for unexpected situations; 仅在意外情况下使用try / catch块; do not use them as general control constructs. 不要将它们用作一般控制结构。
  • Use early binding as much as possible. 尽可能使用早期绑定。 In other words, avoid reflection. 换句话说,避免反思。
  • Avoid unmanaged code such as COM. 避免使用COM等非托管代码。
  • Disable debug mode for shipping apps. 禁用调试模式以运送应用程序。
  • Use stored procedures for database access rather than SQL strings. 使用存储过程进行数据库访问而不是SQL字符串。
  • Use the DataReader classes for reading data rather than DataSets. 使用DataReader类来读取数据而不是DataSet。
  • Use the most restrictive classes possible, such as SqlDataReader rather than DbDataReader. 尽可能使用限制性最强的类,例如SqlDataReader而不是DbDataReader。

将Trace =“true”添加到@Page指令中,您将能够看到哪些方法花费的时间最长。

I'd be a bit surprised if simply loading the controls dynamically incurred a large performance cost. 如果简单地动态加载控件会导致很大的性能成本,我会感到有些惊讶。 However, if there are a lot of controls, and they are deeply nested, ASP.NET can sometimes be quite slow to render them to html. 但是,如果有很多控件,并且它们是深层嵌套的,那么ASP.NET有时会很慢地将它们呈现为html。 Obviously, do profiling as others have suggested to determine where your bottleneck really lies. 显然,正如其他人建议的那样进行分析,以确定你的瓶颈究竟在哪里。

One thing to check, for a complex page, is the size of the rendered html. 对于复杂页面,要检查的一件事是渲染的html的大小。 With many server controls, the page size can climb up to multiple megabytes surprisingly quickly. 使用许多服务器控件,页面大小可以惊人地快速爬升到数兆字节。 Turning on or tweaking your http compression may be the answer you're looking for. 打开或调整您的http压缩可能是您正在寻找的答案。

The first step in investigating performance issues is identifying the bottleneck: is it network traffic (too many HTTP requests? too much HTML coming down the pipe?) or CPU bound on the server? 调查性能问题的第一步是确定瓶颈:是网络流量(HTTP请求太多?管道中有太多HTML?)还是服务器上绑定的CPU? or too many database calls? 或太多的数据库调用?

In many cases it's the sheer size of the page that causes slowdowns. 在许多情况下,页面的大小会导致速度变慢。 If you're including a lot of controls on your page then there will be a lot of HTML, so if you do a view source on the final rendered product and find 20000 lines of HTML/javascript then it's likely that the slowdown is too much data being sent over the network. 如果你在你的页面上包含了很多控件,那么会有很多HTML,所以如果你在最终渲染的产品上做一个查看源并找到20000行HTML / javascript,那么它的减速可能就太多了数据通过网络发送。

I suggest using a tool like YSlow to help gain a better understanding of the final rendered product. 我建议使用像YSlow这样的工具来帮助更好地理解最终渲染的产品。

Key items to look at: 要看的关键项目:

  • Managing the viewstate size 管理视图状态大小
  • Making sure parts of the page that are hidden are set to .Visible=False (instead of just style="display: none;") 确保隐藏的页面部分设置为.Visible = False(而不仅仅是style =“display:none;”)
  • Centralizing and consolidating javascript 集中和整合javascript
  • Minimizing the number of HTTP requests 最大限度地减少HTTP请求的数量

loading user controls dynamically is definitely not the reason for slow performance. 动态加载用户控件绝对不是性能降低的原因。 one thing which generally deters an ASPX page performance dramatically is viewstate. 通常会阻止ASPX页面性能显着的一件事就是viewstate。 I woudl recommend to take viewstate out of the page [even if you have enabled it on some of the controls]. 我建议将viewstate从页面中取出[即使你已经在某些控件上启用了它]。

Store the complete viewstate as a session variable on the server and only transfer the identifier in the viewstate field. 将完整的视图状态存储为服务器上的会话变量,并仅在视图状态字段中传输标识符。 you can take a look at the complete article here , the article also gives performance measurement metrics. 您可以在这里查看完整的文章,文章还提供了性能测量指标。

I would doubt that its the act of dynamically loading controls that's slowing it down and more the behavior of each control. 我怀疑它是动态加载控件的行为,它会减慢它的速度,更多的是每个控件的行为。 Do they all hit the DB? 他们都打了DB吗? I would see if you could streamline the performance of the controls (caching DB calls, etc) before trying to optimize their loading. 在尝试优化加载之前,我会看看你是否可以简化控件的性能(缓存数据库调用等)。

I would do some profiling on the page to see where the true slowdowns are occurring. 我会在页面上做一些分析,看看真正的减速发生在哪里。 Usually in my experience what I think might be causing the slowdowns in actuality are not the slowest spots. 通常根据我的经验,我认为可能导致实际减速并不是最慢的地方。 I have found it best to profile my apps/pages when things start running slow, that then gives me very good info on what areas I could best speed up to get dramatic improvents. 当事情开始变得缓慢时,我发现最好对我的应用程序/页面进行分析,这样就可以让我获得非常好的信息,告诉我哪些方面可以最快速地获得戏剧性的提升。 You could find it is too many DB calls, or the usercontrol loading, or something else that you hadn't considered. 您可能会发现它有太多的数据库调用,或者用户控件加载,或者您没有考虑过的其他内容。

There are so many reasons why a web page can be a slow performer. 网页可能表现缓慢的原因有很多。 You really need to use some tools like the Performance Wizard to start nailing down what's going on. 你真的需要使用性能向导之类的工具来开始确定正在发生的事情。

Our latest performance problems boiled down to anonymous types and linq. 我们最新的性能问题归结为匿名类型和linq。 It is so easy to kill performance with those two things due to all the JIT compiling going on. 由于所有JIT编译都在进行,所以很容易用这两件事来扼杀性能。

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

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