2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.test.tool.client.http.perf;
11 import com.ning.http.client.AsyncCompletionHandler;
12 import com.ning.http.client.AsyncHttpClient;
13 import com.ning.http.client.HttpResponseStatus;
14 import com.ning.http.client.ListenableFuture;
15 import com.ning.http.client.Request;
16 import com.ning.http.client.Response;
17 import java.util.ArrayList;
18 import java.util.concurrent.Semaphore;
19 import org.opendaylight.controller.netconf.test.tool.client.stress.ExecutionStrategy;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class AsyncExecutionStrategy implements ExecutionStrategy{
25 private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutionStrategy.class);
27 private final Parameters params;
28 private final ArrayList<Request> payloads;
29 private final AsyncHttpClient asyncHttpClient;
30 private final Semaphore semaphore;
32 AsyncExecutionStrategy(final Parameters params, final AsyncHttpClient asyncHttpClient, final ArrayList<Request> payloads) {
34 this.asyncHttpClient = asyncHttpClient;
35 this.payloads = payloads;
36 this.semaphore = new Semaphore(RestPerfClient.throttle);
40 public void invoke() {
41 final ArrayList<ListenableFuture<Response>> futures = new ArrayList<>();
42 LOG.info("Begin sending async requests");
44 for (final Request request : payloads) {
47 } catch (InterruptedException e) {
48 LOG.warn("Semaphore acquire interrupted");
50 futures.add(asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Response>() {
52 public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
53 super.onStatusReceived(status);
54 if (status.getStatusCode() != 200 && status.getStatusCode() != 204) {
55 LOG.warn("Request failed, status code: {}", status.getStatusCode() + status.getStatusText());
56 LOG.warn("request: {}", request.toString());
58 return STATE.CONTINUE;
62 public Response onCompleted(Response response) throws Exception {
68 LOG.info("Requests sent, waiting for responses");
71 semaphore.acquire(RestPerfClient.throttle);
72 } catch (InterruptedException e) {
73 LOG.warn("Semaphore acquire interrupted");
76 LOG.info("Responses received, ending...");