測試系統Window 2003 Server ,IIS 6.0 ,ASP.Net 3.5 sp1
Dual 1.8雙核,2G內存,14G虛擬內存。
為了探尋IIS的最大并發數,先要做幾個假設。
1、假設最大并發數就是當前的連接數。意思是當前能承受最大的連接,那么就表明最大的并發。
2、假設IIS應用程序池處于默認狀態,更改設置將會對最大連接數產生影響。
做完假設,現在做限制,設置站點保持HTTP連接,超時設置成0,就是不會超時。在站點請求的default.aspx頁面設置線程Thread.Sleep(int.MaxValue),接下來開發一個用來保持連接的小程序。
class Program {
private volatile static int errorCount = 0;
private volatile static int rightCount = 0;
static void Main(string[] args) {
ServicePointManager.DefaultConnectionLimit = 10000;
int count = 0;
int all = 0;
while (true) {
all++; count++;
CreateThread();
Thread.Sleep(10);
if (count >= 200) {
Console.WriteLine(string.Format("sucess:{0};error:{1}", all - errorCount, errorCount));
count = 0;
}
if (all > 1800)
break;
}
Console.ReadKey();
}
static void CreateThread() {
Thread thread = new Thread(ActiveRequest);
thread.IsBackground = true;
thread.Start();
}
static void ActiveRequest() {
RequestClient client = new RequestClient("http://192.168.18.2/default.aspx?d=" + Guid.NewGuid());
client.RequestProcess();
if (client.IsError) {
errorCount++;
Console.WriteLine(string.Format("錯誤消息:{0}", client.Messages));
} else {
rightCount++;
//Console.WriteLine(client.Messages);
}
}
}
/**
* author : yurow
* http://birdshover.cnblogs.com
* description:
*
* history : created by yurow 2009-8-16 0:29:05
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace MaxLinked {
/// <summary>
///
/// </summary>
public class RequestClient {
HttpWebRequest request;
WebResponse response;
public RequestClient(string url) {
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = int.MaxValue;
request.KeepAlive = true;
ErrorCode = -1;
}
public void AddHeader(string name, string value) {
request.Headers.Add(name, value);
}
private bool isError = false;
private StringBuilder buffer = new StringBuilder();
public int ErrorCode { get; set; }
public bool IsError {
get { return isError; }
}
public string Messages {
get { return buffer.ToString(); }
}
public void RequestProcess() {
try {
response = request.GetResponse();
} catch (WebException ex) {
ErrorCode = (int)ex.Status;
buffer.Append(ex.Message);
isError = true;
}
if (response != null) {
Stream stream = null;
StreamReader reader = null;
try {
//stream = response.GetResponseStream();
//reader = new StreamReader(stream, Encoding.UTF8);
//buffer.Append(reader.ReadToEnd());
} catch (Exception ex) {
buffer.Append(ex.Message);
isError = true;
} finally {
//if (reader != null)
// reader.Close();
//if (stream != null)
// stream.Close();
}
} else {
isError = true;
buffer.Append("建立連接失敗!");
}
}
public void Close() {
if (response != null)
response.Close();
request.Abort();
}
}
}
程序設置為只能啟動1800個線程,這是由于.Net單進程最大線程數好像是2000個。因此,要測試最大并發數,要需要同時開幾個測試進程。把系統虛擬內存調到最大值,線程過多會急劇占用內存。現在開始測試。
打開web站點的性能計數器,把顯示比例調成1萬。
發現到5000個連接時,IIS服務器崩潰(503錯誤),去洗了個澡,發現IIS服務器無法自己修復錯誤。又測試了幾次,發現最大并發值是8200個,但是一般到5000左右就會崩潰,有時候甚至只有1000個。
按8200個計算,一個用戶開一個瀏覽器瀏覽網頁,可能會占用2~3個連接(參考《IIS連接數實驗——Web開發必讀 》),按兩個計算,那么IIS默認情況下,最大并發數是4000個左右。
打開應用程序池配置,把最大工作進程數調高(默認為1),能有效提高最大連接數。我記得以前看過一篇文章,講的是調到5左右比較合適。