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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.metrics;
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;
27 import java.io.IOException;
28 import java.util.Arrays;
30 import java.util.Map.Entry;
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;
51 * The Class concurrentContextMetrics tests concurrent use of context.
53 * @author Liam Fallon (liam.fallon@ericsson.com)
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;
67 // Logger for this class
68 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetrics.class);
70 private String zookeeperAddress = null;
71 private final ConfigrationProvider configrationProvider;
72 private final File zookeeperDirectory;
73 private final int zookeeperPort;
78 * @param args the args
79 * @throws Exception the exception
81 public static void main(final String[] args) throws Exception {
82 if (args.length != NUM_ARGS) {
83 System.err.println("Args: " + Arrays.toString(args));
85 "usage: testLabel jvmCount threadCount threadLoops longArraySize lockType zookeeperAddress zookeeperPort zookeeperDirectory");
89 final ConfigrationProvider configrationProvider =
90 new ConfigrationProviderImpl(args[ARG_LABEL], Integer.valueOf(args[ARG_JVM_COUNT]),
91 Integer.valueOf(args[ARG_THREAD_COUNT]), Integer.valueOf(args[ARG_ITERATIONS]),
92 Integer.valueOf(args[ARG_ARRAY_SIZE]), Integer.valueOf(args[ARG_LOCK_TYPE]));
94 final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(configrationProvider,
95 args[ARG_ZOOKEEPER_ADDRESS], Integer.valueOf(args[ARG_ZOOKEEPER_PORT]), args[ARG_ZOOKEEPER_DIRECTORY]);
97 concurrentContextMetrics.concurrentContextMetricsJVMLocal();
98 concurrentContextMetrics.concurrentContextMetricsCurator();
99 concurrentContextMetrics.concurrentContextMetricsHazelcast();
100 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMHazelcastLock();
101 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMHazelcastlock();
102 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMCuratorLock();
103 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMCuratorLock();
109 * @param configrationProvider
110 * @param zookeeperAddress
111 * @param zookeeperPort
112 * @param zookeeperDirectory
114 public ConcurrentContextMetrics(final ConfigrationProvider configrationProvider, final String zookeeperAddress,
115 final int zookeeperPort, final String zookeeperDirectory) {
116 this.configrationProvider = configrationProvider;
117 this.zookeeperAddress = zookeeperAddress;
118 this.zookeeperPort = zookeeperPort;
119 this.zookeeperDirectory = new File(zookeeperDirectory);
123 * Concurrent context metrics JVM local.
125 * @throws ApexModelException the apex model exception
126 * @throws IOException the IO exception
127 * @throws ApexException the apex exception
129 private void concurrentContextMetricsJVMLocal() throws ApexModelException, IOException, ApexException {
130 if (configrationProvider.getJvmCount() != 1) {
134 LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
136 final ContextParameters contextParameters = new ContextParameters();
137 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
138 contextParameters.getLockManagerParameters().setPluginClass(DEFAULT_LOCK_MANAGER_PLUGIN_CLASS);
139 runConcurrentContextMetrics("JVMLocal");
141 LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
145 * Concurrent context metrics hazelcast.
147 * @throws IOException the IO exception
148 * @throws ApexException the apex exception
150 private void concurrentContextMetricsHazelcast() throws IOException, ApexException {
151 if (configrationProvider.getJvmCount() != 1) {
155 LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
157 final ContextParameters contextParameters = new ContextParameters();
158 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
159 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
160 runConcurrentContextMetrics("Hazelcast");
162 LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
166 * Concurrent context metrics curator.
168 * @throws IOException the IO exception
169 * @throws ApexException the apex exception
171 private void concurrentContextMetricsCurator() throws IOException, ApexException {
172 if (configrationProvider.getJvmCount() != 1) {
176 LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
178 final ContextParameters contextParameters = new ContextParameters();
179 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
181 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
182 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
183 contextParameters.setLockManagerParameters(curatorParameters);
184 curatorParameters.setZookeeperAddress(zookeeperAddress);
186 runConcurrentContextMetrics("Curator");
188 LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
192 * Concurrent context metrics hazelcast multi JVM hazelcast lock.
194 * @throws IOException the IO exception
195 * @throws ApexException the apex exception
197 private void concurrentContextMetricsHazelcastMultiJVMHazelcastLock() throws IOException, ApexException {
198 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
200 final ContextParameters contextParameters = new ContextParameters();
201 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
202 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
203 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
204 runConcurrentContextMetrics("HazelcastMultiJVMHazelcastLock");
206 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
210 * Concurrent context metrics infinispan multi JVM hazelcastlock.
212 * @throws IOException the IO exception
213 * @throws ApexException the apex exception
215 private void concurrentContextMetricsInfinispanMultiJVMHazelcastlock() throws IOException, ApexException {
216 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
218 final ContextParameters contextParameters = new ContextParameters();
219 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
220 distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
221 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
223 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
224 contextParameters.setDistributorParameters(infinispanParameters);
226 runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
228 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
232 * Concurrent context metrics infinispan multi JVM curator lock.
234 * @throws IOException the IO exception
235 * @throws ApexException the apex exception
236 * @throws InterruptedException
238 private void concurrentContextMetricsInfinispanMultiJVMCuratorLock()
239 throws IOException, ApexException, InterruptedException {
241 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
243 final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider =
244 new ZooKeeperServerServiceProvider(zookeeperDirectory, zookeeperAddress, zookeeperPort);
246 zooKeeperServerServiceProvider.startZookeeperServer();
247 final ContextParameters contextParameters = new ContextParameters();
248 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
249 distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
251 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
252 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
253 contextParameters.setLockManagerParameters(curatorParameters);
254 curatorParameters.setZookeeperAddress(zookeeperAddress);
256 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
257 contextParameters.setDistributorParameters(infinispanParameters);
259 runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
261 zooKeeperServerServiceProvider.stopZookeeperServer();
263 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
267 * Concurrent context metrics hazelcast multi JVM curator lock.
269 * @throws IOException the IO exception
270 * @throws ApexException the apex exception
271 * @throws InterruptedException
273 private void concurrentContextMetricsHazelcastMultiJVMCuratorLock()
274 throws IOException, ApexException, InterruptedException {
275 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
277 final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider =
278 new ZooKeeperServerServiceProvider(zookeeperDirectory, zookeeperAddress, zookeeperPort);
281 zooKeeperServerServiceProvider.startZookeeperServer();
282 final ContextParameters contextParameters = new ContextParameters();
283 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
284 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
286 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
287 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
288 contextParameters.setLockManagerParameters(curatorParameters);
289 curatorParameters.setZookeeperAddress(zookeeperAddress);
291 runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
293 zooKeeperServerServiceProvider.stopZookeeperServer();
295 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
299 * Run concurrent context metrics.
301 * @param testName the test name
302 * @throws IOException the IO exception
303 * @throws ApexException the apex exception
305 private void runConcurrentContextMetrics(final String testName) throws IOException, ApexException {
306 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
308 LOGGER.info("Running {} ...", testName);
309 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
312 for (final Entry<String, TestContextLongItem> entry : result.entrySet()) {
313 LOGGER.trace("Album key: {}, value: {}", entry.getKey(), entry.getValue());
314 total += entry.getValue().getLongValue();
316 LOGGER.info("Album total value after execution: {}", total);
318 LOGGER.info("Completed {} ...", testName);