aboutsummaryrefslogtreecommitdiffstats
path: root/testsuites/performance/performance-benchmark-test/src/main/java/org/onap/policy/apex/testsuites/performance/benchmark/eventgenerator/EventBatchStats.java
blob: fa5083ed05ce1bc40834ed61060c5568b39e81eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;

import java.util.List;
import lombok.Getter;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;

/**
 * This POJO class returns statistics on a event batch execution in Apex.
 */
@Getter
public class EventBatchStats {
    private final int batchNumber;
    private final int batchSize;
    private final String apexClient;

    // @formatter:off
    private long eventsNotSent             = 0;
    private long eventsSent                = 0;
    private long eventsNotReceived         = 0;
    private long eventsReceived            = 0;
    private long averageRoundTripNano      = 0;
    private long shortestRoundTripNano     = Long.MAX_VALUE;
    private long longestRoundTripNano      = 0;
    private long averageApexExecutionNano  = 0;
    private long shortestApexExecutionNano = Long.MAX_VALUE;
    private long longestApexExecutionNano  = 0;
    // @formatter:on

    /**
     * Create a statistics object for an event batch.
     *
     * @param eventBatch the event batch for these statistics
     */
    public EventBatchStats(final EventBatch eventBatch) {
        this.batchNumber = eventBatch.getBatchNumber();
        this.batchSize = eventBatch.getBatchSize();
        this.apexClient = eventBatch.getApexClient();

        calcutateStats(eventBatch);
    }

    /**
     * Create a total statistics object for a list of event batches.
     *
     * @param eventBatchStatsList the event batch for these statistics
     */
    public EventBatchStats(final List<EventBatchStats> eventBatchStatsList) {
        this.batchNumber = -1;
        this.apexClient = "TOTAL";

        calcutateStats(eventBatchStatsList);

        this.batchSize = (int) (eventsNotSent + eventsSent);
    }

    /**
     * Compile the statistics.
     * @param eventBatch the event batch for which statisticss should be calculated
     */
    private void calcutateStats(final EventBatch eventBatch) {
        long accumulatedRoundTripTime = 0;
        long accumulatedApexExecutionTime = 0;

        for (int eventNo = 0; eventNo < batchSize; eventNo++) {
            Pair<Long, Long> eventTimings = calculateEventTimings(eventBatch, eventNo);
            if (eventTimings == null) {
                // The event has not been sent yet or the response has not been received yet
                continue;
            }

            accumulatedRoundTripTime += eventTimings.getLeft();
            accumulatedApexExecutionTime += eventTimings.getRight();
        }

        if (eventsReceived != 0) {
            averageRoundTripNano = accumulatedRoundTripTime / eventsReceived;
            averageApexExecutionNano = accumulatedApexExecutionTime / eventsReceived;
        }
    }

    /**
     * Compile the statistics.
     * @param eventBatchStatsList the event batch list for which statistics should be calculated
     */
    private void calcutateStats(final List<EventBatchStats> eventBatchStatsList) {
        long accumulatedRoundTripTime = 0;
        long accumulatedApexExecutionTime = 0;

        for (EventBatchStats eventBatchStats: eventBatchStatsList) {
            // @formatter:off
            eventsNotSent     += eventBatchStats.getEventsNotSent();
            eventsSent        += eventBatchStats.getEventsSent();
            eventsNotReceived += eventBatchStats.getEventsNotReceived();
            eventsReceived    += eventBatchStats.getEventsReceived();
            // @formatter:on

            if (shortestRoundTripNano > eventBatchStats.getShortestRoundTripNano()) {
                shortestRoundTripNano = eventBatchStats.getShortestRoundTripNano();
            }

            if (shortestApexExecutionNano > eventBatchStats.getShortestApexExecutionNano()) {
                shortestApexExecutionNano = eventBatchStats.getShortestApexExecutionNano();
            }

            if (longestRoundTripNano < eventBatchStats.getLongestRoundTripNano()) {
                longestRoundTripNano = eventBatchStats.getLongestRoundTripNano();
            }

            if (longestApexExecutionNano < eventBatchStats.getLongestApexExecutionNano()) {
                longestApexExecutionNano = eventBatchStats.getLongestApexExecutionNano();
            }

            accumulatedRoundTripTime += eventBatchStats.getAverageRoundTripNano();
            accumulatedApexExecutionTime += eventBatchStats.getAverageApexExecutionNano();
        }

        if (!eventBatchStatsList.isEmpty()) {
            averageRoundTripNano = accumulatedRoundTripTime / eventBatchStatsList.size();
            averageApexExecutionNano = accumulatedApexExecutionTime / eventBatchStatsList.size();
        }
    }

    /**
     * Calculate statistics for a single event.
     * @param eventBatch the event batch for the event
     * @param eventNo the event number of the event
     * @return event timings
     */
    private Pair<Long, Long> calculateEventTimings(EventBatch eventBatch, int eventNo) {
        // If an event is in a batch, it has been sent
        eventsSent++;

        OutputEvent outputEvent = eventBatch.getOutputEvent(eventNo);

        if (outputEvent == null) {
            eventsNotReceived++;
            return null;

        } else {
            eventsReceived++;
        }

        long roundTrip = outputEvent.getTestReceviedTimestamp() - outputEvent.getTestTimestamp();
        long apexExecution = outputEvent.getTestActStateTime() - outputEvent.getTestMatchStateTime();


        if (shortestRoundTripNano > roundTrip) {
            shortestRoundTripNano = roundTrip;
        }

        if (shortestApexExecutionNano > apexExecution) {
            shortestApexExecutionNano = apexExecution;
        }

        if (longestRoundTripNano < roundTrip) {
            longestRoundTripNano = roundTrip;
        }

        if (longestApexExecutionNano < apexExecution) {
            longestApexExecutionNano = apexExecution;
        }

        return new ImmutablePair<>(roundTrip, apexExecution);
    }
}