Skip to content

Commit 6c72aca

Browse files
JonasKunzKubik42
authored andcommitted
Share testing random exponential histogram generation across modules (elastic#136590)
1 parent 36cfc66 commit 6c72aca

File tree

4 files changed

+70
-32
lines changed

4 files changed

+70
-32
lines changed

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramEqualityTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public ExponentialHistogramEqualityTests(Modification modification) {
5959
}
6060

6161
public void testEquality() {
62-
ExponentialHistogram histo = randomHistogram();
62+
ReleasableExponentialHistogram histo = ExponentialHistogramTestUtils.randomHistogram(breaker());
63+
autoReleaseOnTestEnd(histo);
6364
ReleasableExponentialHistogram copy = ExponentialHistogram.builder(histo, breaker()).build();
6465
autoReleaseOnTestEnd(copy);
6566
ExponentialHistogram modifiedCopy = copyWithModification(histo, modification);
@@ -70,7 +71,8 @@ public void testEquality() {
7071
}
7172

7273
public void testHashQuality() {
73-
ExponentialHistogram histo = randomHistogram();
74+
ReleasableExponentialHistogram histo = ExponentialHistogramTestUtils.randomHistogram(breaker());
75+
autoReleaseOnTestEnd(histo);
7476
// of 10 tries, at least one should produce a different hash code
7577
for (int i = 0; i < 10; i++) {
7678
ExponentialHistogram modifiedCopy = copyWithModification(histo, modification);
@@ -81,10 +83,6 @@ public void testHashQuality() {
8183
fail("Could not produce different hash code after 10 tries");
8284
}
8385

84-
private ExponentialHistogram randomHistogram() {
85-
return createAutoReleasedHistogram(randomIntBetween(4, 20), randomDoubles(randomIntBetween(0, 200)).toArray());
86-
}
87-
8886
private ExponentialHistogram copyWithModification(ExponentialHistogram toCopy, Modification modification) {
8987
ExponentialHistogramBuilder copyBuilder = ExponentialHistogram.builder(toCopy, breaker());
9088
switch (modification) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}

x-pack/plugin/mapper-exponential-histogram/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ restResources {
2626
dependencies {
2727
api project(":libs:exponential-histogram")
2828
compileOnly project(path: xpackModule('core'))
29+
testImplementation(testArtifact(project(":libs:exponential-histogram")))
2930
yamlRestTestImplementation(testArtifact(project(xpackModule('core'))))
3031
}
3132

x-pack/plugin/mapper-exponential-histogram/src/test/java/org/elasticsearch/xpack/exponentialhistogram/aggregations/metrics/ExponentialHistogramAggregatorTestCase.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import org.apache.lucene.index.IndexableField;
1111
import org.apache.lucene.tests.index.RandomIndexWriter;
1212
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
13-
import org.elasticsearch.exponentialhistogram.ExponentialHistogramBuilder;
14-
import org.elasticsearch.exponentialhistogram.ExponentialHistogramCircuitBreaker;
13+
import org.elasticsearch.exponentialhistogram.ExponentialHistogramTestUtils;
1514
import org.elasticsearch.plugins.SearchPlugin;
1615
import org.elasticsearch.search.aggregations.AggregatorTestCase;
1716
import org.elasticsearch.xpack.exponentialhistogram.ExponentialHistogramFieldMapper;
@@ -32,30 +31,7 @@ protected List<SearchPlugin> getSearchPlugins() {
3231
}
3332

3433
protected static List<ExponentialHistogram> createRandomHistograms(int count) {
35-
return IntStream.range(0, count).mapToObj(i -> {
36-
boolean hasNegativeValues = randomBoolean();
37-
boolean hasPositiveValues = randomBoolean();
38-
boolean hasZeroValues = randomBoolean();
39-
double[] rawValues = IntStream.concat(
40-
IntStream.concat(
41-
hasNegativeValues ? IntStream.range(0, randomIntBetween(1, 1000)).map(i1 -> -1) : IntStream.empty(),
42-
hasPositiveValues ? IntStream.range(0, randomIntBetween(1, 1000)).map(i1 -> 1) : IntStream.empty()
43-
),
44-
hasZeroValues ? IntStream.range(0, randomIntBetween(1, 100)).map(i1 -> 0) : IntStream.empty()
45-
).mapToDouble(sign -> sign * (Math.pow(1_000_000, randomDouble()))).toArray();
46-
47-
int numBuckets = randomIntBetween(4, 300);
48-
ExponentialHistogram histo = ExponentialHistogram.create(numBuckets, ExponentialHistogramCircuitBreaker.noop(), rawValues);
49-
// Randomize sum, min and max a little
50-
if (histo.valueCount() > 0) {
51-
ExponentialHistogramBuilder builder = ExponentialHistogram.builder(histo, ExponentialHistogramCircuitBreaker.noop());
52-
builder.sum(histo.sum() + (randomDouble() - 0.5) * 10_000);
53-
builder.max(histo.max() - randomDouble());
54-
builder.min(histo.min() + randomDouble());
55-
histo = builder.build();
56-
}
57-
return histo;
58-
}).toList();
34+
return IntStream.range(0, count).mapToObj(i -> ExponentialHistogramTestUtils.randomHistogram()).toList();
5935
}
6036

6137
protected static void addHistogramDoc(

0 commit comments

Comments
 (0)