Skip to content

Commit 66b5e2c

Browse files
committed
Fix tests, implement benchmarks
1 parent fd7064e commit 66b5e2c

File tree

7 files changed

+120
-8
lines changed

7 files changed

+120
-8
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/exponentialhistogram/ExponentialHistogramGenerationBench.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
@State(Scope.Thread)
4444
public class ExponentialHistogramGenerationBench {
4545

46-
@Param({ "100", "500", "1000", "5000" })
46+
@Param({ "100", "500", "1000", "5000" , "10000", "20000"})
4747
int bucketCount;
4848

4949
@Param({ "NORMAL", "GAUSSIAN" })
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.benchmark.exponentialhistogram;
11+
12+
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
13+
import org.elasticsearch.exponentialhistogram.ExponentialHistogramGenerator;
14+
import org.elasticsearch.exponentialhistogram.ExponentialHistogramMerger;
15+
import org.openjdk.jmh.annotations.Benchmark;
16+
import org.openjdk.jmh.annotations.BenchmarkMode;
17+
import org.openjdk.jmh.annotations.Fork;
18+
import org.openjdk.jmh.annotations.Measurement;
19+
import org.openjdk.jmh.annotations.Mode;
20+
import org.openjdk.jmh.annotations.OutputTimeUnit;
21+
import org.openjdk.jmh.annotations.Param;
22+
import org.openjdk.jmh.annotations.Scope;
23+
import org.openjdk.jmh.annotations.Setup;
24+
import org.openjdk.jmh.annotations.State;
25+
import org.openjdk.jmh.annotations.Threads;
26+
import org.openjdk.jmh.annotations.Warmup;
27+
28+
import java.util.List;
29+
import java.util.Random;
30+
import java.util.concurrent.ThreadLocalRandom;
31+
import java.util.concurrent.TimeUnit;
32+
33+
@BenchmarkMode(Mode.AverageTime)
34+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
35+
@Warmup(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS)
36+
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
37+
@Fork(1)
38+
@Threads(1)
39+
@State(Scope.Thread)
40+
public class ExponentialHistogramMergeBench {
41+
42+
@Param({ "1000", "5000" })
43+
int bucketCount;
44+
45+
@Param({ "0.01", "0.1", "0.25", "0.5", "1.0", "2.0" })
46+
double mergedHistoSizeFactor;
47+
48+
Random random;
49+
ExponentialHistogramMerger histoMerger;
50+
51+
ExponentialHistogram[] toMerge = new ExponentialHistogram[10_000];
52+
53+
@Setup
54+
public void setUp() {
55+
random = ThreadLocalRandom.current();
56+
histoMerger = new ExponentialHistogramMerger(bucketCount);
57+
58+
ExponentialHistogramGenerator initial = new ExponentialHistogramGenerator(bucketCount);
59+
for (int j = 0; j < bucketCount; j++) {
60+
initial.add(Math.pow(1.001, j));
61+
}
62+
ExponentialHistogram initialHisto = initial.get();
63+
int cnt = getBucketCount(initialHisto);
64+
if (cnt < bucketCount) {
65+
throw new IllegalArgumentException("Expected bucket count to be " + bucketCount + ", but was " + cnt);
66+
}
67+
histoMerger.add(initialHisto);
68+
69+
int dataPointSize = (int) Math.round(bucketCount * mergedHistoSizeFactor);
70+
71+
for (int i = 0; i < toMerge.length; i++) {
72+
ExponentialHistogramGenerator generator = new ExponentialHistogramGenerator(dataPointSize);
73+
74+
int bucketIndex = 0;
75+
for (int j = 0; j < dataPointSize; j++) {
76+
bucketIndex += 1 + random.nextInt(bucketCount) % (Math.max(1, bucketCount / dataPointSize));
77+
generator.add(Math.pow(1.001, bucketIndex));
78+
}
79+
toMerge[i] = generator.get();
80+
cnt = getBucketCount(toMerge[i]);
81+
if (cnt < dataPointSize) {
82+
throw new IllegalArgumentException("Expected bucket count to be " + dataPointSize + ", but was " + cnt);
83+
}
84+
}
85+
}
86+
87+
private static int getBucketCount(ExponentialHistogram histo) {
88+
int cnt = 0;
89+
for (ExponentialHistogram.BucketIterator it : List.of(histo.negativeBuckets(), histo.positiveBuckets())) {
90+
while (it.hasNext()) {
91+
cnt++;
92+
it.advance();
93+
}
94+
}
95+
return cnt;
96+
}
97+
98+
@State(Scope.Thread)
99+
public static class ThreadState {
100+
int index = 0;
101+
}
102+
103+
@Benchmark
104+
@BenchmarkMode(Mode.AverageTime)
105+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
106+
public void add(ThreadState state) {
107+
if (state.index >= toMerge.length) {
108+
state.index = 0;
109+
}
110+
histoMerger.add(toMerge[state.index++]);
111+
}
112+
}

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/DownscaleStatsTest.java renamed to libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/DownscaleStatsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import static org.hamcrest.MatcherAssert.assertThat;
2323
import static org.hamcrest.Matchers.equalTo;
2424

25-
public class DownscaleStatsTest extends ESTestCase {
25+
public class DownscaleStatsTests extends ESTestCase {
2626

2727
public void testExponential() {
2828
long[] values = IntStream.range(0, 100).mapToLong(i -> (long) Math.min(Integer.MAX_VALUE, Math.pow(1.1, i))).distinct().toArray();

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramMergerTest.java renamed to libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramMergerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import java.util.stream.Collectors;
2020
import java.util.stream.IntStream;
2121

22-
import static org.elasticsearch.exponentialhistogram.FixedSizeExponentialHistogramTest.printMidpoints;
22+
import static org.elasticsearch.exponentialhistogram.FixedSizeExponentialHistogramTests.printMidpoints;
2323
import static org.hamcrest.Matchers.equalTo;
2424

25-
public class ExponentialHistogramMergerTest extends ESTestCase {
25+
public class ExponentialHistogramMergerTests extends ESTestCase {
2626

2727
public void testZeroThresholdCollapsesOverlappingBuckets() {
2828
FixedSizeExponentialHistogram first = new FixedSizeExponentialHistogram(100);

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramUtilsTest.java renamed to libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramUtilsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import static org.hamcrest.Matchers.equalTo;
2222
import static org.hamcrest.Matchers.lessThanOrEqualTo;
2323

24-
public class ExponentialHistogramUtilsTest extends ESTestCase {
24+
public class ExponentialHistogramUtilsTests extends ESTestCase {
2525

2626
public void testMaxValue() {
2727
assertThat(getMaximumScaleIncrease(Long.MAX_VALUE), equalTo(0));

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/FixedSizeExponentialHistogramTest.java renamed to libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/FixedSizeExponentialHistogramTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import java.util.stream.IntStream;
1515

16-
public class FixedSizeExponentialHistogramTest extends ESTestCase {
16+
public class FixedSizeExponentialHistogramTests extends ESTestCase {
1717

1818
public void testPrintBuckets() {
1919
ExponentialHistogram first = ExponentialHistogramGenerator.createFor(0.01234, 42, 56789);

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/PercentileAccuracyTest.java renamed to libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/PercentileAccuracyTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import static org.hamcrest.Matchers.closeTo;
2828
import static org.hamcrest.Matchers.lessThan;
2929

30-
public class PercentileAccuracyTest extends ESTestCase {
30+
public class PercentileAccuracyTests extends ESTestCase {
3131

3232
public static final double[] PERCENTILES_TO_TEST = { 0, 0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99, 1.0 };
3333

@@ -254,7 +254,7 @@ private double getMaximumRelativeError(double[] values, int bucketCount) {
254254
// only positive values
255255
double gammaSquare = Math.pow(largestPositive / smallestPositive, 2.0 / (bucketCount));
256256
return (gammaSquare - 1) / (gammaSquare + 1);
257-
} else if (largestAbsNegative == 0) {
257+
} else if (smallestAbsNegative == 0) {
258258
// only negative values
259259
double gammaSquare = Math.pow(largestAbsNegative / smallestAbsNegative, 2.0 / (bucketCount));
260260
return (gammaSquare - 1) / (gammaSquare + 1);

0 commit comments

Comments
 (0)