fdefd5583999754d195337ff90d35e762e06ac87
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.testsuites.performance.benchmark.engine.benchmark;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.TimeUnit;
26
27 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
28 import org.onap.policy.apex.context.parameters.ContextParameters;
29 import org.onap.policy.apex.context.parameters.SchemaParameters;
30 import org.onap.policy.apex.core.engine.EngineParameters;
31 import org.onap.policy.apex.core.engine.ExecutorParameters;
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.plugins.context.schema.avro.AvroSchemaHelperParameters;
36 import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters;
37 import org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters;
38 import org.onap.policy.apex.plugins.executor.jruby.JrubyExecutorParameters;
39 import org.onap.policy.apex.plugins.executor.jython.JythonExecutorParameters;
40 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
41 import org.onap.policy.apex.service.engine.event.ApexEvent;
42 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
43 import org.onap.policy.apex.service.engine.runtime.EngineService;
44 import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
45 import org.onap.policy.apex.service.engine.runtime.impl.EngineServiceImpl;
46 import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
47 import org.onap.policy.apex.testsuites.performance.benchmark.engine.runtime.ApexServiceModelUpdateTest;
48 import org.slf4j.ext.XLogger;
49 import org.slf4j.ext.XLoggerFactory;
50
51 /**
52  * The Class ApexBaseBenchMarkTest.
53  */
54 public class ApexBaseBenchMarkTest {
55     private static final long STOP_TIME_OUT = TimeUnit.SECONDS.toMillis(30);
56     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexServiceModelUpdateTest.class);
57     private static final long MAX_START_WAIT = TimeUnit.SECONDS.toMillis(10);
58     private final AxArtifactKey engineServiceKey = new AxArtifactKey("Machine-1_process-1_engine-1", "0.0.0");
59     private final String model;
60     private final int threads;
61     private final ApexEventListener listener;
62     private EngineService service;
63     private EngineServiceEventInterface engineServiceEventInterface;
64
65
66     /**
67      * Instantiates a new apex base bench mark test.
68      *
69      * @param model the model
70      * @param threads the threads
71      * @param listener the listener
72      */
73     public ApexBaseBenchMarkTest(final String model, final int threads, final ApexEventListener listener) {
74         this.model = model;
75         this.threads = threads;
76         this.listener = listener;
77     }
78
79     /**
80      * Sets the up.
81      *
82      * @throws Exception the exception
83      */
84     public void setUp() throws Exception {
85         final EngineServiceParameters parameters = new EngineServiceParameters();
86         parameters.setInstanceCount(threads);
87         parameters.setName(engineServiceKey.getName());
88         parameters.setVersion(engineServiceKey.getVersion());
89         parameters.setId(100);
90
91         final EngineParameters engineParameters = parameters.getEngineParameters();
92         final Map<String, ExecutorParameters> executorParameterMap = engineParameters.getExecutorParameterMap();
93         executorParameterMap.put("MVEL", new MvelExecutorParameters());
94         executorParameterMap.put("JAVASCRIPT", new JavascriptExecutorParameters());
95         executorParameterMap.put("JYTHON", new JythonExecutorParameters());
96         executorParameterMap.put("JAVA", new JavaExecutorParameters());
97         executorParameterMap.put("JRUBY", new JrubyExecutorParameters());
98
99         final ContextParameters contextParameters = engineParameters.getContextParameters();
100         final SchemaParameters schemaParameters = contextParameters.getSchemaParameters();
101         schemaParameters.getSchemaHelperParameterMap().put("Avro", new AvroSchemaHelperParameters());
102         schemaParameters.getSchemaHelperParameterMap().put("Java", new JavaSchemaHelperParameters());
103         service = EngineServiceImpl.create(parameters);
104
105         service = EngineServiceImpl.create(parameters);
106         service.registerActionListener("listener", listener);
107         service.updateModel(parameters.getEngineKey(), model, true);
108
109         LOGGER.info("Starting EngineService ... ");
110         service.startAll();
111
112         final long starttime = System.currentTimeMillis();
113         while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
114             ThreadUtilities.sleep(50);
115         }
116         if (!service.isStarted()) {
117             LOGGER.error("Apex Service {} failed to start after {} ms", service.getKey(), MAX_START_WAIT);
118             new ApexException("Unable to start engine service ");
119         }
120
121         engineServiceEventInterface = service.getEngineServiceEventInterface();
122     }
123
124     /**
125      * Send events.
126      *
127      * @param events the events
128      */
129     public void sendEvents(final List<ApexEvent> events) {
130         for (final ApexEvent event : events) {
131             engineServiceEventInterface.sendEvent(event);
132         }
133     }
134
135     /**
136      * Send event.
137      *
138      * @param event the event
139      */
140     public void sendEvent(final ApexEvent event) {
141         engineServiceEventInterface.sendEvent(event);
142     }
143
144
145     /**
146      * Gets the service.
147      *
148      * @return the service
149      */
150     public EngineService getService() {
151         return service;
152     }
153
154     /**
155      * Destroy.
156      *
157      * @throws Exception the exception
158      */
159     public void destroy() throws Exception {
160         if (service != null) {
161             LOGGER.info("Stopping EngineService ... ");
162             service.stop();
163             final long currentTimeInMillSec = System.currentTimeMillis();
164             while (!service.isStopped()) {
165                 if (System.currentTimeMillis() - currentTimeInMillSec > STOP_TIME_OUT) {
166                     LOGGER.warn("Timed Out EngineService status: ", service.isStopped());
167                     break;
168                 }
169                 ThreadUtilities.sleep(500);
170             }
171             service = null;
172         }
173     }
174
175 }