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.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.util.Arrays;
28 import org.onap.policy.apex.context.ContextAlbum;
29 import org.onap.policy.apex.context.Distributor;
30 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
31 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
32 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.service.AbstractParameters;
36 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
38 import org.onap.policy.apex.model.utilities.Assertions;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
42 import com.google.gson.Gson;
43 import com.google.gson.JsonSyntaxException;
46 * The Class ConcurrentContextMetricsJVM rins in its own JVM to test concurrent context updates and
47 * lockings across JVMs.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public final class ConcurrentContextMetricsJVM {
52 // Logger for this class
53 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetricsJVM.class);
55 private static final int NUM_ARGS = 6;
56 private static final int ARG_JVM_NO = 1;
57 private static final int ARG_THREAD_COUNT = 2;
58 private static final int ARG_ITERATIONS = 3;
59 private static final int ARG_ARRAY_SIZE = 4;
60 private static final int ARG_LOCK_TYPE = 5;
62 private static final int WAIT_10_MS = 10;
65 * The Constructor for this class.
67 * @param testType the test type
68 * @param jvmNo the jvm no
69 * @param threadCount the thread count
70 * @param threadLoops the thread loops
71 * @param longArraySize the long array size
72 * @param lockType the lock type
73 * @throws ApexException the apex exception
74 * @throws IOException the IO exception
76 private ConcurrentContextMetricsJVM(final String testType, final int jvmNo, final int threadCount,
77 final int threadLoops, final int longArraySize, final int lockType) throws ApexException, IOException {
78 LOGGER.debug("starting JVMs and threads . . .");
80 final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
82 // Set up the distributor for this JVM
83 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
84 final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
86 final AxArtifactKey[] usedArtifactStackArray = {new AxArtifactKey("testC-top_" + jvmNo, "0.0.1"),
87 new AxArtifactKey("testC-next_" + jvmNo, "0.0.1"), new AxArtifactKey("testC-bot_" + jvmNo, "0.0.1")};
89 final AxContextModel testAxContextModel = TestContextAlbumFactory.createLongContextModel();
90 contextDistributor.registerModel(testAxContextModel);
91 final ContextAlbum testContextAlbum =
92 contextDistributor.createContextAlbum(new AxArtifactKey("LongSameTypeContextAlbum", "0.0.1"));
93 Assertions.argumentNotNull(testContextAlbum, "testContextAlbum may not be null");
94 testContextAlbum.setUserArtifactStack(usedArtifactStackArray);
96 final Thread[] threadArray = new Thread[threadCount];
98 for (int t = 0; t < threadCount; t++) {
100 new Thread(new ConcurrentContextMetricsThread(jvmNo, t, threadLoops, longArraySize, lockType));
101 threadArray[t].setName(testType + ":ConcurrentContextMetricsThread_" + jvmNo + "_" + t);
102 threadArray[t].start();
103 LOGGER.debug("started thread " + threadArray[t].getName());
106 System.out.println("ReadyToGo");
108 final String goLine = bufferedReader.readLine();
109 if (!goLine.trim().equals("OffYouGo")) {
110 throw new IOException("Expected OffYouGo");
118 for (int t = 0; t < threadCount; t++) {
119 if (threadArray[t].isAlive()) {
121 ThreadUtilities.sleep(WAIT_10_MS);
125 } while (!allFinished);
127 System.out.println("AllFinished");
129 final String goLine = bufferedReader.readLine();
130 if (!goLine.trim().equals("FinishItOut")) {
131 throw new IOException("Expected FinishItOut");
136 LOGGER.debug("threads finished");
137 contextDistributor.clear();
143 * @param args the args
144 * @throws JsonSyntaxException the json syntax exception
145 * @throws ClassNotFoundException the class not found exception
147 @SuppressWarnings("unchecked")
148 public static void main(final String[] args) throws JsonSyntaxException, ClassNotFoundException {
149 if (args.length < NUM_ARGS || (args.length % 2 != 0)) {
150 LOGGER.error("invalid arguments: " + Arrays.toString(args));
152 "usage: ConcurrentContextMetricsJVM testLabel jvmNo threadCount threadLoops longArraySize lockType [parameterKey parameterJson].... ");
157 int threadCount = -1;
158 int threadLoops = -1;
159 int longArraySize = -1;
163 jvmNo = Integer.parseInt(args[ARG_JVM_NO]);
164 } catch (final Exception e) {
165 LOGGER.error("invalid argument jvmNo", e);
170 threadCount = Integer.parseInt(args[ARG_THREAD_COUNT]);
171 } catch (final Exception e) {
172 LOGGER.error("invalid argument threadCount", e);
177 threadLoops = Integer.parseInt(args[ARG_ITERATIONS]);
178 } catch (final Exception e) {
179 LOGGER.error("invalid argument threadLoops", e);
184 longArraySize = Integer.parseInt(args[ARG_ARRAY_SIZE]);
185 } catch (final Exception e) {
186 LOGGER.error("invalid argument longArraySize", e);
191 lockType = Integer.parseInt(args[ARG_LOCK_TYPE]);
192 } catch (final Exception e) {
193 LOGGER.error("invalid argument lockType", e);
197 for (int p = NUM_ARGS; p < args.length - 1; p += 2) {
198 @SuppressWarnings("rawtypes")
199 final Class parametersClass = Class.forName(args[p]);
200 final AbstractParameters parameters =
201 (AbstractParameters) new Gson().fromJson(args[p + 1], parametersClass);
202 ParameterService.registerParameters(parametersClass, parameters);
206 new ConcurrentContextMetricsJVM(args[0], jvmNo, threadCount, threadLoops, longArraySize, lockType);
207 } catch (final Exception e) {
208 LOGGER.error("error running test in JVM", e);