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.service.engine.benchmark;
23 import static org.junit.Assert.assertEquals;
25 import java.util.concurrent.TimeUnit;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.service.engine.event.ApexEvent;
31 import org.onap.policy.apex.service.engine.utils.Utils;
32 import org.onap.policy.apex.test.common.model.EvalDomainModelFactory;
33 import org.python.icu.impl.Assert;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
38 * The Class ApexEngineBenchmark.
40 public class ApexEngineBenchmark {
41 // Logger for this class
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEngineBenchmark.class);
44 private static final String TARGET = "apex";
45 private static final String SOURCE = "test";
46 private static final String NAME = "Event0000";
47 private static final String VERSION = "0.0.1";
48 private static final String PACKAGE = "org.onap.policy.apex.domains.sample.events";
50 private static final long TIME_OUT_IN_MILLISEC = TimeUnit.MINUTES.toMillis(1);
52 private String apexECAModelString;
53 private String apexOODAModelString;
56 public void setUp() throws Exception {
57 apexECAModelString = Utils.getModelString(new EvalDomainModelFactory().getECAPolicyModel());
58 apexOODAModelString = Utils.getModelString(new EvalDomainModelFactory().getOODAPolicyModel());
62 public void testBenchmark_SingletonWorker() throws Exception {
63 executeTest(apexECAModelString, 100, 1, 20);
64 executeTest(apexOODAModelString, 100, 1, 20);
68 public void testBenchmark_3ThreadWorker() throws Exception {
69 executeTest(apexECAModelString, 1000, 3, 10);
70 executeTest(apexOODAModelString, 100, 3, 10);
74 public void testBenchmark_10ThreadWorker() throws Exception {
75 executeTest(apexECAModelString, 2000, 10, 10);
76 executeTest(apexOODAModelString, 2000, 10, 10);
80 public void testBenchmark_50ThreadWorker() throws Exception {
81 executeTest(apexECAModelString, 3000, 50, 10);
82 executeTest(apexOODAModelString, 3000, 50, 10);
86 public void TestE_AvailableProcessorsThreadWorker() throws Exception {
87 final int cores = Runtime.getRuntime().availableProcessors();
88 executeTest(apexECAModelString, 3000, cores, 10);
89 executeTest(apexOODAModelString, 3000, cores, 10);
92 private void executeTest(final String policyModel, final int eventsCount, final int threads, final int loop)
95 LOGGER.info("Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount, threads, loop);
96 final TestApexEventListener apexEventListener = new TestApexEventListener();
98 final ApexBaseBenchMarkTest apexBaseBenchMarkTest =
99 new ApexBaseBenchMarkTest(policyModel, threads, apexEventListener);
102 apexBaseBenchMarkTest.setUp();
103 for (int i = 0; i < loop; i++) {
104 sendEvents(apexBaseBenchMarkTest, eventsCount);
105 final long currentTimeInMillSec = System.currentTimeMillis();
106 while (apexEventListener.getEventReceived() < eventsCount) {
107 if (System.currentTimeMillis() - currentTimeInMillSec > TIME_OUT_IN_MILLISEC) {
108 LOGGER.warn("Wait timed out ... ");
111 ThreadUtilities.sleep(500);
113 assertEquals(eventsCount, apexEventListener.getEventReceived());
114 apexEventListener.printResult();
115 apexEventListener.reset();
117 } catch (final Exception exception) {
118 Assert.fail(exception);
120 apexBaseBenchMarkTest.destroy();
121 LOGGER.info("Finished Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount,
126 public void sendEvents(final ApexBaseBenchMarkTest apexBaseBenchMarkTest, final int eventsCount) throws Exception {
127 for (int eventNum = 0; eventNum < eventsCount; eventNum++) {
128 final long currentTimeMillis = System.currentTimeMillis();
129 final ApexEvent event = new ApexEvent(NAME, VERSION, PACKAGE, SOURCE, TARGET);
130 event.put("TestTemperature", eventNum);
131 event.put("FirstEventTimestamp", currentTimeMillis);
132 event.put("SentTimestamp", currentTimeMillis);
133 event.put("EventNumber", eventNum);
134 apexBaseBenchMarkTest.sendEvent(event);