測試系統(tǒng)Window 2003 Server ,IIS 6.0 ,ASP.Net 3.5 sp1
Dual 1.8雙核,2G內(nèi)存,14G虛擬內(nèi)存。
為了探尋IIS的最大并發(fā)數(shù),先要做幾個(gè)假設(shè)。
1、假設(shè)最大并發(fā)數(shù)就是當(dāng)前的連接數(shù)。意思是當(dāng)前能承受最大的連接,那么就表明最大的并發(fā)。
2、假設(shè)IIS應(yīng)用程序池處于默認(rèn)狀態(tài),更改設(shè)置將會(huì)對(duì)最大連接數(shù)產(chǎn)生影響。
做完假設(shè),現(xiàn)在做限制,設(shè)置站點(diǎn)保持HTTP連接,超時(shí)設(shè)置成0,就是不會(huì)超時(shí)。在站點(diǎn)請(qǐng)求的default.aspx頁面設(shè)置線程Thread.Sleep(int.MaxValue),接下來開發(fā)一個(gè)用來保持連接的小程序。
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("錯(cuò)誤消息:{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();
}
}
}
程序設(shè)置為只能啟動(dòng)1800個(gè)線程,這是由于.Net單進(jìn)程最大線程數(shù)好像是2000個(gè)。因此,要測試最大并發(fā)數(shù),要需要同時(shí)開幾個(gè)測試進(jìn)程。把系統(tǒng)虛擬內(nèi)存調(diào)到最大值,線程過多會(huì)急劇占用內(nèi)存。現(xiàn)在開始測試。
打開web站點(diǎn)的性能計(jì)數(shù)器,把顯示比例調(diào)成1萬。
發(fā)現(xiàn)到5000個(gè)連接時(shí),IIS服務(wù)器崩潰(503錯(cuò)誤),去洗了個(gè)澡,發(fā)現(xiàn)IIS服務(wù)器無法自己修復(fù)錯(cuò)誤。又測試了幾次,發(fā)現(xiàn)最大并發(fā)值是8200個(gè),但是一般到5000左右就會(huì)崩潰,有時(shí)候甚至只有1000個(gè)。
按8200個(gè)計(jì)算,一個(gè)用戶開一個(gè)瀏覽器瀏覽網(wǎng)頁,可能會(huì)占用2~3個(gè)連接(參考《IIS連接數(shù)實(shí)驗(yàn)——Web開發(fā)必讀 》),按兩個(gè)計(jì)算,那么IIS默認(rèn)情況下,最大并發(fā)數(shù)是4000個(gè)左右。
打開應(yīng)用程序池配置,把最大工作進(jìn)程數(shù)調(diào)高(默認(rèn)為1),能有效提高最大連接數(shù)。我記得以前看過一篇文章,講的是調(diào)到5左右比較合適。