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;
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 String errorMessage = "Args: " + Arrays.toString(args)
84 + "\nusage: testLabel jvmCount threadCount threadLoops longArraySize lockType "
85 + "zookeeperAddress zookeeperPort zookeeperDirectory";
86 LOGGER.info(errorMessage);
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]));
95 final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(configrationProvider,
96 args[ARG_ZOOKEEPER_ADDRESS], Integer.valueOf(args[ARG_ZOOKEEPER_PORT]),
97 args[ARG_ZOOKEEPER_DIRECTORY]);
99 concurrentContextMetrics.concurrentContextMetricsJvmLocal();
100 concurrentContextMetrics.concurrentContextMetricsCurator();
101 concurrentContextMetrics.concurrentContextMetricsHazelcast();
102 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmHazelcastLock();
103 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmHazelcastlock();
104 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJvmCuratorLock();
105 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJvmCuratorLock();
109 * Construct a concurrent context object.
111 * @param configrationProvider Configuration for the context metrics
112 * @param zookeeperAddress Zookeeper address
113 * @param zookeeperPort Zookeeper port
114 * @param zookeeperDirectory Zookeeper directory
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);
125 * Concurrent context metrics JVM local.
127 * @throws ApexModelException the apex model exception
128 * @throws IOException the IO exception
129 * @throws ApexException the apex exception
131 private void concurrentContextMetricsJvmLocal() throws IOException, ApexException {
132 if (configrationProvider.getJvmCount() != 1) {
136 LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
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");
143 LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
147 * Concurrent context metrics hazelcast.
149 * @throws IOException the IO exception
150 * @throws ApexException the apex exception
152 private void concurrentContextMetricsHazelcast() throws IOException, ApexException {
153 if (configrationProvider.getJvmCount() != 1) {
157 LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
159 final ContextParameters contextParameters = new ContextParameters();
160 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
161 contextParameters.getLockManagerParameters().setPluginClass(HazelcastLockManager.class.getCanonicalName());
162 runConcurrentContextMetrics("Hazelcast");
164 LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
168 * Concurrent context metrics curator.
170 * @throws IOException the IO exception
171 * @throws ApexException the apex exception
173 private void concurrentContextMetricsCurator() throws IOException, ApexException {
174 if (configrationProvider.getJvmCount() != 1) {
178 LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
180 final ContextParameters contextParameters = new ContextParameters();
181 contextParameters.getDistributorParameters().setPluginClass(DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
183 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
184 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
185 contextParameters.setLockManagerParameters(curatorParameters);
186 curatorParameters.setZookeeperAddress(zookeeperAddress);
188 runConcurrentContextMetrics("Curator");
190 LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
194 * Concurrent context metrics hazelcast multi JVM hazelcast lock.
196 * @throws IOException the IO exception
197 * @throws ApexException the apex exception
199 private void concurrentContextMetricsHazelcastMultiJvmHazelcastLock() throws IOException, ApexException {
200 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
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");
208 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
212 * Concurrent context metrics infinispan multi JVM hazelcastlock.
214 * @throws IOException the IO exception
215 * @throws ApexException the apex exception
217 private void concurrentContextMetricsInfinispanMultiJvmHazelcastlock() throws IOException, ApexException {
218 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
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());
225 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
226 contextParameters.setDistributorParameters(infinispanParameters);
228 runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
230 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
234 * Concurrent context metrics infinispan multi JVM curator lock.
236 * @throws IOException the IO exception
237 * @throws ApexException the apex exception
238 * @throws InterruptedException on interrupts
240 private void concurrentContextMetricsInfinispanMultiJvmCuratorLock()
241 throws IOException, ApexException, InterruptedException {
243 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
245 final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
246 zookeeperDirectory, zookeeperAddress, zookeeperPort);
248 zooKeeperServerServiceProvider.startZookeeperServer();
249 final ContextParameters contextParameters = new ContextParameters();
250 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
251 distributorParameters.setPluginClass(InfinispanContextDistributor.class.getCanonicalName());
253 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
254 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
255 contextParameters.setLockManagerParameters(curatorParameters);
256 curatorParameters.setZookeeperAddress(zookeeperAddress);
258 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
259 contextParameters.setDistributorParameters(infinispanParameters);
261 runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
263 zooKeeperServerServiceProvider.stopZookeeperServer();
265 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
269 * Concurrent context metrics hazelcast multi JVM curator lock.
271 * @throws IOException the IO exception
272 * @throws ApexException the apex exception
273 * @throws InterruptedException on interrupts
275 private void concurrentContextMetricsHazelcastMultiJvmCuratorLock()
276 throws IOException, ApexException, InterruptedException {
277 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
279 final ZooKeeperServerServiceProvider zooKeeperServerServiceProvider = new ZooKeeperServerServiceProvider(
280 zookeeperDirectory, zookeeperAddress, zookeeperPort);
283 zooKeeperServerServiceProvider.startZookeeperServer();
284 final ContextParameters contextParameters = new ContextParameters();
285 final DistributorParameters distributorParameters = contextParameters.getDistributorParameters();
286 distributorParameters.setPluginClass(HazelcastContextDistributor.class.getCanonicalName());
288 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
289 curatorParameters.setPluginClass(CuratorLockManager.class.getCanonicalName());
290 contextParameters.setLockManagerParameters(curatorParameters);
291 curatorParameters.setZookeeperAddress(zookeeperAddress);
293 runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
295 zooKeeperServerServiceProvider.stopZookeeperServer();
297 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
301 * Run concurrent context metrics.
303 * @param testName the test name
304 * @throws IOException the IO exception
305 * @throws ApexException the apex exception
307 private void runConcurrentContextMetrics(final String testName) throws IOException, ApexException {
308 final ConcurrentContext concurrentContext = new ConcurrentContext(configrationProvider);
310 LOGGER.info("Running {} ...", testName);
311 final Map<String, TestContextLongItem> result = concurrentContext.testConcurrentContext();
314 for (final Entry<String, TestContextLongItem> entry : result.entrySet()) {
315 LOGGER.trace("Album key: {}, value: {}", entry.getKey(), entry.getValue());
316 total += entry.getValue().getLongValue();
318 LOGGER.info("Album total value after execution: {}", total);
320 LOGGER.info("Completed {} ...", testName);