de719c93722a92bbffc1ee8d1e472bc8848bfb4c
[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.plugins.context.metrics;
22
23 import java.io.IOException;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27
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;
46
47 /**
48  * The Class concurrentContextMetrics tests concurrent use of context.
49  *
50  * @author Liam Fallon (liam.fallon@ericsson.com)
51  */
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;
62
63     private static final int TIME_10_MS = 10;
64
65     // Logger for this class
66     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetrics.class);
67
68     // Test parameters
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;
78
79     // The context distributor and map used by each test
80     private Distributor contextDistributor = null;
81     private ContextAlbum lTypeAlbum = null;
82
83     private final DateFormat dateFormat = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss,S");
84
85     /**
86      * The main method.
87      *
88      * @param args the args
89      * @throws ApexModelException the apex model exception
90      * @throws IOException the IO exception
91      * @throws ApexException the apex exception
92      */
93     public static void main(final String[] args) throws ApexModelException, IOException, ApexException {
94         if (args.length != NUM_ARGS) {
95             System.err.println(
96                     "usage: ConcurrentContextMetrics testLabel jvmCount threadCount threadLoops longArraySize lockType zookeeperAddress interactive");
97             return;
98         }
99         // @formatter:off
100         final ConcurrentContextMetrics concurrentContextMetrics = new ConcurrentContextMetrics(
101                 args[ARG_LABEL],
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]));
109         // @formatter:on
110
111         concurrentContextMetrics.concurrentContextMetricsJVMLocal();
112         concurrentContextMetrics.concurrentContextMetricsCurator();
113         concurrentContextMetrics.concurrentContextMetricsHazelcast();
114         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMHazelcastLock();
115         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMHazelcastlock();
116         concurrentContextMetrics.concurrentContextMetricsInfinispanMultiJVMCuratorLock();
117         concurrentContextMetrics.concurrentContextMetricsHazelcastMultiJVMCuratorLock();
118     }
119
120     /**
121      * The Constructor.
122      *
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
131      */
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
137         super();
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;
146     }
147
148     /**
149      * Concurrent context metrics JVM local.
150      *
151      * @throws ApexModelException the apex model exception
152      * @throws IOException the IO exception
153      * @throws ApexException the apex exception
154      */
155     private void concurrentContextMetricsJVMLocal() throws ApexModelException, IOException, ApexException {
156         if (jvmCount != 1) {
157             return;
158         }
159
160         LOGGER.debug("Running concurrentContextMetricsJVMLocalVarSet metrics . . .");
161
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");
168
169         LOGGER.debug("Ran concurrentContextMetricsJVMLocalVarSet metrics");
170     }
171
172     /**
173      * Concurrent context metrics hazelcast.
174      *
175      * @throws ApexModelException the apex model exception
176      * @throws IOException the IO exception
177      * @throws ApexException the apex exception
178      */
179     private void concurrentContextMetricsHazelcast() throws ApexModelException, IOException, ApexException {
180         if (jvmCount != 1) {
181             return;
182         }
183
184         LOGGER.debug("Running concurrentContextMetricsHazelcast metrics . . .");
185
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");
192
193         LOGGER.debug("Ran concurrentContextMetricsHazelcast metrics");
194     }
195
196     /**
197      * Concurrent context metrics curator.
198      *
199      * @throws ApexModelException the apex model exception
200      * @throws IOException the IO exception
201      * @throws ApexException the apex exception
202      */
203     private void concurrentContextMetricsCurator() throws ApexModelException, IOException, ApexException {
204         if (jvmCount != 1) {
205             return;
206         }
207
208         LOGGER.debug("Running concurrentContextMetricsCurator metrics . . .");
209
210         final ContextParameters contextParameters = new ContextParameters();
211         contextParameters.getDistributorParameters()
212                 .setPluginClass(DistributorParameters.DEFAULT_DISTRIBUTOR_PLUGIN_CLASS);
213
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);
218
219         runConcurrentContextMetrics("Curator");
220
221         LOGGER.debug("Ran concurrentContextMetricsCurator metrics");
222     }
223
224     /**
225      * Concurrent context metrics hazelcast multi JVM hazelcast lock.
226      *
227      * @throws ApexModelException the apex model exception
228      * @throws IOException the IO exception
229      * @throws ApexException the apex exception
230      */
231     private void concurrentContextMetricsHazelcastMultiJVMHazelcastLock()
232             throws ApexModelException, IOException, ApexException {
233         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics . . .");
234
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");
241
242         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMHazelcastLock metrics");
243     }
244
245     /**
246      * Concurrent context metrics infinispan multi JVM hazelcastlock.
247      *
248      * @throws ApexModelException the apex model exception
249      * @throws IOException the IO exception
250      * @throws ApexException the apex exception
251      */
252     private void concurrentContextMetricsInfinispanMultiJVMHazelcastlock()
253             throws ApexModelException, IOException, ApexException {
254         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics . . .");
255
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");
261
262         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
263         contextParameters.setDistributorParameters(infinispanParameters);
264
265         runConcurrentContextMetrics("InfinispanMultiJVMHazelcastlock");
266
267         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMHazelcastlock metrics");
268     }
269
270     /**
271      * Concurrent context metrics infinispan multi JVM curator lock.
272      *
273      * @throws ApexModelException the apex model exception
274      * @throws IOException the IO exception
275      * @throws ApexException the apex exception
276      */
277     private void concurrentContextMetricsInfinispanMultiJVMCuratorLock()
278             throws ApexModelException, IOException, ApexException {
279         LOGGER.debug("Running concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics . . .");
280
281         final ContextParameters contextParameters = new ContextParameters();
282         contextParameters.getDistributorParameters().setPluginClass(
283                 "org.onap.policy.apex.plugins.context.distribution.infinispan.InfinispanContextDistributor");
284
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);
289
290         final InfinispanDistributorParameters infinispanParameters = new InfinispanDistributorParameters();
291         contextParameters.setDistributorParameters(infinispanParameters);
292
293         runConcurrentContextMetrics("InfinispanMultiJVMCuratorLock");
294
295         LOGGER.debug("Ran concurrentContextMetricsInfinispanMultiJVMCuratorLock metrics");
296     }
297
298     /**
299      * Concurrent context metrics hazelcast multi JVM curator lock.
300      *
301      * @throws ApexModelException the apex model exception
302      * @throws IOException the IO exception
303      * @throws ApexException the apex exception
304      */
305     private void concurrentContextMetricsHazelcastMultiJVMCuratorLock()
306             throws ApexModelException, IOException, ApexException {
307         LOGGER.debug("Running concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics . . .");
308
309         final ContextParameters contextParameters = new ContextParameters();
310         contextParameters.getDistributorParameters().setPluginClass(
311                 "org.onap.policy.apex.plugins.context.distribution.hazelcast.HazelcastContextDistributor");
312
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);
317
318         runConcurrentContextMetrics("HazelcastMultiJVMCuratorLock");
319
320         LOGGER.debug("Ran concurrentContextMetricsHazelcastMultiJVMCuratorLock metrics");
321     }
322
323     /**
324      * Run concurrent context metrics.
325      *
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
330      */
331     private void runConcurrentContextMetrics(final String testName)
332             throws ApexModelException, IOException, ApexException {
333         total = -1;
334         outMetricLine(testName, "Init");
335
336         try {
337             setupContext();
338         } catch (final Exception e) {
339             e.printStackTrace();
340             throw e;
341         }
342
343         outMetricLine(testName, "Start");
344
345         Thread[] threadArray;
346
347         // Check if we have a single JVM or multiple JVMs
348         int runningThreadCount = -1;
349         if (jvmCount == 1) {
350             threadArray = new Thread[threadCount];
351
352             // Run everything in this JVM
353             for (int t = 0; t < threadCount; t++) {
354                 threadArray[t] =
355                         new Thread(new ConcurrentContextMetricsThread(0, t, threadLoops, longArraySize, lockType));
356                 threadArray[t].setName(testLabel + "_" + testName + ":concurrentContextMetricsThread_0_" + t);
357                 threadArray[t].start();
358             }
359
360             outMetricLine(testName, "Running");
361             runningThreadCount = threadCount;
362         } else {
363             threadArray = new Thread[jvmCount];
364
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();
373             }
374
375             boolean allReadyToGo;
376             do {
377                 ThreadUtilities.sleep(TIME_10_MS);
378                 allReadyToGo = true;
379                 for (int j = 0; j < jvmCount; j++) {
380                     if (!jvmArray[j].isReadyToGo()) {
381                         allReadyToGo = false;
382                         break;
383                     }
384                 }
385             } while (!allReadyToGo);
386
387             outMetricLine(testName, "Ready");
388             if (interactive) {
389                 System.in.read();
390             }
391             outMetricLine(testName, "Running");
392
393             for (int j = 0; j < jvmCount; j++) {
394                 jvmArray[j].offYouGo();
395             }
396
397             boolean allFinished;
398             do {
399                 ThreadUtilities.sleep(TIME_10_MS);
400                 allFinished = true;
401                 for (int j = 0; j < jvmCount; j++) {
402                     if (!jvmArray[j].isAllFinished()) {
403                         allFinished = false;
404                         break;
405                     }
406                 }
407             } while (!allFinished);
408
409             outMetricLine(testName, "Completed");
410
411             verifyContext(testName);
412
413             for (int j = 0; j < jvmCount; j++) {
414                 jvmArray[j].finishItOut();
415             }
416
417             runningThreadCount = jvmCount;
418         }
419
420         boolean allFinished;
421         do {
422             ThreadUtilities.sleep(TIME_10_MS);
423             allFinished = true;
424             for (int i = 0; i < runningThreadCount; i++) {
425                 if (threadArray[i].isAlive()) {
426                     allFinished = false;
427                     break;
428                 }
429             }
430         } while (!allFinished);
431
432         if (jvmCount == 1) {
433             outMetricLine(testName, "Completed");
434             verifyContext(testName);
435         }
436
437         clearContext(testName);
438     }
439
440     /**
441      * Setup context.
442      *
443      * @throws ContextException the context exception
444      */
445     private void setupContext() throws ContextException {
446         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
447         contextDistributor = new DistributorFactory().getDistributor(distributorKey);
448
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")};
451
452         final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
453         contextDistributor.registerModel(albumsModel);
454
455         lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
456         assert (lTypeAlbum != null);
457         lTypeAlbum.setUserArtifactStack(usedArtifactStackArray);
458
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);
464         }
465     }
466
467     /**
468      * Verify context.
469      *
470      * @param testName the test name
471      * @throws ContextException the context exception
472      */
473     private void verifyContext(final String testName) throws ContextException {
474         total = 0;
475
476         try {
477             for (int i = 0; i < longArraySize; i++) {
478                 total += ((TestContextItem003) lTypeAlbum.get(Integer.toString(i))).getLongValue();
479             }
480
481             outMetricLine(testName, "Totaled");
482         } catch (final Exception e) {
483             e.printStackTrace();
484         }
485
486         if (lockType == 2) {
487             if (total == jvmCount * threadCount * threadLoops) {
488                 outMetricLine(testName, "VerifiedOK");
489             } else {
490                 outMetricLine(testName, "VerifiedFail");
491             }
492         } else {
493             if (total == 0) {
494                 outMetricLine(testName, "VerifiedOK");
495             } else {
496                 outMetricLine(testName, "VerifiedFail");
497             }
498         }
499     }
500
501     /**
502      * Clear context.
503      *
504      * @param testName the test name
505      * @throws ContextException the context exception
506      */
507     private void clearContext(final String testName) throws ContextException {
508         contextDistributor.clear();
509         contextDistributor = null;
510
511         outMetricLine(testName, "Cleared");
512     }
513
514     /**
515      * Out metric line.
516      *
517      * @param testName the test name
518      * @param testPhase the test phase
519      */
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);
524     }
525 }