我正在尝试测量网络服务的吞吐量。
为了做到这一点,我编写了一个小工具,它可以连续发送请求并从多个线程读取响应。
每个线程的内循环内容如下所示:
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(如果我通过app.config设置它没有什么区别)正如贾德·迪亚斯建议的 https://stackoverflow.com/a/436477/5542).
.NET 或我的计算机上是否有任何其他设置会影响性能? (我运行的是 Vista,但我在 Windows Server 2003 上看到同样的问题)。
也许对单个进程可以建立的连接数有一些限制?
您必须在 app.config 或 web.config 文件中设置 maxconnection 参数:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="80"/>
</connectionManagement>
</system.net>
</configuration>
最大 100 的值非常适合 Windows XP。
Update:我刚刚发现上面的方法是设置 System.Net.ServicePointManager.DefaultConnectionLimit 的另一种方法
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)