2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
24 import java.util.List;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
30 * This POJO class returns statistics on a event batch execution in Apex.
32 public class EventBatchStats {
33 private final int batchNumber;
34 private final int batchSize;
35 private final String apexClient;
38 private long eventsNotSent = 0;
39 private long eventsSent = 0;
40 private long eventsNotReceived = 0;
41 private long eventsReceived = 0;
42 private long averageRoundTripNano = 0;
43 private long shortestRoundTripNano = Long.MAX_VALUE;
44 private long longestRoundTripNano = 0;
45 private long averageApexExecutionNano = 0;
46 private long shortestApexExecutionNano = Long.MAX_VALUE;
47 private long longestApexExecutionNano = 0;
51 * Create a statistics object for an event batch.
53 * @param eventBatch the event batch for these statistics
55 public EventBatchStats(final EventBatch eventBatch) {
56 this.batchNumber = eventBatch.getBatchNumber();
57 this.batchSize = eventBatch.getBatchSize();
58 this.apexClient = eventBatch.getApexClient();
60 calcutateStats(eventBatch);
64 * Create a total statistics object for a list of event batches.
66 * @param eventBatchStatsList the event batch for these statistics
68 public EventBatchStats(final List<EventBatchStats> eventBatchStatsList) {
69 this.batchNumber = -1;
70 this.apexClient = "TOTAL";
72 calcutateStats(eventBatchStatsList);
74 this.batchSize = (int)(eventsNotSent + eventsSent);
78 * Compile the statistics.
79 * @param eventBatch the event batch for which statisticss should be calculated
81 private void calcutateStats(final EventBatch eventBatch) {
82 long accumulatedRoundTripTime = 0;
83 long accumulatedApexExecutionTime = 0;
85 for (int eventNo = 0; eventNo < batchSize; eventNo++) {
86 Pair<Long, Long> eventTimings = calculateEventTimings(eventBatch, eventNo);
87 if (eventTimings == null) {
88 // The event has not been sent yet or the response has not been received yet
92 accumulatedRoundTripTime += eventTimings.getLeft();
93 accumulatedApexExecutionTime += eventTimings.getRight();
96 if (eventsReceived != 0) {
97 averageRoundTripNano = accumulatedRoundTripTime / eventsReceived;
98 averageApexExecutionNano = accumulatedApexExecutionTime / eventsReceived;
103 * Compile the statistics.
104 * @param eventBatchStatsList the event batch list for which statistics should be calculated
106 private void calcutateStats(final List<EventBatchStats> eventBatchStatsList) {
107 long accumulatedRoundTripTime = 0;
108 long accumulatedApexExecutionTime = 0;
110 for (EventBatchStats eventBatchStats: eventBatchStatsList) {
112 eventsNotSent += eventBatchStats.getEventsNotSent();
113 eventsSent += eventBatchStats.getEventsSent();
114 eventsNotReceived += eventBatchStats.getEventsNotReceived();
115 eventsReceived += eventBatchStats.getEventsReceived();
118 if (shortestRoundTripNano > eventBatchStats.getShortestRoundTripNano()) {
119 shortestRoundTripNano = eventBatchStats.getShortestRoundTripNano();
122 if (shortestApexExecutionNano > eventBatchStats.getShortestApexExecutionNano()) {
123 shortestApexExecutionNano = eventBatchStats.getShortestApexExecutionNano();
126 if (longestRoundTripNano < eventBatchStats.getLongestRoundTripNano()) {
127 longestRoundTripNano = eventBatchStats.getLongestRoundTripNano();
130 if (longestApexExecutionNano < eventBatchStats.getLongestApexExecutionNano()) {
131 longestApexExecutionNano = eventBatchStats.getLongestApexExecutionNano();
134 accumulatedRoundTripTime += eventBatchStats.getAverageRoundTripNano();
135 accumulatedApexExecutionTime += eventBatchStats.getAverageApexExecutionNano();
138 if (!eventBatchStatsList.isEmpty()) {
139 averageRoundTripNano = accumulatedRoundTripTime / eventBatchStatsList.size();
140 averageApexExecutionNano = accumulatedApexExecutionTime / eventBatchStatsList.size();
145 * Calculate statistics for a single event.
146 * @param eventBatch the event batch for the event
147 * @param eventNo the event number of the event
148 * @return event timings
150 private Pair<Long, Long> calculateEventTimings(EventBatch eventBatch, int eventNo) {
151 // If an event is in a batch, it has been sent
154 OutputEvent outputEvent = eventBatch.getOutputEvent(eventNo);
156 if (outputEvent == null) {
165 long roundTrip = outputEvent.getTestReceviedTimestamp() - outputEvent.getTestTimestamp();
166 long apexExecution = outputEvent.getTestActStateTime() - outputEvent.getTestMatchStateTime();
169 if (shortestRoundTripNano > roundTrip) {
170 shortestRoundTripNano = roundTrip;
173 if (shortestApexExecutionNano > apexExecution) {
174 shortestApexExecutionNano = apexExecution;
177 if (longestRoundTripNano < roundTrip) {
178 longestRoundTripNano = roundTrip;
181 if (longestApexExecutionNano < apexExecution) {
182 longestApexExecutionNano = apexExecution;
185 return new ImmutablePair<>(roundTrip, apexExecution);
188 public int getBatchNumber() {
192 public int getBatchSize() {
196 public String getApexClient() {
200 public long getEventsNotSent() {
201 return eventsNotSent;
204 public long getEventsSent() {
208 public long getEventsNotReceived() {
209 return eventsNotReceived;
212 public long getEventsReceived() {
213 return eventsReceived;
216 public long getAverageRoundTripNano() {
217 return averageRoundTripNano;
220 public long getShortestRoundTripNano() {
221 return shortestRoundTripNano;
224 public long getLongestRoundTripNano() {
225 return longestRoundTripNano;
228 public long getAverageApexExecutionNano() {
229 return averageApexExecutionNano;
232 public long getShortestApexExecutionNano() {
233 return shortestApexExecutionNano;
236 public long getLongestApexExecutionNano() {
237 return longestApexExecutionNano;