d7a4ef800e5bba62fe6ba4327e72fcbb6ebe4aa3
[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.service.engine.benchmark;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.concurrent.TimeUnit;
26
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;
36
37 /**
38  * The Class ApexEngineBenchmark.
39  */
40 public class ApexEngineBenchmark {
41     // Logger for this class
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEngineBenchmark.class);
43
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";
49
50     private static final long TIME_OUT_IN_MILLISEC = TimeUnit.MINUTES.toMillis(1);
51
52     private String apexECAModelString;
53     private String apexOODAModelString;
54
55     @Before
56     public void setUp() throws Exception {
57         apexECAModelString = Utils.getModelString(new EvalDomainModelFactory().getECAPolicyModel());
58         apexOODAModelString = Utils.getModelString(new EvalDomainModelFactory().getOODAPolicyModel());
59     }
60
61     @Test
62     public void testBenchmark_SingletonWorker() throws Exception {
63         executeTest(apexECAModelString, 100, 1, 20);
64         executeTest(apexOODAModelString, 100, 1, 20);
65     }
66
67     @Test
68     public void testBenchmark_3ThreadWorker() throws Exception {
69         executeTest(apexECAModelString, 1000, 3, 10);
70         executeTest(apexOODAModelString, 100, 3, 10);
71     }
72
73     @Test
74     public void testBenchmark_10ThreadWorker() throws Exception {
75         executeTest(apexECAModelString, 2000, 10, 10);
76         executeTest(apexOODAModelString, 2000, 10, 10);
77     }
78
79     @Test
80     public void testBenchmark_50ThreadWorker() throws Exception {
81         executeTest(apexECAModelString, 3000, 50, 10);
82         executeTest(apexOODAModelString, 3000, 50, 10);
83     }
84
85     @Test
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);
90     }
91
92     private void executeTest(final String policyModel, final int eventsCount, final int threads, final int loop)
93             throws Exception {
94
95         LOGGER.info("Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount, threads, loop);
96         final TestApexEventListener apexEventListener = new TestApexEventListener();
97
98         final ApexBaseBenchMarkTest apexBaseBenchMarkTest =
99                 new ApexBaseBenchMarkTest(policyModel, threads, apexEventListener);
100
101         try {
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 ... ");
109                         break;
110                     }
111                     ThreadUtilities.sleep(500);
112                 }
113                 assertEquals(eventsCount, apexEventListener.getEventReceived());
114                 apexEventListener.printResult();
115                 apexEventListener.reset();
116             }
117         } catch (final Exception exception) {
118             Assert.fail(exception);
119         } finally {
120             apexBaseBenchMarkTest.destroy();
121             LOGGER.info("Finished Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount,
122                     threads, loop);
123         }
124     }
125
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);
135         }
136     }
137 }