|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V., and/or licensed to Elasticsearch B.V. |
| 3 | + * under one or more license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + * This file is based on a modification of https://github.com/open-telemetry/opentelemetry-java which is licensed under the Apache 2.0 License. |
| 20 | + */ |
| 21 | + |
| 22 | +package org.elasticsearch.exponentialhistogram; |
| 23 | + |
| 24 | +import java.util.stream.DoubleStream; |
| 25 | +import java.util.stream.IntStream; |
| 26 | + |
| 27 | +import static org.elasticsearch.test.ESTestCase.randomBoolean; |
| 28 | +import static org.elasticsearch.test.ESTestCase.randomDouble; |
| 29 | +import static org.elasticsearch.test.ESTestCase.randomIntBetween; |
| 30 | + |
| 31 | +public abstract class ExponentialHistogramTestUtils { |
| 32 | + |
| 33 | + public static ExponentialHistogram randomHistogram() { |
| 34 | + return randomHistogram(ExponentialHistogramCircuitBreaker.noop()); |
| 35 | + } |
| 36 | + |
| 37 | + public static ReleasableExponentialHistogram randomHistogram(ExponentialHistogramCircuitBreaker breaker) { |
| 38 | + boolean hasNegativeValues = randomBoolean(); |
| 39 | + boolean hasPositiveValues = randomBoolean(); |
| 40 | + boolean hasZeroValues = randomBoolean(); |
| 41 | + double[] rawValues = IntStream.concat( |
| 42 | + IntStream.concat( |
| 43 | + hasNegativeValues ? IntStream.range(0, randomIntBetween(1, 1000)).map(i1 -> -1) : IntStream.empty(), |
| 44 | + hasPositiveValues ? IntStream.range(0, randomIntBetween(1, 1000)).map(i1 -> 1) : IntStream.empty() |
| 45 | + ), |
| 46 | + hasZeroValues ? IntStream.range(0, randomIntBetween(1, 100)).map(i1 -> 0) : IntStream.empty() |
| 47 | + ).mapToDouble(sign -> sign * (Math.pow(1_000_000, randomDouble()))).toArray(); |
| 48 | + |
| 49 | + int numBuckets = randomIntBetween(4, 300); |
| 50 | + ReleasableExponentialHistogram histo = ExponentialHistogram.create(numBuckets, breaker, rawValues); |
| 51 | + // Setup a proper zeroThreshold based on a random chance |
| 52 | + if (histo.zeroBucket().count() > 0 && randomBoolean()) { |
| 53 | + double smallestNonZeroValue = DoubleStream.of(rawValues).map(Math::abs).filter(val -> val != 0).min().orElse(0.0); |
| 54 | + double zeroThreshold = smallestNonZeroValue * randomDouble(); |
| 55 | + try (ReleasableExponentialHistogram releaseAfterCopy = histo) { |
| 56 | + histo = ExponentialHistogram.builder(histo, breaker) |
| 57 | + .zeroBucket(ZeroBucket.create(zeroThreshold, histo.zeroBucket().count())) |
| 58 | + .build(); |
| 59 | + } |
| 60 | + } |
| 61 | + return histo; |
| 62 | + } |
| 63 | +} |
0 commit comments