Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / performance / performance-benchmark-test / src / main / java / org / onap / policy / apex / testsuites / performance / benchmark / eventgenerator / EventBatchStats.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
23
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;
28
29 /**
30  * This POJO class returns statistics on a event batch execution in Apex.
31  */
32 public class EventBatchStats {
33     private final int batchNumber;
34     private final int batchSize;
35     private final String apexClient;
36
37     // @formatter:off
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;
48     // @formatter:on
49
50     /**
51      * Create a statistics object for an event batch.
52      *
53      * @param eventBatch the event batch for these statistics
54      */
55     public EventBatchStats(final EventBatch eventBatch) {
56         this.batchNumber = eventBatch.getBatchNumber();
57         this.batchSize = eventBatch.getBatchSize();
58         this.apexClient = eventBatch.getApexClient();
59
60         calcutateStats(eventBatch);
61     }
62
63     /**
64      * Create a total statistics object for a list of event batches.
65      *
66      * @param eventBatchStatsList the event batch for these statistics
67      */
68     public EventBatchStats(final List<EventBatchStats> eventBatchStatsList) {
69         this.batchNumber = -1;
70         this.apexClient = "TOTAL";
71
72         calcutateStats(eventBatchStatsList);
73
74         this.batchSize = (int) (eventsNotSent + eventsSent);
75     }
76
77     /**
78      * Compile the statistics.
79      * @param eventBatch the event batch for which statisticss should be calculated
80      */
81     private void calcutateStats(final EventBatch eventBatch) {
82         long accumulatedRoundTripTime = 0;
83         long accumulatedApexExecutionTime = 0;
84
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
89                 continue;
90             }
91
92             accumulatedRoundTripTime += eventTimings.getLeft();
93             accumulatedApexExecutionTime += eventTimings.getRight();
94         }
95
96         if (eventsReceived != 0) {
97             averageRoundTripNano = accumulatedRoundTripTime / eventsReceived;
98             averageApexExecutionNano = accumulatedApexExecutionTime / eventsReceived;
99         }
100     }
101
102     /**
103      * Compile the statistics.
104      * @param eventBatchStatsList the event batch list for which statistics should be calculated
105      */
106     private void calcutateStats(final List<EventBatchStats> eventBatchStatsList) {
107         long accumulatedRoundTripTime = 0;
108         long accumulatedApexExecutionTime = 0;
109
110         for (EventBatchStats eventBatchStats: eventBatchStatsList) {
111             // @formatter:off
112             eventsNotSent     += eventBatchStats.getEventsNotSent();
113             eventsSent        += eventBatchStats.getEventsSent();
114             eventsNotReceived += eventBatchStats.getEventsNotReceived();
115             eventsReceived    += eventBatchStats.getEventsReceived();
116             // @formatter:on
117
118             if (shortestRoundTripNano > eventBatchStats.getShortestRoundTripNano()) {
119                 shortestRoundTripNano = eventBatchStats.getShortestRoundTripNano();
120             }
121
122             if (shortestApexExecutionNano > eventBatchStats.getShortestApexExecutionNano()) {
123                 shortestApexExecutionNano = eventBatchStats.getShortestApexExecutionNano();
124             }
125
126             if (longestRoundTripNano < eventBatchStats.getLongestRoundTripNano()) {
127                 longestRoundTripNano = eventBatchStats.getLongestRoundTripNano();
128             }
129
130             if (longestApexExecutionNano < eventBatchStats.getLongestApexExecutionNano()) {
131                 longestApexExecutionNano = eventBatchStats.getLongestApexExecutionNano();
132             }
133
134             accumulatedRoundTripTime += eventBatchStats.getAverageRoundTripNano();
135             accumulatedApexExecutionTime += eventBatchStats.getAverageApexExecutionNano();
136         }
137
138         if (!eventBatchStatsList.isEmpty()) {
139             averageRoundTripNano = accumulatedRoundTripTime / eventBatchStatsList.size();
140             averageApexExecutionNano = accumulatedApexExecutionTime / eventBatchStatsList.size();
141         }
142     }
143
144     /**
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
149      */
150     private Pair<Long, Long> calculateEventTimings(EventBatch eventBatch, int eventNo) {
151         // If an event is in a batch, it has been sent
152         eventsSent++;
153
154         OutputEvent outputEvent = eventBatch.getOutputEvent(eventNo);
155
156         if (outputEvent == null) {
157             eventsNotReceived++;
158             return null;
159
160         } else {
161             eventsReceived++;
162         }
163
164         long roundTrip = outputEvent.getTestReceviedTimestamp() - outputEvent.getTestTimestamp();
165         long apexExecution = outputEvent.getTestActStateTime() - outputEvent.getTestMatchStateTime();
166
167
168         if (shortestRoundTripNano > roundTrip) {
169             shortestRoundTripNano = roundTrip;
170         }
171
172         if (shortestApexExecutionNano > apexExecution) {
173             shortestApexExecutionNano = apexExecution;
174         }
175
176         if (longestRoundTripNano < roundTrip) {
177             longestRoundTripNano = roundTrip;
178         }
179
180         if (longestApexExecutionNano < apexExecution) {
181             longestApexExecutionNano = apexExecution;
182         }
183
184         return new ImmutablePair<>(roundTrip, apexExecution);
185     }
186
187     public int getBatchNumber() {
188         return batchNumber;
189     }
190
191     public int getBatchSize() {
192         return batchSize;
193     }
194
195     public String getApexClient() {
196         return apexClient;
197     }
198
199     public long getEventsNotSent() {
200         return eventsNotSent;
201     }
202
203     public long getEventsSent() {
204         return eventsSent;
205     }
206
207     public long getEventsNotReceived() {
208         return eventsNotReceived;
209     }
210
211     public long getEventsReceived() {
212         return eventsReceived;
213     }
214
215     public long getAverageRoundTripNano() {
216         return averageRoundTripNano;
217     }
218
219     public long getShortestRoundTripNano() {
220         return shortestRoundTripNano;
221     }
222
223     public long getLongestRoundTripNano() {
224         return longestRoundTripNano;
225     }
226
227     public long getAverageApexExecutionNano() {
228         return averageApexExecutionNano;
229     }
230
231     public long getShortestApexExecutionNano() {
232         return shortestApexExecutionNano;
233     }
234
235     public long getLongestApexExecutionNano() {
236         return longestApexExecutionNano;
237     }
238 }