2ec721f29ee7401b9e305b2c0c6ed762e455f36f
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / client / http / perf / PerfClientCallable.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.AsyncHttpClient;
12 import com.ning.http.client.AsyncHttpClientConfig;
13 import java.util.concurrent.Callable;
14 import org.opendaylight.netconf.test.tool.client.http.perf.RestPerfClient.RequestData;
15 import org.opendaylight.netconf.test.tool.client.stress.ExecutionStrategy;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class PerfClientCallable implements Callable<Void> {
20     private static final Logger LOG = LoggerFactory.getLogger(PerfClientCallable.class);
21
22     private final Parameters params;
23     private final AsyncHttpClient asyncHttpClient;
24     private ExecutionStrategy executionStrategy;
25     private RequestData payloads;
26
27     public PerfClientCallable(Parameters params, RequestData payloads) {
28         this.params = params;
29         this.payloads = payloads;
30         this.asyncHttpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder()
31                 .setConnectTimeout(Integer.MAX_VALUE)
32                 .setRequestTimeout(Integer.MAX_VALUE)
33                 .setAllowPoolingConnections(true)
34                 .build());
35         executionStrategy = getExecutionStrategy();
36     }
37
38     private ExecutionStrategy getExecutionStrategy() {
39         return params.async
40                 ? new AsyncExecutionStrategy(params, asyncHttpClient, payloads)
41                 : new SyncExecutionStrategy(params, asyncHttpClient, payloads);
42     }
43
44     @Override
45     public Void call() {
46         executionStrategy.invoke();
47         asyncHttpClient.closeAsynchronously();
48         return null;
49     }
50 }