我正在尝试测量 Web 服务的吞吐量。
为了做到这一点,我编写了一个小工具,它可以连续发送请求并从多个线程读取响应。
每个线程的内部循环的内容如下所示:
public void PerformRequest()
{
WebRequest webRequest = WebRequest.Create(_uri);
webRequest.ContentType = "application/ocsp-request";
webRequest.Method = "POST";
webRequest.Credentials = _credentials;
webRequest.ContentLength = _request.Length;
((HttpWebRequest)webRequest).KeepAlive = false;
using (Stream st = webRequest.GetRequestStream())
st.Write(_request, 0, _request.Length);
using (HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
using (Stream responseStream = httpWebResponse.GetResponseStream())
using (BufferedStream bufferedStream = new BufferedStream(responseStream))
using (BinaryReader reader = new BinaryReader(bufferedStream))
{
if (httpWebResponse.StatusCode != HttpStatusCode.OK)
throw new WebException("Got response status code: " + httpWebResponse.StatusCode);
byte[] response = reader.ReadBytes((int)httpWebResponse.ContentLength);
httpWebResponse.Close();
}
}
它似乎工作正常,除了似乎限制了该工具的某些东西。如果我以每 40 个线程运行该工具的两个实例,我获得的吞吐量比一个具有 80 个线程的实例要高得多。
我找到了 ServicePointManager.DefaultConnectionLimit 属性,我将其设置为 10000 (如果我按照 Jader Dias 的建议通过 app.config 设置它没有区别)。
.NET 或我的机器上是否有任何其他设置会影响性能?(我正在运行 Vista,但我在 Windows Server 2003 上看到了同样的问题)。
也许对单个进程可以建立多少个连接有一些限制?