Skip to content

Commit

Permalink
Add Histogram deserialization test
Browse files Browse the repository at this point in the history
Add an addition test to the HistogramSerDeTest which tests that
histogram deserialization roundtrips even when the serialized size
exceeds the initial buffer.
  • Loading branch information
travisdowns committed Dec 6, 2023
1 parent d2b1059 commit 35b6cff
Showing 1 changed file with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;

import org.HdrHistogram.Histogram;
import org.junit.jupiter.api.Test;

Expand All @@ -41,4 +44,62 @@ void deserialize() throws IOException {

assertThat(deserialized).isEqualTo(value);
}

/**
* Create a random histogram and insert the given number of samples.
*
* @param samples the number of samples to record into the histogram
* @return a Histogram with the given number of samples
*/
private Histogram randomHisto(int samples) {
Random r = new Random(0xBADBEEF);
Histogram h = new org.HdrHistogram.Histogram(5);
for (int i = 0; i < samples; i++) {
h.recordValue(r.nextInt(10000000));
}

return h;
}

byte[] serializeRandomHisto(int samples, int initialBufferSize) throws Exception {
ByteBuffer inbuffer = ByteBuffer.allocate(initialBufferSize);
Histogram inHisto = randomHisto(samples);
byte[] serialBytes = HistogramSerializer.toByteArray(HistogramSerializer.serializeHistogram(inHisto, inbuffer));

// check roundtrip
Histogram outHisto = Histogram.decodeFromCompressedByteBuffer(ByteBuffer.wrap(serialBytes), Long.MIN_VALUE);
assertThat(inHisto).isEqualTo(outHisto);

return serialBytes;
}

@Test
public void testHistogram() throws Exception {

// in the worker it's 8 MB but it takes a while to make a histogram that big
final int bufSize = 1002;

int samples = 300;

// we do an exponential search to fit the crossover point where we need to grow the buffer
while (true) {
byte[] serialBytes = serializeRandomHisto(samples, bufSize);
// System.out.println("Samples: " + samples + ", histogram size: " + serialBytes.length);
if (serialBytes.length >= bufSize) {
break;
}
samples *= 1.05;
}

// then walk backwards across the point linearly with increment of 1 to check the boundary
// carefully
while (true) {
samples--;
byte[] serialBytes = serializeRandomHisto(samples, bufSize);
// System.out.println("Samples: " + samples + ", histogram size: " + serialBytes.length);
if (serialBytes.length < bufSize - 10) {
break;
}
}
}
}

0 comments on commit 35b6cff

Please sign in to comment.