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
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.assertNull;
25 import java.util.Queue;
26 import java.util.concurrent.ConcurrentLinkedQueue;
27 import java.util.concurrent.atomic.AtomicLong;
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;
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
43 * @see TestApexEventEvent
45 public class TestApexEventListener implements ApexEventListener {
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;
52 private final AtomicLong eventReceived = new AtomicLong();
55 * Instantiates a new test apex event listener.
57 public TestApexEventListener() {
58 this.queue = new ConcurrentLinkedQueue<ApexEvent>();
62 * @see org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEventApexEvent)
65 public void onApexEvent(final ApexEvent apexEvent) {
66 apexEvent.put(RECVD_TIMESTAMP, System.currentTimeMillis());
67 eventReceived.incrementAndGet();
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;
89 if (timeTaken < minTimeInMilliSeconds) {
90 minTimeInMilliSeconds = timeTaken;
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);
103 public void reset() {
104 this.queue = new ConcurrentLinkedQueue<ApexEvent>();
105 eventReceived.set(0);;
113 public Queue<ApexEvent> getQueue() {
118 * Gets the event received.
120 * @return the event received
122 public long getEventReceived() {
123 return eventReceived.get();