98d38dd475e46d4cafe5f426e301c94d73fe9b82
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / http / perf / AsyncExecutionStrategy.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.test.tool.client.http.perf;
10
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;
21
22 public class AsyncExecutionStrategy implements ExecutionStrategy {
23
24     private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutionStrategy.class);
25
26     private final Parameters params;
27     private final ArrayList<Request> payloads;
28     private final AsyncHttpClient asyncHttpClient;
29     private final Semaphore semaphore;
30
31     AsyncExecutionStrategy(final Parameters params, final AsyncHttpClient asyncHttpClient,
32                            final ArrayList<Request> payloads) {
33         this.params = params;
34         this.asyncHttpClient = asyncHttpClient;
35         this.payloads = payloads;
36         this.semaphore = new Semaphore(RestPerfClient.throttle);
37     }
38
39     @Override
40     public void invoke() {
41         LOG.info("Begin sending async requests");
42
43         for (final Request request : payloads) {
44             try {
45                 semaphore.acquire();
46             } catch (InterruptedException e) {
47                 LOG.warn("Semaphore acquire interrupted");
48             }
49             asyncHttpClient.executeRequest(request, new AsyncCompletionHandler<Response>() {
50                 @Override
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());
56                     }
57                     return STATE.CONTINUE;
58                 }
59
60                 @Override
61                 public Response onCompleted(Response response) throws Exception {
62                     semaphore.release();
63                     return response;
64                 }
65             });
66         }
67         LOG.info("Requests sent, waiting for responses");
68
69         try {
70             semaphore.acquire(RestPerfClient.throttle);
71         } catch (InterruptedException e) {
72             LOG.warn("Semaphore acquire interrupted");
73         }
74
75         LOG.info("Responses received, ending...");
76     }
77 }