c4e4de480c9b0543f142ae654cc6e1af66c3aa22
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.performance.context.metrics;
22
23 import static org.onap.policy.apex.context.parameters.DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS;
24 import static org.onap.policy.apex.context.parameters.LockManagerParameters.DEFAULT_LOCK_MANAGER_PLUGIN_CLASS;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.onap.policy.apex.context.parameters.ContextParameters;
33 import org.onap.policy.apex.context.parameters.DistributorParameters;
34 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
35 import org.onap.policy.apex.context.test.locking.ConcurrentContext;
36 import org.onap.policy.apex.context.test.utils.ConfigrationProvider;
37 import org.onap.policy.apex.context.test.utils.ConfigrationProviderImpl;
38 import org.onap.policy.apex.context.test.utils.ZooKeeperServerServiceProvider;
39 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
40 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
41 import org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor;
42 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor;
43 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
44 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager;
45 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
46 import org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager;
47 import org.slf4j.ext.XLogger;
48 import org.slf4j.ext.XLoggerFactory;
49
50 /**
51  * The Class concurrentContextMetrics tests concurrent use of context.
52  *
53  * @author Liam Fallon (liam.fallon@ericsson.com)
54  */
55 public class ConcurrentContextMetrics {
56     private static final int NUM_ARGS = 9;
57     private static final int ARG_LABEL = 0;
58     private static final int ARG_JVM_COUNT = 1;
59     private static final int ARG_THREAD_COUNT = 2;
60     private static final int ARG_ITERATIONS = 3;
61     private static final int ARG_ARRAY_SIZE = 4;
62     private static final int ARG_LOCK_TYPE = 5;
63     private static final int ARG_ZOOKEEPER_ADDRESS = 6;
64     private static final int ARG_ZOOKEEPER_PORT = 7;
65     private static final int ARG_ZOOKEEPER_DIRECTORY = 8;
66
67     // Logger for this class
68     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetrics.class);
69
70     private String zookeeperAddress = null;
71     private final ConfigrationProvider configrationProvider;
72     private final File zookeeperDirectory;
73     private final int zookeeperPort;
74
75     /**
76      * The main method.
77      *
78      * @param args the args
79      * @throws Exception the exception
80      */
81     public static void main(final String[] args) throws Exception {
82         if (args.length != NUM_ARGS) {
83             String errorMessage = "Args: " + Arrays.toString(args)
84                             + "\nusage: testLabel jvmCount threadCount threadLoops longArraySize lockType "
85                             + "zookeeperAddress zookeeperPort zookeeperDirectory";
86             LOGGER.info(errorMessage);
87             return;
88         }
89
90         final ConfigrationProvider configrationProvider = new ConfigrationProviderImpl(args[ARG_LABEL],
91                         Integer.valueOf(args[ARG_JVM_COUNT]), Integer.valueOf(args[ARG_THREAD_COUNT]),
92                         Integer.valueOf(args[ARG_ITERATIONS]), Integer.valueOf(args[ARG_ARRAY_SIZE]),
93                         Integer.valueOf(args[ARG_LOCK_TYPE]));
94
95         final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(configrationProvider,
96                         args[ARG_ZOOKEEPER_ADDRESS], Integer.valueOf(args[ARG_ZOOKEEPER_PORT]),
97                         args[ARG_ZOOKEEPER_DIRECTORY]);
98
99         concurrentContextMetrics.concurrentContextMetricsJvmLocal();
100         concurrentContextMetrics.concurrentContextMetricsCurator();
101         concurrentContextMetrics.concurrentContextMetricsHazelcast();
102         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmHazelcastLock();
103         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmHazelcastlock();
104         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmCuratorLock();
105         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmCuratorLock();
106     }
107
108     /**
109      * Construct a concurrent context object.
110      * 
111      * @param configrationProvider Configuration for the context metrics
112      * @param zookeeperAddress Zookeeper address
113      * @param zookeeperPort Zookeeper port
114      * @param zookeeperDirectory Zookeeper directory
115      */
116     public ConcurrentContextMetrics(final ConfigrationProvider configrationProvider, final String zookeeperAddress,
117                     final int zookeeperPort, final String zookeeperDirectory) {
118         this.configrationProvider = configrationProvider;
119         this.zookeeperAddress = zookeeperAddress;
120         this.zookeeperPort = zookeeperPort;
121         this.zookeeperDirectory = new File(zookeeperDirectory);
122     }
123
124     /**
125      * Concurrent context metrics JVM local.
126      *
127      * @throws ApexModelException the apex model exception
128      * @throws IOException the IO exception
129      * @throws ApexException the apex exception
130      */
131     private void concurrentContextMetricsJvmLocal() throws IOException, ApexException {
132         if (configrationProvider.getJvmCount() != 1) {
133             return;
134         }
135
136         LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
137
138         final ContextParameters contextParameters = new ContextParameters();
139         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
140         contextParameters.getLockManagerParameters().setPluginClass(DEFAULT_LOCK_MANAGER_PLUGIN_CLASS);
141         runConcurrentContextMetrics("JVMLocal");
142
143         LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
144     }
145
146     /**
147      * Concurrent context metrics hazelcast.
148      *
149      * @throws IOException the IO exception
150      * @throws ApexException the apex exception
151      */
152     private void concurrentContextMetricsHazelcast() throws IOException, ApexException {
153         if (configrationProvider.getJvmCount() != 1) {
154             return;
155         }
156
157         LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
158
159         final ContextParameters contextParameters = new ContextParameters();
160         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
161         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
162         runConcurrentContextMetrics("Hazelcast");
163
164         LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
165     }
166
167     /**
168      * Concurrent context metrics curator.
169      *
170      * @throws IOException the IO exception
171      * @throws ApexException the apex exception
172      */
173     private void concurrentContextMetricsCurator() throws IOException, ApexException {
174         if (configrationProvider.getJvmCount() != 1) {
175             return;
176         }
177
178         LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
179
180         final ContextParameters contextParameters = new ContextParameters();
181         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
182
183         final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
184         curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
185         contextParameters.setLockManagerParameters(curatorParameters);
186         curatorParameters.setZookeeperAddress(zookeeperAddress);
187
188         runConcurrentContextMetrics("Curator");
189
190         LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
191     }
192
193     /**
194      * Concurrent context metrics hazelcast multi JVM hazelcast lock.
195      *
196      * @throws IOException the IO exception
197      * @throws ApexException the apex exception
198      */
199     private void concurrentContextMetricsHazelcastMultiJvmHazelcastLock() throws IOException, ApexException {
200         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
201
202         final ContextParameters contextParameters = new ContextParameters();
203         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
204         distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
205         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
206         runConcurrentContextMetrics("HazelcastMultiJVMHazelcastLock");
207
208         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
209     }
210
211     /**
212      * Concurrent context metrics infinispan multi JVM hazelcastlock.
213      *
214      * @throws IOException the IO exception
215      * @throws ApexException the apex exception
216      */
217     private void concurrentContextMetricsInfinispanMultiJvmHazelcastlock() throws IOException, ApexException {
218         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
219
220         final ContextParameters contextParameters = new ContextParameters();
221         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
222         distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
223         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
224
225         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
226         contextParameters.setDistributorParameters(infinispanParameters);
227
228         runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
229
230         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
231     }
232
233     /**
234      * Concurrent context metrics infinispan multi JVM curator lock.
235      *
236      * @throws IOException the IO exception
237      * @throws ApexException the apex exception
238      * @throws InterruptedException on interrupts
239      */
240     private void concurrentContextMetricsInfinispanMultiJvmCuratorLock()
241                     throws IOException, ApexException, InterruptedException {
242
243         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
244
245         final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
246                         zookeeperDirectory, zookeeperAddress, zookeeperPort);
247         try {
248             zooKeeperServerServiceProvider.startZookeeperServer();
249             final ContextParameters contextParameters = new ContextParameters();
250             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
251             distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
252
253             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
254             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
255             contextParameters.setLockManagerParameters(curatorParameters);
256             curatorParameters.setZookeeperAddress(zookeeperAddress);
257
258             final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
259             contextParameters.setDistributorParameters(infinispanParameters);
260
261             runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
262         } finally {
263             zooKeeperServerServiceProvider.stopZookeeperServer();
264         }
265         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
266     }
267
268     /**
269      * Concurrent context metrics hazelcast multi JVM curator lock.
270      *
271      * @throws IOException the IO exception
272      * @throws ApexException the apex exception
273      * @throws InterruptedException on interrupts
274      */
275     private void concurrentContextMetricsHazelcastMultiJvmCuratorLock()
276                     throws IOException, ApexException, InterruptedException {
277         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
278
279         final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
280                         zookeeperDirectory, zookeeperAddress, zookeeperPort);
281
282         try {
283             zooKeeperServerServiceProvider.startZookeeperServer();
284             final ContextParameters contextParameters = new ContextParameters();
285             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
286             distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
287
288             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
289             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
290             contextParameters.setLockManagerParameters(curatorParameters);
291             curatorParameters.setZookeeperAddress(zookeeperAddress);
292
293             runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
294         } finally {
295             zooKeeperServerServiceProvider.stopZookeeperServer();
296         }
297         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
298     }
299
300     /**
301      * Run concurrent context metrics.
302      *
303      * @param testName the test name
304      * @throws IOException the IO exception
305      * @throws ApexException the apex exception
306      */
307     private void runConcurrentContextMetrics(final String testName) throws IOException, ApexException {
308         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
309
310         LOGGER.info("Running {} ...", testName);
311         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
312
313         long total = 0;
314         for (final Entry<String, TestContextLongItem> entry : result.entrySet()) {
315             LOGGER.trace("Album key: {}, value: {}", entry.getKey(), entry.getValue());
316             total += entry.getValue().getLongValue();
317         }
318         LOGGER.info("Album total value after execution: {}", total);
319
320         LOGGER.info("Completed {} ...", testName);
321     }
322
323 }