2b6c89de7bbef33c059d55060b94305675021078
[policy/apex-pdp.git] /
1 /*-
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
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.assertNull;
24
25 import java.util.Queue;
26 import java.util.concurrent.ConcurrentLinkedQueue;
27 import java.util.concurrent.atomic.AtomicLong;
28
29 import org.onap.policy.apex.service.engine.event.ApexEvent;
30 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * The listener interface for receiving testApexEvent events.
36  * The class that is interested in processing a testApexEvent
37  * event implements this interface, and the object created
38  * with that class is registered with a component using the
39  * component's <code>addTestApexEventListener</code> method. When
40  * the testApexEvent event occurs, that object's appropriate
41  * method is invoked.
42  *
43  * @see TestApexEventEvent
44  */
45 public class TestApexEventListener implements ApexEventListener {
46
47     private static final String SENT_TIMESTAMP = "SentTimestamp";
48     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestApexEventListener.class);
49     private static final String RECVD_TIMESTAMP = "RecvdTimestamp";
50     private Queue<ApexEvent> queue;
51
52     private final AtomicLong eventReceived = new AtomicLong();
53
54     /**
55      * Instantiates a new test apex event listener.
56      */
57     public TestApexEventListener() {
58         this.queue = new ConcurrentLinkedQueue<ApexEvent>();
59     }
60
61     /* (non-Javadoc)
62      * @see org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEventApexEvent)
63      */
64     @Override
65     public void onApexEvent(final ApexEvent apexEvent) {
66         apexEvent.put(RECVD_TIMESTAMP, System.currentTimeMillis());
67         eventReceived.incrementAndGet();
68         queue.add(apexEvent);
69     }
70
71     /**
72      * Prints the result.
73      */
74     public void printResult() {
75         if (!queue.isEmpty()) {
76             long maxTimeInMilliSeconds = 0;
77             long minTimeInMilliSeconds = Long.MAX_VALUE;
78             final long numEvents = queue.size();
79             long totalTimeInMilliSeconds = 0;
80             for (final ApexEvent apexEvent : queue) {
81                 assertNull(apexEvent.getExceptionMessage());
82                 final Long endTimeInMilliSeconds = (Long) apexEvent.get(RECVD_TIMESTAMP);
83                 final Long startTimeInMilliSeconds = (Long) apexEvent.get(SENT_TIMESTAMP);
84                 final long timeTaken = endTimeInMilliSeconds - startTimeInMilliSeconds;
85                 totalTimeInMilliSeconds += timeTaken;
86                 if (timeTaken > maxTimeInMilliSeconds) {
87                     maxTimeInMilliSeconds = timeTaken;
88                 }
89                 if (timeTaken < minTimeInMilliSeconds) {
90                     minTimeInMilliSeconds = timeTaken;
91                 }
92             }
93             LOGGER.info("Average Time Taken to process {} events: {} ms", numEvents,
94                     (totalTimeInMilliSeconds / numEvents));
95             LOGGER.info("Max Time Taken: {} ms", maxTimeInMilliSeconds);
96             LOGGER.info("Min Time Taken: {} ms", minTimeInMilliSeconds);
97         }
98     }
99
100     /**
101      * Reset.
102      */
103     public void reset() {
104         this.queue = new ConcurrentLinkedQueue<ApexEvent>();
105         eventReceived.set(0);;
106     }
107
108     /**
109      * Gets the queue.
110      *
111      * @return the queue
112      */
113     public Queue<ApexEvent> getQueue() {
114         return queue;
115     }
116
117     /**
118      * Gets the event received.
119      *
120      * @return the event received
121      */
122     public long getEventReceived() {
123         return eventReceived.get();
124     }
125
126 }