2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
25 import java.util.List;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
32 * This POJO class returns statistics on a event batch execution in Apex.
35 public class EventBatchStats {
36 private final int batchNumber;
37 private final int batchSize;
38 private final String apexClient;
41 private long eventsNotSent = 0;
42 private long eventsSent = 0;
43 private long eventsNotReceived = 0;
44 private long eventsReceived = 0;
45 private long averageRoundTripNano = 0;
46 private long shortestRoundTripNano = Long.MAX_VALUE;
47 private long longestRoundTripNano = 0;
48 private long averageApexExecutionNano = 0;
49 private long shortestApexExecutionNano = Long.MAX_VALUE;
50 private long longestApexExecutionNano = 0;
54 * Create a statistics object for an event batch.
56 * @param eventBatch the event batch for these statistics
58 public EventBatchStats(final EventBatch eventBatch) {
59 this.batchNumber = eventBatch.getBatchNumber();
60 this.batchSize = eventBatch.getBatchSize();
61 this.apexClient = eventBatch.getApexClient();
63 calcutateStats(eventBatch);
67 * Create a total statistics object for a list of event batches.
69 * @param eventBatchStatsList the event batch for these statistics
71 public EventBatchStats(final List<EventBatchStats> eventBatchStatsList) {
72 this.batchNumber = -1;
73 this.apexClient = "TOTAL";
75 calcutateStats(eventBatchStatsList);
77 this.batchSize = (int) (eventsNotSent + eventsSent);
81 * Compile the statistics.
82 * @param eventBatch the event batch for which statisticss should be calculated
84 private void calcutateStats(final EventBatch eventBatch) {
85 long accumulatedRoundTripTime = 0;
86 long accumulatedApexExecutionTime = 0;
88 for (int eventNo = 0; eventNo < batchSize; eventNo++) {
89 Pair<Long, Long> eventTimings = calculateEventTimings(eventBatch, eventNo);
90 if (eventTimings == null) {
91 // The event has not been sent yet or the response has not been received yet
95 accumulatedRoundTripTime += eventTimings.getLeft();
96 accumulatedApexExecutionTime += eventTimings.getRight();
99 if (eventsReceived != 0) {
100 averageRoundTripNano = accumulatedRoundTripTime / eventsReceived;
101 averageApexExecutionNano = accumulatedApexExecutionTime / eventsReceived;
106 * Compile the statistics.
107 * @param eventBatchStatsList the event batch list for which statistics should be calculated
109 private void calcutateStats(final List<EventBatchStats> eventBatchStatsList) {
110 long accumulatedRoundTripTime = 0;
111 long accumulatedApexExecutionTime = 0;
113 for (EventBatchStats eventBatchStats: eventBatchStatsList) {
115 eventsNotSent += eventBatchStats.getEventsNotSent();
116 eventsSent += eventBatchStats.getEventsSent();
117 eventsNotReceived += eventBatchStats.getEventsNotReceived();
118 eventsReceived += eventBatchStats.getEventsReceived();
121 if (shortestRoundTripNano > eventBatchStats.getShortestRoundTripNano()) {
122 shortestRoundTripNano = eventBatchStats.getShortestRoundTripNano();
125 if (shortestApexExecutionNano > eventBatchStats.getShortestApexExecutionNano()) {
126 shortestApexExecutionNano = eventBatchStats.getShortestApexExecutionNano();
129 if (longestRoundTripNano < eventBatchStats.getLongestRoundTripNano()) {
130 longestRoundTripNano = eventBatchStats.getLongestRoundTripNano();
133 if (longestApexExecutionNano < eventBatchStats.getLongestApexExecutionNano()) {
134 longestApexExecutionNano = eventBatchStats.getLongestApexExecutionNano();
137 accumulatedRoundTripTime += eventBatchStats.getAverageRoundTripNano();
138 accumulatedApexExecutionTime += eventBatchStats.getAverageApexExecutionNano();
141 if (!eventBatchStatsList.isEmpty()) {
142 averageRoundTripNano = accumulatedRoundTripTime / eventBatchStatsList.size();
143 averageApexExecutionNano = accumulatedApexExecutionTime / eventBatchStatsList.size();
148 * Calculate statistics for a single event.
149 * @param eventBatch the event batch for the event
150 * @param eventNo the event number of the event
151 * @return event timings
153 private Pair<Long, Long> calculateEventTimings(EventBatch eventBatch, int eventNo) {
154 // If an event is in a batch, it has been sent
157 OutputEvent outputEvent = eventBatch.getOutputEvent(eventNo);
159 if (outputEvent == null) {
167 long roundTrip = outputEvent.getTestReceviedTimestamp() - outputEvent.getTestTimestamp();
168 long apexExecution = outputEvent.getTestActStateTime() - outputEvent.getTestMatchStateTime();
171 if (shortestRoundTripNano > roundTrip) {
172 shortestRoundTripNano = roundTrip;
175 if (shortestApexExecutionNano > apexExecution) {
176 shortestApexExecutionNano = apexExecution;
179 if (longestRoundTripNano < roundTrip) {
180 longestRoundTripNano = roundTrip;
183 if (longestApexExecutionNano < apexExecution) {
184 longestApexExecutionNano = apexExecution;
187 return new ImmutablePair<>(roundTrip, apexExecution);