Ras*_*ber 29 .net c# multithreading httpwebrequest
我正在尝试衡量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();
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作正常,除了似乎有限制工具.如果我使用每40个线程运行该工具的两个实例,那么我获得的吞吐量明显高于具有80个线程的一个实例.
我找到了ServicePointManager.DefaultConnectionLimit属性,我设置为10000(如果我按照Jader Dias的建议通过app.config设置它没有任何区别).
.NET或我的机器上是否有任何其他可能影响性能的设置?(我正在运行Vista,但我在Windows Server 2003上看到了同样的问题).
也许对单个进程可以建立多少连接有一些限制?
Jad*_*ias 37
您必须在app.config或web.config文件中设置maxconnection参数:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<connectionManagement>
<add address="*" maxconnection="80"/>
</connectionManagement>
</system.net>
</configuration>
Run Code Online (Sandbox Code Playgroud)
使用Windows XP时,高达100的值可以很好地工作.
更新:我刚刚发现上面的方法是另一种设置System.Net.ServicePointManager.DefaultConnectionLimit的方法
| 归档时间: |
|
| 查看次数: |
24454 次 |
| 最近记录: |