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=========================================================
20 package org.onap.policy.apex.service.engine.benchmark;
22 import static org.junit.Assert.assertNull;
24 import java.util.Queue;
25 import java.util.concurrent.ConcurrentLinkedQueue;
26 import java.util.concurrent.atomic.AtomicLong;
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;
33 public class TestApexEventListener implements ApexEventListener {
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;
40 private final AtomicLong eventReceived = new AtomicLong();
42 public TestApexEventListener() {
43 this.queue = new ConcurrentLinkedQueue<ApexEvent>();
47 public void onApexEvent(final ApexEvent apexEvent) {
48 apexEvent.put(RECVD_TIMESTAMP, System.currentTimeMillis());
49 eventReceived.incrementAndGet();
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;
68 if (timeTaken < minTimeInMilliSeconds) {
69 minTimeInMilliSeconds = timeTaken;
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);
80 this.queue = new ConcurrentLinkedQueue<ApexEvent>();
81 eventReceived.set(0);;
84 public Queue<ApexEvent> getQueue() {
88 public long getEventReceived() {
89 return eventReceived.get();