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.testsuites.performance.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;
76 * Construct a concurrent context object.
78 * @param configrationProvider Configuration for the context metrics
79 * @param zookeeperAddress Zookeeper address
80 * @param zookeeperPort Zookeeper port
81 * @param zookeeperDirectory Zookeeper directory
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);
92 * Concurrent context metrics JVM local.
94 * @throws ApexModelException the apex model exception
95 * @throws IOException the IO exception
96 * @throws ApexException the apex exception
98 private void concurrentContextMetricsJvmLocal() throws ApexException {
99 if (configrationProvider.getJvmCount() != 1) {
103 LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
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");
110 LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
114 * Concurrent context metrics hazelcast.
116 * @throws IOException the IO exception
117 * @throws ApexException the apex exception
119 private void concurrentContextMetricsHazelcast() throws ApexException {
120 if (configrationProvider.getJvmCount() != 1) {
124 LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
126 final ContextParameters contextParameters = new ContextParameters();
127 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
128 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
129 runConcurrentContextMetrics("Hazelcast");
131 LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
135 * Concurrent context metrics curator.
137 * @throws IOException the IO exception
138 * @throws ApexException the apex exception
140 private void concurrentContextMetricsCurator() throws ApexException {
141 if (configrationProvider.getJvmCount() != 1) {
145 LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
147 final ContextParameters contextParameters = new ContextParameters();
148 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
150 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
151 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
152 contextParameters.setLockManagerParameters(curatorParameters);
153 curatorParameters.setZookeeperAddress(zookeeperAddress);
155 runConcurrentContextMetrics("Curator");
157 LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
161 * Concurrent context metrics hazelcast multi JVM hazelcast lock.
163 * @throws IOException the IO exception
164 * @throws ApexException the apex exception
166 private void concurrentContextMetricsHazelcastMultiJvmHazelcastLock() throws ApexException {
167 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
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");
175 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
179 * Concurrent context metrics infinispan multi JVM hazelcastlock.
181 * @throws IOException the IO exception
182 * @throws ApexException the apex exception
184 private void concurrentContextMetricsInfinispanMultiJvmHazelcastlock() throws ApexException {
185 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
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());
192 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
193 contextParameters.setDistributorParameters(infinispanParameters);
195 runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
197 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
201 * Concurrent context metrics infinispan multi JVM curator lock.
203 * @throws IOException the IO exception
204 * @throws ApexException the apex exception
205 * @throws InterruptedException on interrupts
207 private void concurrentContextMetricsInfinispanMultiJvmCuratorLock()
208 throws ApexException {
210 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
212 final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
213 zookeeperDirectory, zookeeperAddress, zookeeperPort);
215 zooKeeperServerServiceProvider.startZookeeperServer();
216 final ContextParameters contextParameters = new ContextParameters();
217 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
218 distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
220 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
221 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
222 contextParameters.setLockManagerParameters(curatorParameters);
223 curatorParameters.setZookeeperAddress(zookeeperAddress);
225 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
226 contextParameters.setDistributorParameters(infinispanParameters);
228 runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
230 zooKeeperServerServiceProvider.stopZookeeperServer();
232 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
236 * Concurrent context metrics hazelcast multi JVM curator lock.
238 * @throws IOException the IO exception
239 * @throws ApexException the apex exception
240 * @throws InterruptedException on interrupts
242 private void concurrentContextMetricsHazelcastMultiJvmCuratorLock()
243 throws ApexException {
244 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
246 final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
247 zookeeperDirectory, zookeeperAddress, zookeeperPort);
250 zooKeeperServerServiceProvider.startZookeeperServer();
251 final ContextParameters contextParameters = new ContextParameters();
252 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
253 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
255 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
256 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
257 contextParameters.setLockManagerParameters(curatorParameters);
258 curatorParameters.setZookeeperAddress(zookeeperAddress);
260 runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
262 zooKeeperServerServiceProvider.stopZookeeperServer();
264 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
268 * Run concurrent context metrics.
270 * @param testName the test name
271 * @throws IOException the IO exception
272 * @throws ApexException the apex exception
274 private void runConcurrentContextMetrics(final String testName) throws ApexException {
275 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
277 LOGGER.info("Running {} ...", testName);
278 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
281 for (final Entry<String, TestContextLongItem> entry : result.entrySet()) {
282 LOGGER.trace("Album key: {}, value: {}", entry.getKey(), entry.getValue());
283 total += entry.getValue().getLongValue();
285 LOGGER.info("Album total value after execution: {}", total);
287 LOGGER.info("Completed {} ...", testName);
293 * @param args the args
294 * @throws Exception the exception
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);
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]));
310 final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(configrationProvider,
311 args[ARG_ZOOKEEPER_ADDRESS], Integer.valueOf(args[ARG_ZOOKEEPER_PORT]),
312 args[ARG_ZOOKEEPER_DIRECTORY]);
314 concurrentContextMetrics.concurrentContextMetricsJvmLocal();
315 concurrentContextMetrics.concurrentContextMetricsCurator();
316 concurrentContextMetrics.concurrentContextMetricsHazelcast();
317 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmHazelcastLock();
318 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmHazelcastlock();
319 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmCuratorLock();
320 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmCuratorLock();