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 java.io.IOException;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
28 import org.onap.policy.apex.context.ContextAlbum;
29 import org.onap.policy.apex.context.ContextException;
30 import org.onap.policy.apex.context.Distributor;
31 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
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.parameters.LockManagerParameters;
35 import org.onap.policy.apex.context.test.concepts.TestContextItem003;
36 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
37 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
38 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
42 import org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanDistributorParameters;
43 import org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManagerParameters;
44 import org.slf4j.ext.XLogger;
45 import org.slf4j.ext.XLoggerFactory;
48 * The Class concurrentContextMetrics tests concurrent use of context.
50 * @author Liam Fallon (liam.fallon@ericsson.com)
52 public class ConcurrentContextMetrics {
53 private static final int NUM_ARGS = 8;
54 private static final int ARG_LABEL = 0;
55 private static final int ARG_JVM_COUNT = 1;
56 private static final int ARG_THREAD_COUNT = 2;
57 private static final int ARG_ITERATIONS = 3;
58 private static final int ARG_ARRAY_SIZE = 4;
59 private static final int ARG_LOCK_TYPE = 5;
60 private static final int ARG_ZOOKEEPER_ADDRESS = 6;
61 private static final int ARG_INTERACTIVE = 7;
63 private static final int TIME_10_MS = 10;
65 // Logger for this class
66 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetrics.class);
69 private String testLabel = null;
70 private int jvmCount = -1;
71 private int threadCount = -1;
72 private int threadLoops = -1;
73 private int longArraySize = -1;
74 private int lockType = -1;
75 private String zookeeperAddress = null;
76 private long total = -1;
77 private boolean interactive = false;
79 // The context distributor and map used by each test
80 private Distributor contextDistributor = null;
81 private ContextAlbum lTypeAlbum = null;
83 private final DateFormat dateFormat = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss,S");
88 * @param args the args
89 * @throws ApexModelException the apex model exception
90 * @throws IOException the IO exception
91 * @throws ApexException the apex exception
93 public static void main(final String[] args) throws ApexModelException, IOException, ApexException {
94 if (args.length != NUM_ARGS) {
96 "usage: ConcurrentContextMetrics testLabel jvmCount threadCount threadLoops longArraySize lockType zookeeperAddress interactive");
100 final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(
102 Integer.valueOf(args[ARG_JVM_COUNT]),
103 Integer.valueOf(args[ARG_THREAD_COUNT]),
104 Integer.valueOf(args[ARG_ITERATIONS]),
105 Integer.valueOf(args[ARG_ARRAY_SIZE]),
106 Integer.valueOf(args[ARG_LOCK_TYPE]),
107 args[ARG_ZOOKEEPER_ADDRESS],
108 Boolean.valueOf(args[ARG_INTERACTIVE]));
111 concurrentContextMetrics.concurrentContextMetricsJVMLocal();
112 concurrentContextMetrics.concurrentContextMetricsCurator();
113 concurrentContextMetrics.concurrentContextMetricsHazelcast();
114 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMHazelcastLock();
115 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMHazelcastlock();
116 concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMCuratorLock();
117 concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMCuratorLock();
123 * @param testLabel the test label
124 * @param jvmCount the jvm count
125 * @param threadCount the thread count
126 * @param threadLoops the thread loops
127 * @param longArraySize the long array size
128 * @param lockType the lock type
129 * @param zookeeperAddress the zookeeper address
130 * @param interactive the interactive
132 // CHECKSTYLE:OFF: checkstyle:parameterNumber
133 public ConcurrentContextMetrics(final String testLabel, final int jvmCount, final int threadCount,
134 final int threadLoops, final int longArraySize, final int lockType, final String zookeeperAddress,
135 final boolean interactive) {
136 // CHECKSTYLE:ON: checkstyle:parameterNumber
138 this.testLabel = testLabel;
139 this.jvmCount = jvmCount;
140 this.threadCount = threadCount;
141 this.threadLoops = threadLoops;
142 this.longArraySize = longArraySize;
143 this.lockType = lockType;
144 this.zookeeperAddress = zookeeperAddress;
145 this.interactive = interactive;
149 * Concurrent context metrics JVM local.
151 * @throws ApexModelException the apex model exception
152 * @throws IOException the IO exception
153 * @throws ApexException the apex exception
155 private void concurrentContextMetricsJVMLocal() throws ApexModelException, IOException, ApexException {
160 LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
162 final ContextParameters contextParameters = new ContextParameters();
163 contextParameters.getDistributorParameters()
164 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
165 contextParameters.getLockManagerParameters()
166 .setPluginClass(LockManagerParameters.DEFAULT_LOCK_MANAGER_PLUGIN_CLASS);
167 runConcurrentContextMetrics("JVMLocal");
169 LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
173 * Concurrent context metrics hazelcast.
175 * @throws ApexModelException the apex model exception
176 * @throws IOException the IO exception
177 * @throws ApexException the apex exception
179 private void concurrentContextMetricsHazelcast() throws ApexModelException, IOException, ApexException {
184 LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
186 final ContextParameters contextParameters = new ContextParameters();
187 contextParameters.getDistributorParameters()
188 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
189 contextParameters.getLockManagerParameters()
190 .setPluginClass("org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager");
191 runConcurrentContextMetrics("Hazelcast");
193 LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
197 * Concurrent context metrics curator.
199 * @throws ApexModelException the apex model exception
200 * @throws IOException the IO exception
201 * @throws ApexException the apex exception
203 private void concurrentContextMetricsCurator() throws ApexModelException, IOException, ApexException {
208 LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
210 final ContextParameters contextParameters = new ContextParameters();
211 contextParameters.getDistributorParameters()
212 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
214 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
215 curatorParameters.setPluginClass("org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager");
216 contextParameters.setLockManagerParameters(curatorParameters);
217 curatorParameters.setZookeeperAddress(zookeeperAddress);
219 runConcurrentContextMetrics("Curator");
221 LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
225 * Concurrent context metrics hazelcast multi JVM hazelcast lock.
227 * @throws ApexModelException the apex model exception
228 * @throws IOException the IO exception
229 * @throws ApexException the apex exception
231 private void concurrentContextMetricsHazelcastMultiJVMHazelcastLock()
232 throws ApexModelException, IOException, ApexException {
233 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
235 final ContextParameters contextParameters = new ContextParameters();
236 contextParameters.getDistributorParameters().setPluginClass(
237 "org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor");
238 contextParameters.getLockManagerParameters()
239 .setPluginClass("org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager");
240 runConcurrentContextMetrics("HazelcastMultiJVMHazelcastLock");
242 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
246 * Concurrent context metrics infinispan multi JVM hazelcastlock.
248 * @throws ApexModelException the apex model exception
249 * @throws IOException the IO exception
250 * @throws ApexException the apex exception
252 private void concurrentContextMetricsInfinispanMultiJVMHazelcastlock()
253 throws ApexModelException, IOException, ApexException {
254 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
256 final ContextParameters contextParameters = new ContextParameters();
257 contextParameters.getDistributorParameters().setPluginClass(
258 "org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor");
259 contextParameters.getLockManagerParameters()
260 .setPluginClass("org.onap.policy.apex.plugins.context.locking.hazelcast.HazelcastLockManager");
262 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
263 contextParameters.setDistributorParameters(infinispanParameters);
265 runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
267 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
271 * Concurrent context metrics infinispan multi JVM curator lock.
273 * @throws ApexModelException the apex model exception
274 * @throws IOException the IO exception
275 * @throws ApexException the apex exception
277 private void concurrentContextMetricsInfinispanMultiJVMCuratorLock()
278 throws ApexModelException, IOException, ApexException {
279 LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
281 final ContextParameters contextParameters = new ContextParameters();
282 contextParameters.getDistributorParameters().setPluginClass(
283 "org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor");
285 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
286 curatorParameters.setPluginClass("org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager");
287 contextParameters.setLockManagerParameters(curatorParameters);
288 curatorParameters.setZookeeperAddress(zookeeperAddress);
290 final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
291 contextParameters.setDistributorParameters(infinispanParameters);
293 runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
295 LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
299 * Concurrent context metrics hazelcast multi JVM curator lock.
301 * @throws ApexModelException the apex model exception
302 * @throws IOException the IO exception
303 * @throws ApexException the apex exception
305 private void concurrentContextMetricsHazelcastMultiJVMCuratorLock()
306 throws ApexModelException, IOException, ApexException {
307 LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
309 final ContextParameters contextParameters = new ContextParameters();
310 contextParameters.getDistributorParameters().setPluginClass(
311 "org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor");
313 final CuratorLockManagerParameters curatorParameters = new CuratorLockManagerParameters();
314 curatorParameters.setPluginClass("org.onap.policy.apex.plugins.context.locking.curator.CuratorLockManager");
315 contextParameters.setLockManagerParameters(curatorParameters);
316 curatorParameters.setZookeeperAddress(zookeeperAddress);
318 runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
320 LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
324 * Run concurrent context metrics.
326 * @param testName the test name
327 * @throws ApexModelException the apex model exception
328 * @throws IOException the IO exception
329 * @throws ApexException the apex exception
331 private void runConcurrentContextMetrics(final String testName)
332 throws ApexModelException, IOException, ApexException {
334 outMetricLine(testName, "Init");
338 } catch (final Exception e) {
343 outMetricLine(testName, "Start");
345 Thread[] threadArray;
347 // Check if we have a single JVM or multiple JVMs
348 int runningThreadCount = -1;
350 threadArray = new Thread[threadCount];
352 // Run everything in this JVM
353 for (int t = 0; t < threadCount; t++) {
355 new Thread(new ConcurrentContextMetricsThread(0, t, threadLoops, longArraySize, lockType));
356 threadArray[t].setName(testLabel + "_" + testName + ":concurrentContextMetricsThread_0_" + t);
357 threadArray[t].start();
360 outMetricLine(testName, "Running");
361 runningThreadCount = threadCount;
363 threadArray = new Thread[jvmCount];
365 final ConcurrentContextMetricsJVMThread[] jvmArray = new ConcurrentContextMetricsJVMThread[jvmCount];
366 // Spawn JVMs to run the tests
367 for (int j = 0; j < jvmCount; j++) {
368 jvmArray[j] = new ConcurrentContextMetricsJVMThread(testLabel + "_" + testName, j, threadCount,
369 threadLoops, longArraySize, lockType);
370 threadArray[j] = new Thread(jvmArray[j]);
371 threadArray[j].setName(testLabel + "_" + testName + ":concurrentContextMetricsJVMThread_" + j);
372 threadArray[j].start();
375 boolean allReadyToGo;
377 ThreadUtilities.sleep(TIME_10_MS);
379 for (int j = 0; j < jvmCount; j++) {
380 if (!jvmArray[j].isReadyToGo()) {
381 allReadyToGo = false;
385 } while (!allReadyToGo);
387 outMetricLine(testName, "Ready");
391 outMetricLine(testName, "Running");
393 for (int j = 0; j < jvmCount; j++) {
394 jvmArray[j].offYouGo();
399 ThreadUtilities.sleep(TIME_10_MS);
401 for (int j = 0; j < jvmCount; j++) {
402 if (!jvmArray[j].isAllFinished()) {
407 } while (!allFinished);
409 outMetricLine(testName, "Completed");
411 verifyContext(testName);
413 for (int j = 0; j < jvmCount; j++) {
414 jvmArray[j].finishItOut();
417 runningThreadCount = jvmCount;
422 ThreadUtilities.sleep(TIME_10_MS);
424 for (int i = 0; i < runningThreadCount; i++) {
425 if (threadArray[i].isAlive()) {
430 } while (!allFinished);
433 outMetricLine(testName, "Completed");
434 verifyContext(testName);
437 clearContext(testName);
443 * @throws ContextException the context exception
445 private void setupContext() throws ContextException {
446 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
447 contextDistributor = new DistributorFactory().getDistributor(distributorKey);
449 final AxArtifactKey[] usedArtifactStackArray = {new AxArtifactKey("testC-top", "0.0.1"),
450 new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1")};
452 final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
453 contextDistributor.registerModel(albumsModel);
455 lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
456 assert (lTypeAlbum != null);
457 lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
459 for (int i = 0; i < longArraySize; i++) {
460 final String longKey = Integer.toString(i);
461 final TestContextItem003 longItem = new TestContextItem003();
462 longItem.setLongValue(0);
463 lTypeAlbum.put(longKey, longItem);
470 * @param testName the test name
471 * @throws ContextException the context exception
473 private void verifyContext(final String testName) throws ContextException {
477 for (int i = 0; i < longArraySize; i++) {
478 total += ((TestContextItem003) lTypeAlbum.get(Integer.toString(i))).getLongValue();
481 outMetricLine(testName, "Totaled");
482 } catch (final Exception e) {
487 if (total == jvmCount * threadCount * threadLoops) {
488 outMetricLine(testName, "VerifiedOK");
490 outMetricLine(testName, "VerifiedFail");
494 outMetricLine(testName, "VerifiedOK");
496 outMetricLine(testName, "VerifiedFail");
504 * @param testName the test name
505 * @throws ContextException the context exception
507 private void clearContext(final String testName) throws ContextException {
508 contextDistributor.clear();
509 contextDistributor = null;
511 outMetricLine(testName, "Cleared");
517 * @param testName the test name
518 * @param testPhase the test phase
520 public void outMetricLine(final String testName, final String testPhase) {
521 System.out.println("ContextMetrics," + dateFormat.format(new Date()) + "," + System.currentTimeMillis() + ","
522 + testLabel + "," + testName + "," + testPhase + "," + jvmCount + "," + threadCount + "," + threadLoops
523 + "," + longArraySize + "," + lockType + "," + total);