How do I achieve a better performance of my website
Hy there!
I'm running IIS7 on a Windows 2008 server.
On peek times we have following behaviour:- CPU load is near idle
- Requests get queued (monitored with Resource Monitor)
- Exeuction time gets over 10sec
1-4) Please see previous versions and edits
5) Doing stuff async
As suggested, I've created a simple web ... one page ... with this codebehind:
using System;
using System.Threading;
using System.Web;
using System.Web.UI;
namespace PerformanceTest
{
public partial class AsyncPage : Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
var pageAsyncTask = new PageAsyncTask(this.BeginAsyncOperation, this.EndAsyncOperation, this.TimeoutAsyncOperation, null);
this.RegisterAsyncTask(pageAsyncTask);
// or
//this.AddOnPreRenderCompleteAsync(this.BeginAsyncOperation, this.EndAsyncOperation);
// this might be useful for doing cleanup or sth alike
this.PreRenderComplete += HandlePreRenderComplete;
}
private void HandlePreRenderComplete(object sender, EventArgs e)
{
开发者_C百科 this.Trace.Write("HandlePreRenderComplete");
this.Trace.Write(string.Format("managedThreadId #{0}", Thread.CurrentThread.ManagedThreadId));
}
private delegate void Sleep(int miliseconds);
private IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback asyncCallback, object state)
{
this.Trace.Write("BeginAsyncOperation");
this.Trace.Write(string.Format("managedThreadId #{0}", Thread.CurrentThread.ManagedThreadId));
var sleep = new Sleep(Thread.Sleep);
return sleep.BeginInvoke(1000, asyncCallback, state);
}
private void EndAsyncOperation(IAsyncResult asyncResult)
{
this.Trace.Write("EndAsyncOperation");
this.Trace.Write(string.Format("managedThreadId #{0}", Thread.CurrentThread.ManagedThreadId));
}
private void TimeoutAsyncOperation(IAsyncResult asyncResult)
{
this.Trace.Write("TimeoutAsyncOperation");
this.Trace.Write(string.Format("managedThreadId #{0}", Thread.CurrentThread.ManagedThreadId));
}
}
}
Seems quite good, doesn't it? Actually it changes nothing, as stressing for more than 30 seconds, response time goes up to 8 seconds with CPU usage near 0%.
6) Updated machine.config
<system.net>
<connectionManagement>
<add address="*" maxconnection="12" />
</connectionManagement>
</system.net>
<system.web>
<processModel autoConfig="true"
maxWorkerThreads="100"
maxIoThreads="100"
minWorkerThreads="50"
minIoThreads="50" />
<httpRuntime minFreeThreads="88"
minLocalRequestFreeThreads="76" />
</system.web>
http://geekswithblogs.net/StuartBrierley/archive/2009/09/30/tuning-iis---machine.config-settings.aspx
You use a Page, and probably Session, so every page load lock all the other because of the Session. ! So you may call it Async, or call the PageAsyncTask
, but the page is lock all the other calls because of the Session, and the page calls are get in a line to execute.
Disable the Session just for test, and then if you not use any session data, keep it disable for this page.
Look also: Replacing ASP.Net's session entirely
Web Garder
You can setup a web garden, that is give more pools to the same web application. By do that you need to re-check all your code and include synchronization with mutex, or database lock, because with two or more pools the same data can be access and write by different threads.
http://msdn.microsoft.com/en-us/library/aa479328.aspx
The answer to this is quite easy:
Don't block the thread
The fact of the matter is that IIS and an ASP.NET AppDomain is only able to handle N amount of concurrent requests. You can increase this number, but having a complete lock on thousands of concurrently running threads is a bottleneck nightmare. Without knowing exactly what's causing the ASP.NET page to respond in less than one second it's hard to suggest any performance tweaks, but the problem here is probably not with IIS, but in the code.
If the code is blocking a thread for several seconds without actually doing anything (as the CPU usage is witness to), there is some kind of IO that is so slow it should obviously be made asynchronously. A web-server can server an almost infinite amount of concurrent requests (only limited by available hardware resources) if these requests don't block threads. If they block threads, it can only perform as many requests as there are available threads, which has a hard upper limit.
Do stuff asynchronously
Rewrite your code so it doesn't lock the thread by invoking Begin...
and End...
method instead of their synchronous siblings. The Async CTP can help with wrapping these Begin and End calls in code that looks synchronous, but the performance benefit here looks to be so huge that you should consider rewriting whatever code it is that is locking up the page even without the async
stuff.
We had a similar problem once. It turned out that it was not our code that was causing the problem.
In our case the problem was with the TCP Chimney offload in windows 2008 R2. Windows 2008 trys to be clever by offloading work to the network card, but if the network card gets maxed out things slow down, but all the normal performance values show little load.
http://www.iislogs.com/steveschofield/troubleshooting-iis-7-network-performance-issues-and-tcp-chimney-offload-receive-side-scaling-and-network-direct-memory-access
精彩评论