Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ private void doubleBucketWidth(Map<Integer, LongAdder> buckets) {
}
buckets.clear();
for (i = 0; i < keys.length; i++) {
int index = (keys[i] + 1) / 2;
int index = (keys[i] > 0 ? keys[i] + 1 : keys[i]) / 2;
LongAdder count = buckets.computeIfAbsent(index, k -> new LongAdder());
count.add(values[i]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CompletionService;
import java.util.concurrent.CountDownLatch;
Expand All @@ -39,6 +41,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -836,6 +839,36 @@ void testNativeBucketIndexToUpperBound()
}
}

@Test
void testDoubleBucketWidthMergesNegativeIndexesCorrectly()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Histogram histogram = Histogram.builder().name("test").nativeOnly().build();
Histogram.DataPoint dataPoint = histogram.newDataPoint();

Method doubleBucketWidth =
Histogram.DataPoint.class.getDeclaredMethod("doubleBucketWidth", Map.class);
doubleBucketWidth.setAccessible(true);

int[] keys = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
int[] expectedMergedIndexes = {-2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 3};

for (int i = 0; i < keys.length; i++) {
Map<Integer, LongAdder> buckets = new HashMap<>();
LongAdder adder = new LongAdder();
adder.add(7);
buckets.put(keys[i], adder);

doubleBucketWidth.invoke(dataPoint, buckets);

assertThat(buckets.keySet())
.as("merged index for original key " + keys[i])
.containsExactly(expectedMergedIndexes[i]);
assertThat(buckets.get(expectedMergedIndexes[i]).sum())
.as("count preserved for original key " + keys[i])
.isEqualTo(7);
}
}

/**
* Test if lowerBound < value <= upperBound is true for the bucket index returned by
* findBucketIndex()
Expand Down