114 lines
4.4 KiB
Java
114 lines
4.4 KiB
Java
|
|
package org.HdrHistogram;
|
||
|
|
|
||
|
|
import org.junit.Test;
|
||
|
|
|
||
|
|
import java.nio.ByteBuffer;
|
||
|
|
import java.util.concurrent.ThreadLocalRandom;
|
||
|
|
|
||
|
|
public class HistogramSketchTest {
|
||
|
|
String line = "###################";
|
||
|
|
|
||
|
|
@Test
|
||
|
|
public void testArrayHistogram() {
|
||
|
|
ArrayHistogram histogram = new ArrayHistogram(1);
|
||
|
|
System.out.println(histogram.describe());
|
||
|
|
for (int i = 0; i < 10000; i++) {
|
||
|
|
histogram.recordValue(i);
|
||
|
|
}
|
||
|
|
System.out.println(histogram.describe());
|
||
|
|
for (Percentile percentile : histogram.percentileList(5)) {
|
||
|
|
System.out.println(percentile);
|
||
|
|
}
|
||
|
|
System.out.println(line);
|
||
|
|
histogram = new ArrayHistogram(1);
|
||
|
|
for (int i = 0; i < 10000; i++) {
|
||
|
|
histogram.recordValue(ThreadLocalRandom.current().nextLong(100000));
|
||
|
|
}
|
||
|
|
System.out.println(histogram.describe());
|
||
|
|
for (Percentile percentile : histogram.percentileList(5)) {
|
||
|
|
System.out.println(percentile);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
public void testDirectArrayHistogram() {
|
||
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1));
|
||
|
|
DirectArrayHistogram histogram = new DirectArrayHistogram(1, 100000, 1,
|
||
|
|
byteBuffer);
|
||
|
|
System.out.println(histogram.describe());
|
||
|
|
for (int i = 0; i < 10000; i++) {
|
||
|
|
histogram.recordValue(i);
|
||
|
|
}
|
||
|
|
System.out.println(histogram.describe());
|
||
|
|
for (Percentile percentile : histogram.percentileList(5)) {
|
||
|
|
System.out.println(percentile);
|
||
|
|
}
|
||
|
|
System.out.println(line);
|
||
|
|
histogram.resetByteBuffer(ByteBuffer.allocate(DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1)));
|
||
|
|
histogram.reset();
|
||
|
|
for (int i = 0; i < 10000; i++) {
|
||
|
|
histogram.recordValue(ThreadLocalRandom.current().nextLong(100000));
|
||
|
|
}
|
||
|
|
System.out.println(histogram.describe());
|
||
|
|
for (Percentile percentile : histogram.percentileList(5)) {
|
||
|
|
System.out.println(percentile);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
public void testCopy() {
|
||
|
|
ArrayHistogram histogram1 = new ArrayHistogram(1);
|
||
|
|
ByteBuffer byteBuffer = ByteBuffer.allocate(DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1));
|
||
|
|
DirectArrayHistogram histogram2 = new DirectArrayHistogram(1, 100000, 1,
|
||
|
|
byteBuffer);
|
||
|
|
for (int i = 0; i < 10000; i++) {
|
||
|
|
histogram1.recordValue(i);
|
||
|
|
histogram2.recordValue(i);
|
||
|
|
}
|
||
|
|
|
||
|
|
ArrayHistogram copy1 = histogram1.copy();
|
||
|
|
ArrayHistogram copy2 = histogram2.copy();
|
||
|
|
|
||
|
|
System.out.println(histogram1.describe());
|
||
|
|
System.out.println(histogram2.describe());
|
||
|
|
System.out.println(line);
|
||
|
|
|
||
|
|
System.out.println(copy1.describe());
|
||
|
|
System.out.println(copy2.describe());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
public void testArrayHistogramMerge() {
|
||
|
|
ArrayHistogram histogram1 = new ArrayHistogram(1);
|
||
|
|
for (int i = 0; i < 1000; i++) {
|
||
|
|
histogram1.recordValue(i);
|
||
|
|
}
|
||
|
|
byte[] bytes1 = histogram1.toBytes();
|
||
|
|
|
||
|
|
DirectMapHistogram directHistogram = DirectMapHistogram.wrapBytes(bytes1);
|
||
|
|
ArrayHistogram histogram2 = new ArrayHistogram(1);
|
||
|
|
histogram2.merge(directHistogram);
|
||
|
|
System.out.println(histogram2.describe());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
public void testDirectArrayHistogramMerge() {
|
||
|
|
ArrayHistogram histogram1 = new ArrayHistogram(1);
|
||
|
|
int updatableSerializationBytes = DirectArrayHistogram.getUpdatableSerializationBytes(1, 100000, 1);
|
||
|
|
DirectArrayHistogram histogram2 = new DirectArrayHistogram(1, 100000, 1, ByteBuffer.allocate(updatableSerializationBytes));
|
||
|
|
for (int i = 0; i < 10000; i++) {
|
||
|
|
histogram1.recordValue(i);
|
||
|
|
histogram2.recordValue(i);
|
||
|
|
}
|
||
|
|
byte[] bytes1 = histogram1.toBytes();
|
||
|
|
byte[] bytes2 = histogram2.toBytes();
|
||
|
|
|
||
|
|
DirectArrayHistogram histogram = new DirectArrayHistogram(1, 100000, 1, ByteBuffer.allocate(updatableSerializationBytes));
|
||
|
|
histogram.merge(DirectMapHistogram.wrapBytes(bytes1));
|
||
|
|
histogram.merge(DirectMapHistogram.wrapBytes(bytes2));
|
||
|
|
System.out.println(histogram.describe());
|
||
|
|
for (Percentile percentile : histogram.percentileList(5)) {
|
||
|
|
System.out.println(percentile);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|