fa5083ed05ce1bc40834ed61060c5568b39e81eb
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
24
25 import java.util.List;
26 import lombok.Getter;
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;
30
31 /**
32  * This POJO class returns statistics on a event batch execution in Apex.
33  */
34 @Getter
35 public class EventBatchStats {
36     private final int batchNumber;
37     private final int batchSize;
38     private final String apexClient;
39
40     // @formatter:off
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;
51     // @formatter:on
52
53     /**
54      * Create a statistics object for an event batch.
55      *
56      * @param eventBatch the event batch for these statistics
57      */
58     public EventBatchStats(final EventBatch eventBatch) {
59         this.batchNumber = eventBatch.getBatchNumber();
60         this.batchSize = eventBatch.getBatchSize();
61         this.apexClient = eventBatch.getApexClient();
62
63         calcutateStats(eventBatch);
64     }
65
66     /**
67      * Create a total statistics object for a list of event batches.
68      *
69      * @param eventBatchStatsList the event batch for these statistics
70      */
71     public EventBatchStats(final List<EventBatchStats> eventBatchStatsList) {
72         this.batchNumber = -1;
73         this.apexClient = "TOTAL";
74
75         calcutateStats(eventBatchStatsList);
76
77         this.batchSize = (int) (eventsNotSent + eventsSent);
78     }
79
80     /**
81      * Compile the statistics.
82      * @param eventBatch the event batch for which statisticss should be calculated
83      */
84     private void calcutateStats(final EventBatch eventBatch) {
85         long accumulatedRoundTripTime = 0;
86         long accumulatedApexExecutionTime = 0;
87
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
92                 continue;
93             }
94
95             accumulatedRoundTripTime += eventTimings.getLeft();
96             accumulatedApexExecutionTime += eventTimings.getRight();
97         }
98
99         if (eventsReceived != 0) {
100             averageRoundTripNano = accumulatedRoundTripTime / eventsReceived;
101             averageApexExecutionNano = accumulatedApexExecutionTime / eventsReceived;
102         }
103     }
104
105     /**
106      * Compile the statistics.
107      * @param eventBatchStatsList the event batch list for which statistics should be calculated
108      */
109     private void calcutateStats(final List<EventBatchStats> eventBatchStatsList) {
110         long accumulatedRoundTripTime = 0;
111         long accumulatedApexExecutionTime = 0;
112
113         for (EventBatchStats eventBatchStats: eventBatchStatsList) {
114             // @formatter:off
115             eventsNotSent     += eventBatchStats.getEventsNotSent();
116             eventsSent        += eventBatchStats.getEventsSent();
117             eventsNotReceived += eventBatchStats.getEventsNotReceived();
118             eventsReceived    += eventBatchStats.getEventsReceived();
119             // @formatter:on
120
121             if (shortestRoundTripNano > eventBatchStats.getShortestRoundTripNano()) {
122                 shortestRoundTripNano = eventBatchStats.getShortestRoundTripNano();
123             }
124
125             if (shortestApexExecutionNano > eventBatchStats.getShortestApexExecutionNano()) {
126                 shortestApexExecutionNano = eventBatchStats.getShortestApexExecutionNano();
127             }
128
129             if (longestRoundTripNano < eventBatchStats.getLongestRoundTripNano()) {
130                 longestRoundTripNano = eventBatchStats.getLongestRoundTripNano();
131             }
132
133             if (longestApexExecutionNano < eventBatchStats.getLongestApexExecutionNano()) {
134                 longestApexExecutionNano = eventBatchStats.getLongestApexExecutionNano();
135             }
136
137             accumulatedRoundTripTime += eventBatchStats.getAverageRoundTripNano();
138             accumulatedApexExecutionTime += eventBatchStats.getAverageApexExecutionNano();
139         }
140
141         if (!eventBatchStatsList.isEmpty()) {
142             averageRoundTripNano = accumulatedRoundTripTime / eventBatchStatsList.size();
143             averageApexExecutionNano = accumulatedApexExecutionTime / eventBatchStatsList.size();
144         }
145     }
146
147     /**
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
152      */
153     private Pair<Long, Long> calculateEventTimings(EventBatch eventBatch, int eventNo) {
154         // If an event is in a batch, it has been sent
155         eventsSent++;
156
157         OutputEvent outputEvent = eventBatch.getOutputEvent(eventNo);
158
159         if (outputEvent == null) {
160             eventsNotReceived++;
161             return null;
162
163         } else {
164             eventsReceived++;
165         }
166
167         long roundTrip = outputEvent.getTestReceviedTimestamp() - outputEvent.getTestTimestamp();
168         long apexExecution = outputEvent.getTestActStateTime() - outputEvent.getTestMatchStateTime();
169
170
171         if (shortestRoundTripNano > roundTrip) {
172             shortestRoundTripNano = roundTrip;
173         }
174
175         if (shortestApexExecutionNano > apexExecution) {
176             shortestApexExecutionNano = apexExecution;
177         }
178
179         if (longestRoundTripNano < roundTrip) {
180             longestRoundTripNano = roundTrip;
181         }
182
183         if (longestApexExecutionNano < apexExecution) {
184             longestApexExecutionNano = apexExecution;
185         }
186
187         return new ImmutablePair<>(roundTrip, apexExecution);
188     }
189 }