2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2022 Bell Canada.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.enginemodel.concepts;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
32 import io.prometheus.client.CollectorRegistry;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
40 * Test the engine statistics.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class EngineStatsTest {
45 private static final Object WAIT_LOCK = new Object();
46 private static final String ENGINE_KEY = "EngineKey";
47 private static final String ENGINE_VERSION = "0.0.1";
50 public void testEngineStats() {
51 assertNotNull(new AxEngineStats());
52 assertNotNull(new AxEngineStats(new AxReferenceKey()));
54 final AxReferenceKey statsKey = new AxReferenceKey(ENGINE_KEY, ENGINE_VERSION, "EngineStats");
55 final AxEngineStats stats = new AxEngineStats(statsKey);
57 assertThatThrownBy(() -> stats.setKey(null))
58 .hasMessage("key may not be null");
59 stats.setKey(statsKey);
60 assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKey().getId());
61 assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKeys().get(0).getId());
63 stats.setAverageExecutionTime(123.45);
64 assertEquals(Double.valueOf(123.45), Double.valueOf(stats.getAverageExecutionTime()));
65 checkAvgExecTimeMetric(stats);
67 stats.setEventCount(987);
68 assertEquals(987, stats.getEventCount());
69 checkEventsCountMetric(stats);
71 final long lastExecutionTime = System.currentTimeMillis();
72 stats.setLastExecutionTime(lastExecutionTime);
73 assertEquals(lastExecutionTime, stats.getLastExecutionTime());
74 checkLastExecTimeMetric(stats);
76 final long timestamp = System.currentTimeMillis();
77 stats.setTimeStamp(timestamp);
78 assertEquals(timestamp, stats.getTimeStamp());
79 assertNotNull(stats.getTimeStampString());
81 final long upTime = System.currentTimeMillis() - timestamp;
82 stats.setUpTime(upTime);
83 assertEquals(upTime, stats.getUpTime());
84 checkUpTimeMetric(stats);
87 assertTrue(stats.getUpTime() > -1);
88 checkEngineStartTimestampMetric(stats);
89 checkLastExecTimeMetric(stats);
91 assertTrue(stats.getUpTime() >= 0);
94 checkUpTimeMetric(stats);
95 checkEngineStartTimestampMetric(stats);
99 stats.setEventCount(-2);
100 stats.executionEnter(new AxArtifactKey());
101 assertEquals(2, stats.getEventCount());
102 checkEventsCountMetric(stats);
104 stats.setEventCount(10);
105 stats.executionEnter(new AxArtifactKey());
106 assertEquals(11, stats.getEventCount());
107 checkEventsCountMetric(stats);
111 stats.setEventCount(4);
112 checkUpTimeMetric(stats);
113 stats.executionEnter(new AxArtifactKey());
114 checkEventsCountMetric(stats);
115 checkAvgExecTimeMetric(stats);
116 checkEngineStartTimestampMetric(stats);
118 synchronized (WAIT_LOCK) {
121 } catch (InterruptedException e) {
122 fail("test should not throw an exception");
126 stats.executionExit();
127 final double avExecutionTime = stats.getAverageExecutionTime();
128 assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
130 checkUpTimeMetric(stats);
132 AxValidationResult result = new AxValidationResult();
133 result = stats.validate(result);
134 assertEquals(ValidationResult.VALID, result.getValidationResult());
136 stats.setKey(new AxReferenceKey());
137 result = new AxValidationResult();
138 result = stats.validate(result);
139 assertEquals(ValidationResult.INVALID, result.getValidationResult());
141 stats.setKey(statsKey);
142 result = new AxValidationResult();
143 result = stats.validate(result);
144 assertEquals(ValidationResult.VALID, result.getValidationResult());
148 checkAllPrometheusMetrics(stats);
150 final AxEngineStats clonedStats = new AxEngineStats(stats);
151 assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
153 assertNotNull(stats.getKeys());
155 assertNotEquals(0, stats.hashCode());
157 // disabling sonar because this code tests the equals() method
158 assertEquals(stats, stats); // NOSONAR
159 assertEquals(stats, clonedStats);
160 assertNotNull(stats);
161 checkAllPrometheusMetrics(clonedStats);
163 Object helloObject = "Hello";
164 assertNotEquals(stats, helloObject);
165 assertNotEquals(stats, new AxEngineStats(new AxReferenceKey()));
167 assertEquals(0, stats.compareTo(stats));
168 assertEquals(0, stats.compareTo(clonedStats));
169 assertNotEquals(0, stats.compareTo(new AxArtifactKey()));
170 assertNotEquals(0, stats.compareTo(null));
171 assertNotEquals(0, stats.compareTo(new AxEngineStats(new AxReferenceKey())));
173 stats.setTimeStamp(1);
174 assertNotEquals(stats, new AxEngineStats(statsKey));
175 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
176 stats.setTimeStamp(0);
177 assertEquals(stats, new AxEngineStats(statsKey));
178 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
179 checkAllPrometheusMetrics(clonedStats);
181 stats.setEventCount(1);
182 assertNotEquals(stats, new AxEngineStats(statsKey));
183 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
184 stats.setEventCount(0);
185 assertEquals(stats, new AxEngineStats(statsKey));
186 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
188 stats.setLastExecutionTime(1);
189 assertNotEquals(stats, new AxEngineStats(statsKey));
190 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
191 stats.setLastExecutionTime(0);
192 assertEquals(stats, new AxEngineStats(statsKey));
193 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
195 stats.setAverageExecutionTime(1);
196 assertNotEquals(stats, new AxEngineStats(statsKey));
197 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
198 stats.setAverageExecutionTime(0);
199 assertEquals(stats, new AxEngineStats(statsKey));
200 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
203 assertNotEquals(stats, new AxEngineStats(statsKey));
204 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
206 assertEquals(stats, new AxEngineStats(statsKey));
207 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
209 assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
212 assertNotEquals(stats, new AxEngineStats(statsKey));
213 final AxEngineStats newStats = new AxEngineStats(statsKey);
214 newStats.setTimeStamp(stats.getTimeStamp());
215 assertNotEquals(stats, newStats);
216 assertNotEquals(0, stats.compareTo(newStats));
218 checkUpTimeMetric(stats);
219 checkEngineStartTimestampMetric(stats);
221 assertEquals(stats, new AxEngineStats(statsKey));
222 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
223 checkAllPrometheusMetrics(stats);
226 private void checkUpTimeMetric(AxEngineStats stats) {
227 Double upTimeMetric = CollectorRegistry.defaultRegistry.getSampleValue("apex_engine_uptime",
228 new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
229 new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
230 assertEquals(upTimeMetric.longValue(), stats.getUpTime());
233 private void checkEventsCountMetric(AxEngineStats stats) {
234 Double eventsCountMetric = CollectorRegistry.defaultRegistry
235 .getSampleValue("apex_engine_events_executed_count",
236 new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
237 new String[]{ENGINE_KEY + ":" + ENGINE_VERSION});
238 assertEquals(eventsCountMetric.longValue(), stats.getEventCount());
241 private void checkLastExecTimeMetric(AxEngineStats stats) {
242 Double lastExecTimeMetric = CollectorRegistry.defaultRegistry
243 .getSampleValue("apex_engine_last_execution_time_sum", new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
244 new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
245 assertEquals(lastExecTimeMetric.longValue(), stats.getLastExecutionTime());
248 private void checkEngineStartTimestampMetric(AxEngineStats stats) {
249 Double engineStartTimestampMetric = CollectorRegistry.defaultRegistry
250 .getSampleValue("apex_engine_last_start_timestamp_epoch",
251 new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
252 new String[]{ENGINE_KEY + ":" + ENGINE_VERSION});
253 assertEquals(engineStartTimestampMetric.longValue(), stats.getLastStart());
256 private void checkAvgExecTimeMetric(AxEngineStats stats) {
257 Double avgExecTimeMetric = CollectorRegistry.defaultRegistry
258 .getSampleValue("apex_engine_average_execution_time_seconds",
259 new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
260 new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
261 assertEquals(avgExecTimeMetric, Double.valueOf(stats.getAverageExecutionTime()));
264 private void checkAllPrometheusMetrics(AxEngineStats stats) {
265 checkEventsCountMetric(stats);
266 checkUpTimeMetric(stats);
267 checkAvgExecTimeMetric(stats);
268 checkEngineStartTimestampMetric(stats);
269 checkEngineStartTimestampMetric(stats);