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.testsuites.performance.benchmark.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.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;
 
  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;
 
  58      * @throws Exception the exception
 
  61     public void setUp() throws Exception {
 
  62         apexEcaModelString = Utils.getModelString(new EvalDomainModelFactory().getEcaPolicyModel());
 
  63         apexOodaModelString = Utils.getModelString(new EvalDomainModelFactory().getOodaPolicyModel());
 
  67      * Test benchmark singleton worker.
 
  69      * @throws Exception the exception
 
  72     public void testBenchmark_SingletonWorker() throws Exception {
 
  73         executeTest(apexEcaModelString, 100, 1, 20);
 
  74         executeTest(apexOodaModelString, 100, 1, 20);
 
  78      * Test benchmark 3 thread worker.
 
  80      * @throws Exception the exception
 
  83     public void testBenchmark_3ThreadWorker() throws Exception {
 
  84         executeTest(apexEcaModelString, 1000, 3, 10);
 
  85         executeTest(apexOodaModelString, 100, 3, 10);
 
  89      * Test benchmark 10 thread worker.
 
  91      * @throws Exception the exception
 
  94     public void testBenchmark_10ThreadWorker() throws Exception {
 
  95         executeTest(apexEcaModelString, 2000, 10, 10);
 
  96         executeTest(apexOodaModelString, 2000, 10, 10);
 
 100      * Test benchmark 50 thread worker.
 
 102      * @throws Exception the exception
 
 105     public void testBenchmark_50ThreadWorker() throws Exception {
 
 106         executeTest(apexEcaModelString, 3000, 50, 10);
 
 107         executeTest(apexOodaModelString, 3000, 50, 10);
 
 111      * Test available processors thread worker.
 
 113      * @throws Exception the exception
 
 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);
 
 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
 
 131     private void executeTest(final String policyModel, final int eventsCount, final int threads, final int loop)
 
 134         LOGGER.info("Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount, threads, loop);
 
 135         final TestApexEventListener apexEventListener = new TestApexEventListener();
 
 137         final ApexBaseBenchMarkTest apexBaseBenchMarkTest =
 
 138                 new ApexBaseBenchMarkTest(policyModel, threads, apexEventListener);
 
 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 ... ");
 
 150                     ThreadUtilities.sleep(500);
 
 152                 assertEquals(eventsCount, apexEventListener.getEventReceived());
 
 153                 apexEventListener.printResult();
 
 154                 apexEventListener.reset();
 
 156         } catch (final Exception exception) {
 
 157             Assert.fail(exception);
 
 159             apexBaseBenchMarkTest.destroy();
 
 160             LOGGER.info("Finished Running Test with Event count: {}, Instance count: {} and loop: {}", eventsCount,
 
 168      * @param apexBaseBenchMarkTest the apex base bench mark test
 
 169      * @param eventsCount the events count
 
 170      * @throws Exception the exception
 
 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);