|
| 1 | +/* |
| 2 | + * Copyright 2015 Red Hat, Inc. and/or its affiliates |
| 3 | + * and other contributors as indicated by the @author tags. |
| 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 | +package org.hawkular.client.test.metrics.openshift; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +import org.apache.commons.lang3.ArrayUtils; |
| 27 | +import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; |
| 28 | +import org.hawkular.client.metrics.model.GaugeDataPoint; |
| 29 | +import org.testng.Assert; |
| 30 | +import org.testng.Reporter; |
| 31 | +import org.testng.annotations.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + *Find value=0 datapoints |
| 35 | + * @author vnguyen |
| 36 | + */ |
| 37 | +@Test(groups={"openshift"}) |
| 38 | +public class CollectionRateDetailTest extends OpenshiftBaseTest { |
| 39 | + |
| 40 | + public CollectionRateDetailTest() throws Exception { |
| 41 | + super(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void findZeroValuesTest() throws Exception { |
| 46 | + String project = "zproject9"; |
| 47 | + String container = "stress"; |
| 48 | + String testID = "namespace: " + project + ", container: " + container; |
| 49 | + |
| 50 | + // for easy identification in the log |
| 51 | + String metricID = super.getMetricID(project, container, METRIC_SUFFIX.CPU_USAGE); |
| 52 | + |
| 53 | + // Get a lot of data in the last few days |
| 54 | + long now = Instant.now().toEpochMilli(); |
| 55 | + long start = now - Duration.ofHours(36).toMillis(); |
| 56 | + long end = start + Duration.ofHours(36).toMillis(); |
| 57 | + |
| 58 | + Duration timeBucket = Duration.ofHours(1); |
| 59 | + getData(metricID, testID, start, end, timeBucket); |
| 60 | + |
| 61 | + // get data in the last hour |
| 62 | + start = now - Duration.ofHours(1).toMillis(); |
| 63 | + end = now; |
| 64 | + timeBucket = Duration.ofMinutes(1); |
| 65 | + getData(metricID, testID, start, end, timeBucket); |
| 66 | + } |
| 67 | + |
| 68 | + private void getData(String metricID, String testID, long start, long end, Duration timeBucket) { |
| 69 | + Reporter.log("Fetching large data set... may take a couple minutes", true); |
| 70 | + List<GaugeDataPoint> rawData = client().metrics().getGaugeData(TENANT_ID, metricID, start, end); |
| 71 | + |
| 72 | + Assert.assertNotNull(rawData, testID); |
| 73 | + Reporter.log("raw datapoints: " + rawData.size(), true); |
| 74 | + |
| 75 | + List<Long> zeroList = findZeroValues(rawData); |
| 76 | + |
| 77 | + Assert.assertTrue(zeroList == null || zeroList.size() == 0, testID); |
| 78 | + |
| 79 | + Map<Long, Integer> hist = OpenshiftBaseTest.makeHistogram(rawData, timeBucket); |
| 80 | + |
| 81 | + Double[] result = hist.entrySet().stream() |
| 82 | + .map(x -> new Double(x.getValue())) |
| 83 | + .toArray(size -> new Double[size]); |
| 84 | + |
| 85 | + double[] d = ArrayUtils.toPrimitive(result); |
| 86 | + |
| 87 | + // drop the first and last as they are usually outliers |
| 88 | + double samples[] = Arrays.copyOfRange(d,1, d.length-1); |
| 89 | + DescriptiveStatistics stats = new DescriptiveStatistics(samples); |
| 90 | + |
| 91 | + Reporter.log(hist.toString(), true); |
| 92 | + Reporter.log("size: " + stats.getN(), true); |
| 93 | + Reporter.log("min/max: " + stats.getMin() + "/" + stats.getMax(), true); |
| 94 | + Reporter.log("mean: " + stats.getMean(), true); |
| 95 | + Reporter.log("variance: " + stats.getVariance(), true); |
| 96 | + Reporter.log("stddev: " + stats.getStandardDeviation(), true); |
| 97 | + } |
| 98 | + |
| 99 | + private List<Long> findZeroValues(List<GaugeDataPoint> rawData) { |
| 100 | + List<Long> zeroList = rawData |
| 101 | + .stream() |
| 102 | + .filter(p -> !(p.getValue() != 0)) |
| 103 | + .map(p -> p.getTimestamp()) |
| 104 | + .collect(Collectors.toList()); |
| 105 | + return zeroList; |
| 106 | + } |
| 107 | +} |
0 commit comments