Java http 客户端和线程
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是在这段时间内它正在网络上等待。
如果您发布与 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.
你在什么操作系统上运行这个? Java 在将您与主机操作系统的特定细节和限制隔离方面做得非常好,但它不能做的一件事是超过操作系统内核、网络驱动程序和网络硬件所允许的允许网络连接数量,组合时。
来自 http://www.techiecorner.com/34/how-to-adjust-your-window-xp-tcp-connection-to-boost-your-bt-download-speed/:
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/ :