30

我正在尝试测量 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 上看到了同样的问题)。

也许对单个进程可以建立多少个连接有一些限制?

4

6 回答 6

40

您必须在 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 中运行良好。

更新:我刚刚发现上面的方法是设置 System.Net.ServicePointManager.DefaultConnectionLimit 的另一种方法

于 2009-01-12T18:32:52.883 回答
2

您是否尝试过在网络设置中增加最大连接数?

http://msdn.microsoft.com/en-us/library/fb6y0fyc.aspx

于 2008-12-23T16:43:16.067 回答
2

这可能是最近施加的连接限制。

http://www.speedguide.net/read_articles.php?id=1497

http://www.mydigitallife.info/2007/04/09/windows-vista-tcpipsys-connection-limit-patch-for-event-id-4226/

于 2008-12-23T14:09:22.507 回答
1

请记住,多线程代码总是会导致任何共享资源的争用,即使您没有明确共享任何内容,您也可能正在使用在幕后共享资源的类。

如果您确实使用 2 40 线程 exe 获得比 1 80 线程 exe 更好的性能,那么您需要从共享资源开始调查。如果是这样的话,你引用的代码远没有创建和管理线程的代码那么有趣。

我要抛出的另一件事是,您可以获得几种工具,它们通常可以为您完成此类事情。请参阅http://support.microsoft.com/kb/231282。Visual Studio 中也包含(我不确定是什么 skus)是新一代的 Web 应用程序性能测试工具。我敢肯定,如果你看的话,你也能找到一些非 MS 的东西。

于 2008-12-23T17:17:35.733 回答
0

性能有两个重要方面:

  1. 一方面,正如所有人所建议的那样,客户端使用的 TCP 连接数(如果这些连接被持久化(保持活动状态 = true),通常会更好)详情请参阅:http: //msdn.microsoft.com/en-us /library/system.net.servicepoint.connectionlimit(v=vs.110).aspx,httpwebrequestTCP 连接是如何以及在哪里创建的,它与 servicepoint 有什么关系?当客户端连接到“localhost”上的服务时,为什么 System.Net.ServicePoint.ConnectionLimit 使用“7FFFFFFF”(Int32.MaxValue/2147483647)?, System.Net.ServicePointManager.DefaultConnectionLimit 和 .MaxServicePointIdleTime )

  2. 第二个方面不是使用多个新线程/或使用工作线程在代码片段中使用同步调用(如 httpwebrequest.getrequeststream)并行工作,完全采用异步模型(例如,begin/endrequeststream 或新任务变体) . 这样 CPU 会一直很忙,让 I/O 完成端口线程通过调用回调在工作(线程池)线程中简单地发送响应。(您可以参考:.NET 如何使用 IO Threads 或 IO Completion Ports?http : //blog.marcgravell.com/2009/02/async-without-pain.html、HttpWebRequest 和 I/O 完成端口)

于 2014-06-22T04:21:48.457 回答
-1

How are you creating your threads? I assume that since you know you have 80 threads, you're not using the threadpool manager, because with the threadpool manager you can ask for as many threads as you like and you'll only get 25 active threads at a time. If you create the threads manually with an array then you'll actually get as many as you need, however they are still in the same process space, so that might limit them over threads running in separate processes.

You might also look into which apartment style the threads are getting created with, I believe the Thread class ctor uses STA by default. Try MTA and see if they affects performance.

于 2008-12-23T16:51:47.140 回答