60732972abefcdabd8a37e6b0bf9ef900a6ab091
[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         }
161         else {
162             eventsReceived++;
163         }
164
165         long roundTrip = outputEvent.getTestReceviedTimestamp() - outputEvent.getTestTimestamp();
166         long apexExecution = outputEvent.getTestActStateTime() - outputEvent.getTestMatchStateTime();
167
168
169         if (shortestRoundTripNano > roundTrip) {
170             shortestRoundTripNano = roundTrip;
171         }
172
173         if (shortestApexExecutionNano > apexExecution) {
174             shortestApexExecutionNano = apexExecution;
175         }
176
177         if (longestRoundTripNano < roundTrip) {
178             longestRoundTripNano = roundTrip;
179         }
180
181         if (longestApexExecutionNano < apexExecution) {
182             longestApexExecutionNano = apexExecution;
183         }
184
185         return new ImmutablePair<>(roundTrip, apexExecution);
186     }
187
188     public int getBatchNumber() {
189         return batchNumber;
190     }
191
192     public int getBatchSize() {
193         return batchSize;
194     }
195
196     public String getApexClient() {
197         return apexClient;
198     }
199
200     public long getEventsNotSent() {
201         return eventsNotSent;
202     }
203
204     public long getEventsSent() {
205         return eventsSent;
206     }
207
208     public long getEventsNotReceived() {
209         return eventsNotReceived;
210     }
211
212     public long getEventsReceived() {
213         return eventsReceived;
214     }
215
216     public long getAverageRoundTripNano() {
217         return averageRoundTripNano;
218     }
219
220     public long getShortestRoundTripNano() {
221         return shortestRoundTripNano;
222     }
223
224     public long getLongestRoundTripNano() {
225         return longestRoundTripNano;
226     }
227
228     public long getAverageApexExecutionNano() {
229         return averageApexExecutionNano;
230     }
231
232     public long getShortestApexExecutionNano() {
233         return shortestApexExecutionNano;
234     }
235
236     public long getLongestApexExecutionNano() {
237         return longestApexExecutionNano;
238     }
239 }