fa2a9934cea3ca0645603a4939dd897384d4bd5d
[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.testsuites.performance.benchmark.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.testsuites.integration.common.model.EvalDomainModelFactory;
32 import org.onap.policy.apex.testsuites.performance.benchmark.engine.utils.Utils;
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     /**
56      * Sets the up.
57      *
58      * @throws Exception the exception
59      */
60     @Before
61     public void setUp() throws Exception {
62         apexEcaModelString = Utils.getModelString(new EvalDomainModelFactory().getEcaPolicyModel());
63         apexOodaModelString = Utils.getModelString(new EvalDomainModelFactory().getOodaPolicyModel());
64     }
65
66     /**
67      * Test benchmark singleton worker.
68      *
69      * @throws Exception the exception
70      */
71     @Test
72     public void testBenchmark_SingletonWorker() throws Exception {
73         executeTest(apexEcaModelString, 100, 1, 20);
74         executeTest(apexOodaModelString, 100, 1, 20);
75     }
76
77     /**
78      * Test benchmark 3 thread worker.
79      *
80      * @throws Exception the exception
81      */
82     @Test
83     public void testBenchmark_3ThreadWorker() throws Exception {
84         executeTest(apexEcaModelString, 1000, 3, 10);
85         executeTest(apexOodaModelString, 100, 3, 10);
86     }
87
88     /**
89      * Test benchmark 10 thread worker.
90      *
91      * @throws Exception the exception
92      */
93     @Test
94     public void testBenchmark_10ThreadWorker() throws Exception {
95         executeTest(apexEcaModelString, 2000, 10, 10);
96         executeTest(apexOodaModelString, 2000, 10, 10);
97     }
98
99     /**
100      * Test benchmark 50 thread worker.
101      *
102      * @throws Exception the exception
103      */
104     @Test
105     public void testBenchmark_50ThreadWorker() throws Exception {
106         executeTest(apexEcaModelString, 3000, 50, 10);
107         executeTest(apexOodaModelString, 3000, 50, 10);
108     }
109
110     /**
111      * Test available processors thread worker.
112      *
113      * @throws Exception the exception
114      */
115     @Test
116     public void testAvailableProcessorsThreadWorker() throws Exception {
117         final int cores = Runtime.getRuntime().availableProcessors();
118         executeTest(apexEcaModelString, 3000, cores, 10);
119         executeTest(apexOodaModelString, 3000, cores, 10);
120     }
121
122     /**
123      * Execute test.
124      *
125      * @param policyModel the policy model
126      * @param eventsCount the events count
127      * @param threads the threads
128      * @param loop the loop
129      * @throws Exception the exception
130      */
131     private void executeTest(final String policyModel, final int eventsCount, final int threads, final int loop)
132             throws Exception {
133
134         LOGGER.info("Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount, threads, loop);
135         final TestApexEventListener apexEventListener = new TestApexEventListener();
136
137         final ApexBaseBenchMarkTest apexBaseBenchMarkTest =
138                 new ApexBaseBenchMarkTest(policyModel, threads, apexEventListener);
139
140         try {
141             apexBaseBenchMarkTest.setUp();
142             for (int i = 0; i < loop; i++) {
143                 sendEvents(apexBaseBenchMarkTest, eventsCount);
144                 final long currentTimeInMillSec = System.currentTimeMillis();
145                 while (apexEventListener.getEventReceived() < eventsCount) {
146                     if (System.currentTimeMillis() - currentTimeInMillSec > TIME_OUT_IN_MILLISEC) {
147                         LOGGER.warn("Wait timed out ... ");
148                         break;
149                     }
150                     ThreadUtilities.sleep(500);
151                 }
152                 assertEquals(eventsCount, apexEventListener.getEventReceived());
153                 apexEventListener.printResult();
154                 apexEventListener.reset();
155             }
156         } catch (final Exception exception) {
157             Assert.fail(exception);
158         } finally {
159             apexBaseBenchMarkTest.destroy();
160             LOGGER.info("Finished Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount,
161                     threads, loop);
162         }
163     }
164
165     /**
166      * Send events.
167      *
168      * @param apexBaseBenchMarkTest the apex base bench mark test
169      * @param eventsCount the events count
170      * @throws Exception the exception
171      */
172     public void sendEvents(final ApexBaseBenchMarkTest apexBaseBenchMarkTest, final int eventsCount) throws Exception {
173         for (int eventNum = 0; eventNum < eventsCount; eventNum++) {
174             final long currentTimeMillis = System.currentTimeMillis();
175             final ApexEvent event = new ApexEvent(NAME, VERSION, PACKAGE, SOURCE, TARGET);
176             event.put("TestTemperature", eventNum);
177             event.put("FirstEventTimestamp", currentTimeMillis);
178             event.put("SentTimestamp", currentTimeMillis);
179             event.put("EventNumber", eventNum);
180             apexBaseBenchMarkTest.sendEvent(event);
181         }
182     }
183 }