b0bfb1ce732c0a9a9599532fe75292d4a458a80e
[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      * Construct a concurrent context object.
77      * 
78      * @param configrationProvider Configuration for the context metrics
79      * @param zookeeperAddress Zookeeper address
80      * @param zookeeperPort Zookeeper port
81      * @param zookeeperDirectory Zookeeper directory
82      */
83     public ConcurrentContextMetrics(final ConfigrationProvider configrationProvider, final String zookeeperAddress,
84                     final int zookeeperPort, final String zookeeperDirectory) {
85         this.configrationProvider = configrationProvider;
86         this.zookeeperAddress = zookeeperAddress;
87         this.zookeeperPort = zookeeperPort;
88         this.zookeeperDirectory = new File(zookeeperDirectory);
89     }
90
91     /**
92      * Concurrent context metrics JVM local.
93      *
94      * @throws ApexModelException the apex model exception
95      * @throws IOException the IO exception
96      * @throws ApexException the apex exception
97      */
98     private void concurrentContextMetricsJvmLocal() throws ApexException {
99         if (configrationProvider.getJvmCount() != 1) {
100             return;
101         }
102
103         LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
104
105         final ContextParameters contextParameters = new ContextParameters();
106         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
107         contextParameters.getLockManagerParameters().setPluginClass(DEFAULT_LOCK_MANAGER_PLUGIN_CLASS);
108         runConcurrentContextMetrics("JVMLocal");
109
110         LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
111     }
112
113     /**
114      * Concurrent context metrics hazelcast.
115      *
116      * @throws IOException the IO exception
117      * @throws ApexException the apex exception
118      */
119     private void concurrentContextMetricsHazelcast() throws ApexException {
120         if (configrationProvider.getJvmCount() != 1) {
121             return;
122         }
123
124         LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
125
126         final ContextParameters contextParameters = new ContextParameters();
127         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
128         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
129         runConcurrentContextMetrics("Hazelcast");
130
131         LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
132     }
133
134     /**
135      * Concurrent context metrics curator.
136      *
137      * @throws IOException the IO exception
138      * @throws ApexException the apex exception
139      */
140     private void concurrentContextMetricsCurator() throws ApexException {
141         if (configrationProvider.getJvmCount() != 1) {
142             return;
143         }
144
145         LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
146
147         final ContextParameters contextParameters = new ContextParameters();
148         contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
149
150         final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
151         curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
152         contextParameters.setLockManagerParameters(curatorParameters);
153         curatorParameters.setZookeeperAddress(zookeeperAddress);
154
155         runConcurrentContextMetrics("Curator");
156
157         LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
158     }
159
160     /**
161      * Concurrent context metrics hazelcast multi JVM hazelcast lock.
162      *
163      * @throws IOException the IO exception
164      * @throws ApexException the apex exception
165      */
166     private void concurrentContextMetricsHazelcastMultiJvmHazelcastLock() throws ApexException {
167         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
168
169         final ContextParameters contextParameters = new ContextParameters();
170         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
171         distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
172         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
173         runConcurrentContextMetrics("HazelcastMultiJVMHazelcastLock");
174
175         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
176     }
177
178     /**
179      * Concurrent context metrics infinispan multi JVM hazelcastlock.
180      *
181      * @throws IOException the IO exception
182      * @throws ApexException the apex exception
183      */
184     private void concurrentContextMetricsInfinispanMultiJvmHazelcastlock() throws ApexException {
185         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
186
187         final ContextParameters contextParameters = new ContextParameters();
188         final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
189         distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
190         contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
191
192         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
193         contextParameters.setDistributorParameters(infinispanParameters);
194
195         runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
196
197         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
198     }
199
200     /**
201      * Concurrent context metrics infinispan multi JVM curator lock.
202      *
203      * @throws IOException the IO exception
204      * @throws ApexException the apex exception
205      * @throws InterruptedException on interrupts
206      */
207     private void concurrentContextMetricsInfinispanMultiJvmCuratorLock()
208                     throws ApexException {
209
210         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
211
212         final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
213                         zookeeperDirectory, zookeeperAddress, zookeeperPort);
214         try {
215             zooKeeperServerServiceProvider.startZookeeperServer();
216             final ContextParameters contextParameters = new ContextParameters();
217             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
218             distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
219
220             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
221             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
222             contextParameters.setLockManagerParameters(curatorParameters);
223             curatorParameters.setZookeeperAddress(zookeeperAddress);
224
225             final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
226             contextParameters.setDistributorParameters(infinispanParameters);
227
228             runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
229         } finally {
230             zooKeeperServerServiceProvider.stopZookeeperServer();
231         }
232         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
233     }
234
235     /**
236      * Concurrent context metrics hazelcast multi JVM curator lock.
237      *
238      * @throws IOException the IO exception
239      * @throws ApexException the apex exception
240      * @throws InterruptedException on interrupts
241      */
242     private void concurrentContextMetricsHazelcastMultiJvmCuratorLock()
243                     throws ApexException {
244         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
245
246         final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
247                         zookeeperDirectory, zookeeperAddress, zookeeperPort);
248
249         try {
250             zooKeeperServerServiceProvider.startZookeeperServer();
251             final ContextParameters contextParameters = new ContextParameters();
252             final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
253             distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
254
255             final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
256             curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
257             contextParameters.setLockManagerParameters(curatorParameters);
258             curatorParameters.setZookeeperAddress(zookeeperAddress);
259
260             runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
261         } finally {
262             zooKeeperServerServiceProvider.stopZookeeperServer();
263         }
264         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
265     }
266
267     /**
268      * Run concurrent context metrics.
269      *
270      * @param testName the test name
271      * @throws IOException the IO exception
272      * @throws ApexException the apex exception
273      */
274     private void runConcurrentContextMetrics(final String testName) throws ApexException {
275         final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
276
277         LOGGER.info("Running {} ...", testName);
278         final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
279
280         long total = 0;
281         for (final Entry<String, TestContextLongItem> entry : result.entrySet()) {
282             LOGGER.trace("Album key: {}, value: {}", entry.getKey(), entry.getValue());
283             total += entry.getValue().getLongValue();
284         }
285         LOGGER.info("Album total value after execution: {}", total);
286
287         LOGGER.info("Completed {} ...", testName);
288     }
289
290     /**
291      * The main method.
292      *
293      * @param args the args
294      * @throws Exception the exception
295      */
296     public static void main(final String[] args) throws Exception {
297         if (args.length != NUM_ARGS) {
298             String errorMessage = "Args: " + Arrays.toString(args)
299                             + "\nusage: testLabel jvmCount threadCount threadLoops longArraySize lockType "
300                             + "zookeeperAddress zookeeperPort zookeeperDirectory";
301             LOGGER.info(errorMessage);
302             return;
303         }
304
305         final ConfigrationProvider configrationProvider = new ConfigrationProviderImpl(args[ARG_LABEL],
306                         Integer.valueOf(args[ARG_JVM_COUNT]), Integer.valueOf(args[ARG_THREAD_COUNT]),
307                         Integer.valueOf(args[ARG_ITERATIONS]), Integer.valueOf(args[ARG_ARRAY_SIZE]),
308                         Integer.valueOf(args[ARG_LOCK_TYPE]));
309
310         final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(configrationProvider,
311                         args[ARG_ZOOKEEPER_ADDRESS], Integer.valueOf(args[ARG_ZOOKEEPER_PORT]),
312                         args[ARG_ZOOKEEPER_DIRECTORY]);
313
314         concurrentContextMetrics.concurrentContextMetricsJvmLocal();
315         concurrentContextMetrics.concurrentContextMetricsCurator();
316         concurrentContextMetrics.concurrentContextMetricsHazelcast();
317         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmHazelcastLock();
318         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmHazelcastlock();
319         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmCuratorLock();
320         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmCuratorLock();
321     }
322 }