Improving performance of multithreaded HttpWebRequests in .NET

后端 未结 6 1200
粉色の甜心
粉色の甜心 2020-12-02 14:30

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 n

相关标签:
6条回答
  • 2020-12-02 15:00

    There are two important aspects w.r.t performance:

    1. One aspect is as suggested by all, the number of TCP connections been used by client (generally better if these connections are persisted (keep alive = true)) for detail refer to : http://msdn.microsoft.com/en-us/library/system.net.servicepoint.connectionlimit(v=vs.110).aspx, How and where the TCP connection has been created in httpwebrequest, and how is it related to servicepoint? , Why System.Net.ServicePoint.ConnectionLimit uses '7FFFFFFF' (Int32.MaxValue/2147483647) when a client connects to a service on 'localhost'? , System.Net.ServicePointManager.DefaultConnectionLimit and .MaxServicePointIdleTime )

    2. Second aspect is rather than using multiple new threads/or using worker threads to do work in parallel using synchronous calls (like httpwebrequest.getrequeststream) in the code snippet, embracing async model completely (for ex, begin/endrequeststream, or new task variations). This way CPU will be always busy and let I/O completion port thread simply send the response in a worker (thread pool) thread by invoking callback. (you may refer to: How does .NET make use of IO Threads or IO Completion Ports?, http://blog.marcgravell.com/2009/02/async-without-pain.html, HttpWebRequest and I/O completion ports )

    0 讨论(0)
  • 2020-12-02 15:06

    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>
    

    Values up to 100 work very well with Windows XP.

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

    0 讨论(0)
  • 2020-12-02 15:09

    it could be the connection limit that has been imposed recently.

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

    and

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

    0 讨论(0)
  • 2020-12-02 15:21

    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.

    0 讨论(0)
  • 2020-12-02 15:22

    have you tried increasing the max connections in the network settings?

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

    0 讨论(0)
  • 2020-12-02 15:23

    Keep in mind multithreaded code can always cause contention on any shared resources and even if you're not explicitly sharing anything you might be using classes that are sharing resources under the covers.

    If you are really getting better performance with 2 40 thread exes than 1 80 thread exe then you'll need to start your investigation with shared resources. And if that is the case, the code you cited is far less interesting than the code that creates and manages the threads.

    The other thing I'd throw out there is there are several tools that you can get that will do this type of thing for you generically. See http://support.microsoft.com/kb/231282. Also included in Visual Studio (I'm not sure what skus) is a new generation of web application performance testing tools. And I'm sure if you looked you could find some non-MS stuff too.

    0 讨论(0)
提交回复
热议问题