Skip to content

Commit 741c031

Browse files
authored
[Test] Add unit tests for InternalHDRPercentilesTests (#24157)
Related to #22278
1 parent 4f773e2 commit 741c031

File tree

3 files changed

+129
-22
lines changed

3 files changed

+129
-22
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch 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+
20+
package org.elasticsearch.search.aggregations.metrics.percentiles;
21+
22+
import org.elasticsearch.search.DocValueFormat;
23+
import org.elasticsearch.search.aggregations.InternalAggregation;
24+
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
26+
import org.junit.Before;
27+
28+
import java.util.List;
29+
import java.util.Map;
30+
31+
public abstract class InternalPercentilesTestCase<T extends InternalAggregation> extends InternalAggregationTestCase<T> {
32+
33+
private double[] percents;
34+
35+
@Before
36+
public void init() {
37+
percents = randomPercents();
38+
}
39+
40+
@Override
41+
protected T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
42+
int numValues = randomInt(100);
43+
double[] values = new double[numValues];
44+
for (int i = 0; i < numValues; ++i) {
45+
values[i] = randomDouble();
46+
}
47+
return createTestInstance(name, pipelineAggregators, metaData, randomBoolean(), DocValueFormat.RAW, percents, values);
48+
}
49+
50+
protected abstract T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData,
51+
boolean keyed, DocValueFormat format, double[] percents, double[] values);
52+
53+
private static double[] randomPercents() {
54+
List<Double> randomCdfValues = randomSubsetOf(randomIntBetween(1, 7), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d);
55+
double[] percents = new double[randomCdfValues.size()];
56+
for (int i = 0; i < randomCdfValues.size(); i++) {
57+
percents[i] = randomCdfValues.get(i);
58+
}
59+
return percents;
60+
}
61+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch 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+
20+
package org.elasticsearch.search.aggregations.metrics.percentiles.hdr;
21+
22+
import org.HdrHistogram.DoubleHistogram;
23+
import org.elasticsearch.common.io.stream.Writeable;
24+
import org.elasticsearch.search.DocValueFormat;
25+
import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentilesTestCase;
26+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
27+
28+
import java.util.Arrays;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
public class InternalHDRPercentilesTests extends InternalPercentilesTestCase<InternalHDRPercentiles> {
33+
34+
@Override
35+
protected InternalHDRPercentiles createTestInstance(String name,
36+
List<PipelineAggregator> pipelineAggregators,
37+
Map<String, Object> metaData,
38+
boolean keyed, DocValueFormat format, double[] percents, double[] values) {
39+
40+
final DoubleHistogram state = new DoubleHistogram(3);
41+
Arrays.stream(values).forEach(state::recordValue);
42+
43+
return new InternalHDRPercentiles(name, percents, state, keyed, format, pipelineAggregators, metaData);
44+
}
45+
46+
@Override
47+
protected void assertReduced(InternalHDRPercentiles reduced, List<InternalHDRPercentiles> inputs) {
48+
// it is hard to check the values due to the inaccuracy of the algorithm
49+
long totalCount = 0;
50+
for (InternalHDRPercentiles ranks : inputs) {
51+
totalCount += ranks.state.getTotalCount();
52+
}
53+
assertEquals(totalCount, reduced.state.getTotalCount());
54+
}
55+
56+
@Override
57+
protected Writeable.Reader<InternalHDRPercentiles> instanceReader() {
58+
return InternalHDRPercentiles::new;
59+
}
60+
}

core/src/test/java/org/elasticsearch/search/aggregations/metrics/percentiles/tdigest/InternalTDigestPercentilesTests.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,24 @@
2121

2222
import org.elasticsearch.common.io.stream.Writeable;
2323
import org.elasticsearch.search.DocValueFormat;
24-
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
24+
import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentilesTestCase;
2525
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2626

27+
import java.util.Arrays;
2728
import java.util.List;
2829
import java.util.Map;
2930

30-
public class InternalTDigestPercentilesTests extends InternalAggregationTestCase<InternalTDigestPercentiles> {
31-
32-
private final double[] percents = randomPercents();
31+
public class InternalTDigestPercentilesTests extends InternalPercentilesTestCase<InternalTDigestPercentiles> {
3332

3433
@Override
3534
protected InternalTDigestPercentiles createTestInstance(String name,
3635
List<PipelineAggregator> pipelineAggregators,
37-
Map<String, Object> metaData) {
38-
boolean keyed = randomBoolean();
39-
DocValueFormat format = DocValueFormat.RAW;
40-
TDigestState state = new TDigestState(100);
36+
Map<String, Object> metaData,
37+
boolean keyed, DocValueFormat format, double[] percents, double[] values) {
38+
final TDigestState state = new TDigestState(100);
39+
Arrays.stream(values).forEach(state::add);
4140

42-
int numValues = randomInt(10);
43-
for (int i = 0; i < numValues; ++i) {
44-
state.add(randomDouble() * 100);
45-
}
46-
assertEquals(state.centroidCount(), numValues);
41+
assertEquals(state.centroidCount(), values.length);
4742
return new InternalTDigestPercentiles(name, percents, state, keyed, format, pipelineAggregators, metaData);
4843
}
4944

@@ -69,13 +64,4 @@ protected void assertReduced(InternalTDigestPercentiles reduced, List<InternalTD
6964
protected Writeable.Reader<InternalTDigestPercentiles> instanceReader() {
7065
return InternalTDigestPercentiles::new;
7166
}
72-
73-
private static double[] randomPercents() {
74-
List<Double> randomCdfValues = randomSubsetOf(randomIntBetween(1, 7), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d);
75-
double[] percents = new double[randomCdfValues.size()];
76-
for (int i = 0; i < randomCdfValues.size(); i++) {
77-
percents[i] = randomCdfValues.get(i);
78-
}
79-
return percents;
80-
}
8167
}

0 commit comments

Comments
 (0)