abf0c993f2fa426c306f1b00fb2e57b77cbb8900
[policy/apex-pdp.git] / testsuites / performance / performance-benchmark-test / src / main / java / org / onap / policy / apex / testsuites / performance / benchmark / eventgenerator / EventBatch.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator;
22
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.InputEvent;
28 import org.onap.policy.apex.testsuites.performance.benchmark.eventgenerator.events.OutputEvent;
29
30 /**
31  * This class keeps track of a batch of events sent to an Apex instance.
32  */
33 public class EventBatch {
34     private static AtomicInteger nextBatchNumber = new AtomicInteger();
35
36     private final int batchNumber = nextBatchNumber.getAndIncrement();
37     private final Map<Integer, InputEvent>  inputEventMap  = new ConcurrentHashMap<>();
38     private final Map<Integer, OutputEvent> outputEventMap = new ConcurrentHashMap<>();
39
40     private final int batchSize;
41     private final String apexClient;
42
43     /**
44      * Create an event batch.
45      *
46      * @param batchSize the size of the batch
47      * @param apexClient the apex client to which the event batch will be sent
48      */
49     public EventBatch(final int batchSize, final String apexClient) {
50         this.batchSize = batchSize;
51         this.apexClient = apexClient;
52
53         // Create the events for the batch of events
54         for (int eventNumber = 0; eventNumber < batchSize; eventNumber++) {
55             InputEvent inputEvent = new InputEvent();
56             inputEvent.setTestSlogan(getEventSlogan(eventNumber));
57             inputEventMap.put(eventNumber, inputEvent);
58         }
59     }
60
61     /**
62      * Get the batch of events as a JSON string.
63      *
64      * @return the JSON string representation of the batch of events.
65      */
66     public String getBatchAsJsonString() {
67         if (batchSize == 1) {
68             return inputEventMap.get(0).asJson();
69         }
70
71         StringBuilder jsonBuilder = new StringBuilder();
72         jsonBuilder.append("[\n");
73         boolean first = true;
74         for (InputEvent inputEvent : inputEventMap.values()) {
75             if (first) {
76                 first = false;
77             }
78             else {
79                 jsonBuilder.append(",\n");
80             }
81             jsonBuilder.append(inputEvent.asJson());
82         }
83         jsonBuilder.append("\n]\n");
84
85         return jsonBuilder.toString();
86     }
87
88     public int getBatchNumber() {
89         return batchNumber;
90     }
91
92     public int getBatchSize() {
93         return batchSize;
94     }
95
96     public String getApexClient() {
97         return apexClient;
98     }
99
100     /**
101      * Get the event slogan.
102      *
103      * @param eventNumber the number of this event
104      * @return the event slogan
105      */
106     private String getEventSlogan(final int eventNumber) {
107         StringBuilder testSloganBuilder = new StringBuilder();
108         testSloganBuilder.append(batchNumber);
109         testSloganBuilder.append('-');
110         testSloganBuilder.append(eventNumber);
111         testSloganBuilder.append(": ");
112         testSloganBuilder.append(apexClient);
113
114         return testSloganBuilder.toString();
115     }
116
117     /**
118      * Handle a response event.
119      *
120      * @param responseEvent the response event
121      */
122     public void handleResponse(OutputEvent responseEvent) {
123         outputEventMap.put(responseEvent.findEventNumber(), responseEvent);
124     }
125
126     /**
127      * Get the statistics on this event batch.
128      * @return the event batch statistics
129      */
130     public EventBatchStats getStats() {
131         return new EventBatchStats(this);
132     }
133
134     /**
135      * Get an input event for an event number.
136      * @param eventNo the event number
137      * @return the event
138      */
139     public InputEvent getInputEvent(int eventNo) {
140         return inputEventMap.get(eventNo);
141     }
142
143     /**
144      * Get an output event for an event number.
145      * @param eventNo the event number
146      * @return the event
147      */
148     public OutputEvent getOutputEvent(int eventNo) {
149         return outputEventMap.get(eventNo);
150     }
151 }