gpt4 book ai didi

java - 获取用于负载测试的 NoHttpResponseException

转载 作者:太空狗 更新时间:2023-10-29 22:36:50 26 4
gpt4 key购买 nike

我正在为我的应用程序运行负载测试。我有两台服务器:一台用于我的应用程序,另一台用于接收响应。

在我的虚拟服务器中,我有以下 jsp 代码:

<%@ page import="java.util.Random" %>
<%@ page language="java" %>
<%@ page session="false" %>
<%
String retVal = "some json string";
Thread.sleep(50);
%>

我正在使用 tomcat7 运行应用程序。我的 server.xml 连接池(在两台服务器中)如下所示:

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="1500" minSpareThreads="1000" prestartminSpareThreads="true" /> 
<Connector port="9031" protocol="HTTP/1.1"
connectionTimeout="20000"
maxConnections="4000"
executor="tomcatThreadPool"
redirectPort="8443" />

我从服务器运行的 java 代码是:

 HttpPost post = new HttpPost(bidderUrl);
post.setHeader("Content-Type", "application/json");
// I'm using http client with ThreadSafeClientConnManager
// total conn = 500, max conn per route = 100, timeout=500millis
HttpClient httpClient = httpClientFactory.getHttpClient();
try {
post.setEntity(new StringEntity(jsobBidRequest));
HttpResponse response = httpClient.execute(post);
...
catch (NoHttpResponseException e){
log.error(e);
}

我正在使用 50 个并发线程(没有循环)运行 Jmetter,并得到很多这样的异常:

org.apache.http.NoHttpResponseException The target server failed to respond 

虽然我只运行 5 或 10 个并发线程,但一切正常。

能否请您告诉我我的设置中可能有什么问题?据我了解,对于 50 个并发线程请求,我没有看到任何错误。

最佳答案

我找到了问题的根本原因。

由于某种原因,连接变得无效并且池不知道它。

在这种情况下,将抛出 NoHttpResponseException 并且请求会失败。我认为此类问题应该在 HTTP 客户端池层中解决,并且对我的代码透明,但事实并非如此。

要解决此问题,应重写 HTTP 客户端中的 HttpRequestRetryHandler:

ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
...
DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);
httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
if (executionCount > 3) {
LOGGER.warn("Maximum tries reached for client http pool ");
return false;
}
if (exception instanceof org.apache.http.NoHttpResponseException) {
LOGGER.warn("No response from server on " + executionCount + " call");
return true;
}
return false;
}
});

关于java - 获取用于负载测试的 NoHttpResponseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com