测试系统Window 2003 Server ,IIS 6.0 ,ASP.Net 3.5 sp1Dual 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);}}}using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;namespace MaxLinked {/// ////// 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默认情况下,最大并发数是4000个左右。打开应用程序池配置,把最大工作进程数调高(默认为1),能有效提高最大连接数。我记得以前看过一篇文章,讲的是调到5左右比较合适。