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.AsyncHttpClient;
12 import com.ning.http.client.AsyncHttpClientConfig;
13 import com.ning.http.client.Realm;
14 import com.ning.http.client.Request;
15 import java.util.ArrayList;
16 import java.util.concurrent.Callable;
17 import org.opendaylight.netconf.test.tool.client.http.perf.RestPerfClient.DestToPayload;
18 import org.opendaylight.netconf.test.tool.client.stress.ExecutionStrategy;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class PerfClientCallable implements Callable<Void> {
23 private static final Logger LOG = LoggerFactory.getLogger(PerfClientCallable.class);
25 private final Parameters params;
26 private final ArrayList<Request> payloads;
27 private final AsyncHttpClient asyncHttpClient;
28 private ExecutionStrategy executionStrategy;
30 public PerfClientCallable(Parameters params, ArrayList<DestToPayload> payloads) {
32 this.asyncHttpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder()
33 .setConnectTimeout(Integer.MAX_VALUE)
34 .setRequestTimeout(Integer.MAX_VALUE)
35 .setAllowPoolingConnections(true)
37 this.payloads = new ArrayList<>();
38 for (DestToPayload payload : payloads) {
39 AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(payload.getDestination())
40 .addHeader("content-type", "application/json")
41 .addHeader("Accept", "application/xml")
42 .setBody(payload.getPayload())
43 .setRequestTimeout(Integer.MAX_VALUE);
45 if (params.auth != null) {
46 requestBuilder.setRealm(new Realm.RealmBuilder()
47 .setScheme(Realm.AuthScheme.BASIC)
48 .setPrincipal(params.auth.get(0))
49 .setPassword(params.auth.get(1))
50 .setUsePreemptiveAuth(true)
54 this.payloads.add(requestBuilder.build());
56 executionStrategy = getExecutionStrategy();
59 private ExecutionStrategy getExecutionStrategy() {
61 ? new AsyncExecutionStrategy(params, asyncHttpClient, payloads)
62 : new SyncExecutionStrategy(params, asyncHttpClient, payloads);
66 public Void call() throws Exception {
67 executionStrategy.invoke();
68 asyncHttpClient.closeAsynchronously();