b2de8f07215709e2b77928b1b6f3bf99e28cccb1
[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 package org.onap.policy.apex.service.engine.benchmark;
21
22 import static org.junit.Assert.assertNull;
23
24 import java.util.Queue;
25 import java.util.concurrent.ConcurrentLinkedQueue;
26 import java.util.concurrent.atomic.AtomicLong;
27
28 import org.onap.policy.apex.service.engine.event.ApexEvent;
29 import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 public class TestApexEventListener implements ApexEventListener {
34
35     private static final String SENT_TIMESTAMP = "SentTimestamp";
36     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestApexEventListener.class);
37     private static final String RECVD_TIMESTAMP = "RecvdTimestamp";
38     private Queue<ApexEvent> queue;
39
40     private final AtomicLong eventReceived = new AtomicLong();
41
42     public TestApexEventListener() {
43         this.queue = new ConcurrentLinkedQueue<ApexEvent>();
44     }
45
46     @Override
47     public void onApexEvent(final ApexEvent apexEvent) {
48         apexEvent.put(RECVD_TIMESTAMP, System.currentTimeMillis());
49         eventReceived.incrementAndGet();
50         queue.add(apexEvent);
51     }
52
53     public void printResult() {
54         if (!queue.isEmpty()) {
55             long maxTimeInMilliSeconds = 0;
56             long minTimeInMilliSeconds = Long.MAX_VALUE;
57             final long numEvents = queue.size();
58             long totalTimeInMilliSeconds = 0;
59             for (final ApexEvent apexEvent : queue) {
60                 assertNull(apexEvent.getExceptionMessage());
61                 final Long endTimeInMilliSeconds = (Long) apexEvent.get(RECVD_TIMESTAMP);
62                 final Long startTimeInMilliSeconds = (Long) apexEvent.get(SENT_TIMESTAMP);
63                 final long timeTaken = endTimeInMilliSeconds - startTimeInMilliSeconds;
64                 totalTimeInMilliSeconds += timeTaken;
65                 if (timeTaken > maxTimeInMilliSeconds) {
66                     maxTimeInMilliSeconds = timeTaken;
67                 }
68                 if (timeTaken < minTimeInMilliSeconds) {
69                     minTimeInMilliSeconds = timeTaken;
70                 }
71             }
72             LOGGER.info("Average Time Taken to process {} events: {} ms", numEvents,
73                     (totalTimeInMilliSeconds / numEvents));
74             LOGGER.info("Max Time Taken: {} ms", maxTimeInMilliSeconds);
75             LOGGER.info("Min Time Taken: {} ms", minTimeInMilliSeconds);
76         }
77     }
78
79     public void reset() {
80         this.queue = new ConcurrentLinkedQueue<ApexEvent>();
81         eventReceived.set(0);;
82     }
83
84     public Queue<ApexEvent> getQueue() {
85         return queue;
86     }
87
88     public long getEventReceived() {
89         return eventReceived.get();
90     }
91
92 }