Java http 客户端和线程

发布于 2024-12-11 13:57:45 字数 2415 浏览 0 评论 0原文

我正在使用 testNG 和 java http 客户端为我们的 API 编写快速负载测试。我的线程似乎阻塞超过预期,所以我想确保 http 客户端正在做我认为的事情。我的基本伪代码是

class 1
    @test method{
     some junk
     myHttpWrapper my = new myHttpWrapper()
    }

class 2   
    public myHttpWrapper{
      HttpClient client = new DefaultHttpClient();
      bunch of packaging up a post params
      HttpResponse response = client.execute(post);
    }

所以我告诉 testNG 在 120 个线程中运行我的 @test 5000 次。我在 java 控制台中看到很多阻塞线程。这不是我的代码,但本质上是我正在做的事情的流程。我希望为我的测试生成的每个线程 testNG 也生成一个单独的 http 客户端。如果是这种情况,为什么所有线程都会阻塞。

下面是 jconsole 中看到的块中的典型线程堆栈:

java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:110)
org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:264)
org.apache.http.impl.conn.LoggingSessionInputBuffer.readLine(LoggingSessionInputBuffer.java:115)
org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:645)
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
ss.qa.test_files.HTTP_Methods.sendPost(HTTP_Methods.java:91)
ss.qa.test_files.apiHelper.ActivateCoupon(apiHelper.java:376)
ss.qa.test_scripts.myPerfTest.ActivateCoupon(BACPerfTest.java:85)

I'm writing a quick load test for our API using testNG and java http client. My threads seem to block more than expected so I want to make sure http client is doing what I think. my basic pseudo code is

class 1
    @test method{
     some junk
     myHttpWrapper my = new myHttpWrapper()
    }

class 2   
    public myHttpWrapper{
      HttpClient client = new DefaultHttpClient();
      bunch of packaging up a post params
      HttpResponse response = client.execute(post);
    }

So I tell testNG to run my @test 5000 times in 120 threads. I see lots of blocking threads in the java console. This is not my code but essentially the flow of what I am doing. I expect that each thread testNG spawns for my test to also spawn a separate http client. If that's the case why all the thread blocking.

Here is a typical thread stack from a block as seen in jconsole:

java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:149)
org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:110)
org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:264)
org.apache.http.impl.conn.LoggingSessionInputBuffer.readLine(LoggingSessionInputBuffer.java:115)
org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:252)
org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:281)
org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:247)
org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:298)
org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:645)
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:464)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
ss.qa.test_files.HTTP_Methods.sendPost(HTTP_Methods.java:91)
ss.qa.test_files.apiHelper.ActivateCoupon(apiHelper.java:376)
ss.qa.test_scripts.myPerfTest.ActivateCoupon(BACPerfTest.java:85)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-12-18 13:57:45

可能是在这段时间内它正在网络上等待。

如果您发布与 TestNG 相关的代码可能会有所帮助。

It could be that it's waiting on the network during those times.

It may help if you post your code related to TestNG.

泅人 2024-12-18 13:57:45

你在什么操作系统上运行这个? Java 在将您与主机操作系统的特定细节和限制隔离方面做得非常好,但它不能做的一件事是超过操作系统内核、网络驱动程序和网络硬件所允许的允许网络连接数量,组合时。

来自 http://www.techiecorner.com/34/how-to-adjust-your-window-xp-tcp-connection-to-boost-your-bt-download-speed/

随着 Microsoft Windows XP Service Pack 2 (SP2) 的发布,它已将并发 TCP 连接限制为每秒 10 个连接 [sic]。

What operating system are you running this on? Java does a pretty good job at insulating you from the specific details and limits of your host OS, but one thing it can't do is surpass the number of allowable network connections that your OS's kernel, network driver, and network hardware will allow, when combined.

From http://www.techiecorner.com/34/how-to-adjust-your-window-xp-tcp-connection-to-boost-your-bt-download-speed/ :

With the release of Microsoft Windows XP Service Pack 2 (SP2), it has limit the concurrent TCP connection to 10 connection per seconds [sic].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文