ed81e39a1e1f0dcb86e6e0df41e272ae71eafc1a
[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.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.util.Arrays;
27
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;
41
42 import com.google.gson.Gson;
43 import com.google.gson.JsonSyntaxException;
44
45 /**
46  * The Class ConcurrentContextMetricsJVM rins in its own JVM to test concurrent context updates and
47  * lockings across JVMs.
48  *
49  * @author Liam Fallon (liam.fallon@ericsson.com)
50  */
51 public final class ConcurrentContextMetricsJVM {
52     // Logger for this class
53     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetricsJVM.class);
54
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;
61
62     private static final int WAIT_10_MS = 10;
63
64     /**
65      * The Constructor for this class.
66      *
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
75      */
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 . . .");
79
80         final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
81
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);
85
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")};
88
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);
95
96         final Thread[] threadArray = new Thread[threadCount];
97
98         for (int t = 0; t < threadCount; t++) {
99             threadArray[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());
104         }
105
106         System.out.println("ReadyToGo");
107         while (true) {
108             final String goLine = bufferedReader.readLine();
109             if (!goLine.trim().equals("OffYouGo")) {
110                 throw new IOException("Expected OffYouGo");
111             }
112             break;
113         }
114
115         boolean allFinished;
116         do {
117             allFinished = true;
118             for (int t = 0; t < threadCount; t++) {
119                 if (threadArray[t].isAlive()) {
120                     allFinished = false;
121                     ThreadUtilities.sleep(WAIT_10_MS);
122                     break;
123                 }
124             }
125         } while (!allFinished);
126
127         System.out.println("AllFinished");
128         while (true) {
129             final String goLine = bufferedReader.readLine();
130             if (!goLine.trim().equals("FinishItOut")) {
131                 throw new IOException("Expected FinishItOut");
132             }
133             break;
134         }
135
136         LOGGER.debug("threads finished");
137         contextDistributor.clear();
138     }
139
140     /**
141      * The main method.
142      *
143      * @param args the args
144      * @throws JsonSyntaxException the json syntax exception
145      * @throws ClassNotFoundException the class not found exception
146      */
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));
151             LOGGER.error(
152                     "usage: ConcurrentContextMetricsJVM testLabel jvmNo threadCount threadLoops longArraySize lockType [parameterKey parameterJson].... ");
153             return;
154         }
155
156         int jvmNo = -1;
157         int threadCount = -1;
158         int threadLoops = -1;
159         int longArraySize = -1;
160         int lockType = -1;
161
162         try {
163             jvmNo = Integer.parseInt(args[ARG_JVM_NO]);
164         } catch (final Exception e) {
165             LOGGER.error("invalid argument jvmNo", e);
166             return;
167         }
168
169         try {
170             threadCount = Integer.parseInt(args[ARG_THREAD_COUNT]);
171         } catch (final Exception e) {
172             LOGGER.error("invalid argument threadCount", e);
173             return;
174         }
175
176         try {
177             threadLoops = Integer.parseInt(args[ARG_ITERATIONS]);
178         } catch (final Exception e) {
179             LOGGER.error("invalid argument threadLoops", e);
180             return;
181         }
182
183         try {
184             longArraySize = Integer.parseInt(args[ARG_ARRAY_SIZE]);
185         } catch (final Exception e) {
186             LOGGER.error("invalid argument longArraySize", e);
187             return;
188         }
189
190         try {
191             lockType = Integer.parseInt(args[ARG_LOCK_TYPE]);
192         } catch (final Exception e) {
193             LOGGER.error("invalid argument lockType", e);
194             return;
195         }
196
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);
203         }
204
205         try {
206             new ConcurrentContextMetricsJVM(args[0], jvmNo, threadCount, threadLoops, longArraySize, lockType);
207         } catch (final Exception e) {
208             LOGGER.error("error running test in JVM", e);
209             return;
210         }
211     }
212 }