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
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.testsuites.performance.benchmark.engine.benchmark;
23 import java.util.List;
25 import java.util.concurrent.TimeUnit;
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;
52 * The Class ApexBaseBenchMarkTest.
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;
67 * Instantiates a new apex base bench mark test.
69 * @param model the model
70 * @param threads the threads
71 * @param listener the listener
73 public ApexBaseBenchMarkTest(final String model, final int threads, final ApexEventListener listener) {
75 this.threads = threads;
76 this.listener = listener;
82 * @throws Exception the exception
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);
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());
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);
105 service = EngineServiceImpl.create(parameters);
106 service.registerActionListener("listener", listener);
107 service.updateModel(parameters.getEngineKey(), model, true);
109 LOGGER.info("Starting EngineService ... ");
112 final long starttime = System.currentTimeMillis();
113 while (!service.isStarted() && System.currentTimeMillis() - starttime < MAX_START_WAIT) {
114 ThreadUtilities.sleep(50);
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 ");
121 engineServiceEventInterface = service.getEngineServiceEventInterface();
127 * @param events the events
129 public void sendEvents(final List<ApexEvent> events) {
130 for (final ApexEvent event : events) {
131 engineServiceEventInterface.sendEvent(event);
138 * @param event the event
140 public void sendEvent(final ApexEvent event) {
141 engineServiceEventInterface.sendEvent(event);
148 * @return the service
150 public EngineService getService() {
157 * @throws Exception the exception
159 public void destroy() throws Exception {
160 if (service != null) {
161 LOGGER.info("Stopping EngineService ... ");
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());
169 ThreadUtilities.sleep(500);