在.NET多线程HttpWebRequests的提高性能多线程、性能、NET、HttpWebRequests

2023-09-02 20:43:36 作者:迷失在人海

我想衡量一个web服务的吞吐量。

I am trying to measure the throughput of a webservice.

为了做到这一点,我已经写了一个小工具,不断发送请求,并从多个线程读取响应。

In order to do that, I have written a small tool that continuously sends requests and reads responses from a number of threads.

每个线程的内部循环的内容是这样的:

The contents of the inner loop of each thread looks like this:

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线程的一个实例。

It seems to work okay, except that something seems to be limiting the tool. If I run two instances of the tool with each 40 threads, I get significantly more throughput than one instance with 80 threads.

我找到了ServicePointManager.DefaultConnectionLimit财产,这是我设置为10000(和它没有什么区别,如果我通过的app.config 设置所建议的Jader迪亚斯)。

I found the ServicePointManager.DefaultConnectionLimit property, which I set to 10000 (and it makes no difference if I set it through app.config as suggested by Jader Dias).

是否有.NET或我的机器能够影响性能上的任何其他设置? (我运行Vista,但我看到2003年的Windows Server相同的问题)。

Are there any other settings in .NET or on my machine that can influence the performance? (I am running Vista, but I see the same problem on Windows Server 2003).

也许多少连接一些限制单个进程可以做什么呢?

Perhaps some restrictions on how many connections a single process can make?

推荐答案

您必须在app.config或web.config文件设置maxconnection参数:

You must set the maxconnection parameter at the app.config or web.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.net>
    <connectionManagement>
      <add address="*" maxconnection="80"/>
    </connectionManagement>
  </system.net>
</configuration>

值高达100很好地工作于Windows XP。

Values up to 100 work very well with Windows XP.

更新:我刚刚发现,上述方法是一种替代方法来设置System.Net.ServicePointManager.DefaultConnectionLimit

Update: I just found out that the method above is an alternative way to set the System.Net.ServicePointManager.DefaultConnectionLimit

 
精彩推荐
图片推荐