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.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.Request;
15 import com.ning.http.client.Response;
16 import java.util.ArrayList;
17 import java.util.concurrent.Semaphore;
18 import org.opendaylight.netconf.test.tool.client.stress.ExecutionStrategy;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class AsyncExecutionStrategy implements ExecutionStrategy {
24 private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutionStrategy.class);
26 private final Parameters params;
27 private final ArrayList<Request> payloads;
28 private final AsyncHttpClient asyncHttpClient;
29 private final Semaphore semaphore;
31 AsyncExecutionStrategy(final Parameters params, final AsyncHttpClient asyncHttpClient,
32 final ArrayList<Request> payloads) {
34 this.asyncHttpClient = asyncHttpClient;
35 this.payloads = payloads;
36 this.semaphore = new Semaphore(RestPerfClient.throttle);
40 public void invoke() {
41 LOG.info("Begin sending async requests");
43 for (final Request request : payloads) {
46 } catch (InterruptedException e) {
47 LOG.warn("Semaphore acquire interrupted");
49 asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Response>() {
51 public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
52 super.onStatusReceived(status);
53 if (status.getStatusCode() != 200 && status.getStatusCode() != 204) {
54 LOG.warn("Request failed, status code: {}", status.getStatusCode() + status.getStatusText());
55 LOG.warn("request: {}", request.toString());
57 return STATE.CONTINUE;
61 public Response onCompleted(Response response) {
67 LOG.info("Requests sent, waiting for responses");
70 semaphore.acquire(RestPerfClient.throttle);
71 } catch (InterruptedException e) {
72 LOG.warn("Semaphore acquire interrupted");
75 LOG.info("Responses received, ending...");