在下面的代码中,DataGather = endDataGather - beginDataGather 需要 1.7 毫秒和服务响应时间 = service_COMPLETED - service_REQUEST_SENT,从 20us 到 200us 不等(因为它们在同一个局域网上被模拟为虚拟,因此如此之低)现在如果我将 tomcat8 线程从 10 增加到200,DataGather 增加到 150ms +,即使我将线程从 200 增加到 1000,它甚至会增加 250+。机器规格 8 核 Xenon,64gb ram。当 apache 基准测试使用 -n 40000 -c 100 args 运行时测量时间,这是由于线程调度/上下文切换还是其他原因?我如何摆脱这种变化?当延迟为 20-100 毫秒的真实服务出现时,它会保留吗?
public List<ServiceResponse> getData(final List<Service> services, final Data data) {
//beginDateGather;
final List<ServiceResponse> serviceResponses = Collections.synchronizedList(new ArrayList<>());
try {
final CountDownLatch latch = new CountDownLatch(services.size());
Map<Future<HttpResponse>, HttpRequestBase> responseRequestMap = new HashMap<Future<HttpResponse>, HttpRequestBase>();
for (final service service : services) {
//creating request for a service
try {
HttpRequestBase request = RequestCreator.getRequestBase(service, data);
//service_REQUEST_SENT
Future<HttpResponse> response = client.execute(request,
new MyFutureCallback(service, data, latch, serviceResponses));
responseRequestMap.put(response, request);
} catch (Exception e) {
latch.countDown();
}
}
try {
boolean isWaitIsOver = latch.await(timeout, TimeUnit.MILLISECONDS);
if (!isWaitIsOver) {
for (Future<HttpResponse> response : responseRequestMap.keySet()) {
if (!response.isDone()) {
response.cancel(true);
}
}
}
} catch (InterruptedException e) {
}
} catch (Exception e) {
}
//endDataGather
return serviceResponses;
}
public class MyFutureCallback implements FutureCallback<HttpResponse> {
private Service service;
private Data data;
private CountDownLatch latch;
private List<serviceResponse> serviceResponses;
public MyFutureCallback( Service service, Data data, CountDownLatch latch, List<ServiceResponse> serviceResponses) {
this.service = service;
this.data = data;
this.latch = latch;
this.serviceResponses = serviceResponses;
}
@Override
public void completed(HttpResponse result) {
try {
ServiceResponse serviceResponse = parseResponse(result, data, service);
serviceResponses.add(serviceResponse);
} catch (Exception e) {
} finally {
//service_COMPLETED
latch.countDown();
}
}
@Override
public void failed(Exception ex) {
latch.countDown();
}
@Override
public void cancelled() {
latch.countDown();
}
}