|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using HdrHistogram.Encoding; |
| 3 | + |
| 4 | +namespace HdrHistogram.Benchmarking.Serialisation |
| 5 | +{ |
| 6 | + [MemoryDiagnoser] |
| 7 | + public class SerialisationBenchmark |
| 8 | + { |
| 9 | + private LongHistogram _source = null!; |
| 10 | + private Utilities.ByteBuffer _encodeBuffer = null!; |
| 11 | + private Utilities.ByteBuffer _decodeBuffer = null!; |
| 12 | + private Utilities.ByteBuffer _compressedDecodeBuffer = null!; |
| 13 | + |
| 14 | + [GlobalSetup] |
| 15 | + public void Setup() |
| 16 | + { |
| 17 | + _source = new LongHistogram(3600_000_000L, 3); |
| 18 | + for (long i = 0; i < 10_000; i++) |
| 19 | + { |
| 20 | + _source.RecordValue(1000L * i); |
| 21 | + } |
| 22 | + |
| 23 | + var capacity = _source.GetNeededByteBufferCapacity(); |
| 24 | + _encodeBuffer = Utilities.ByteBuffer.Allocate(capacity); |
| 25 | + |
| 26 | + // Pre-encode for uncompressed decode benchmark |
| 27 | + _source.Encode(_decodeBuffer = Utilities.ByteBuffer.Allocate(capacity), HistogramEncoderV2.Instance); |
| 28 | + |
| 29 | + // Pre-encode for compressed decode benchmark |
| 30 | + _source.EncodeIntoCompressedByteBuffer(_compressedDecodeBuffer = Utilities.ByteBuffer.Allocate(capacity)); |
| 31 | + } |
| 32 | + |
| 33 | + [Benchmark] |
| 34 | + public int Encode() |
| 35 | + { |
| 36 | + _encodeBuffer.Position = 0; |
| 37 | + return _source.Encode(_encodeBuffer, HistogramEncoderV2.Instance); |
| 38 | + } |
| 39 | + |
| 40 | + [Benchmark] |
| 41 | + public HistogramBase Decode() |
| 42 | + { |
| 43 | + _decodeBuffer.Position = 0; |
| 44 | + return HistogramEncoding.DecodeFromByteBuffer(_decodeBuffer, 0); |
| 45 | + } |
| 46 | + |
| 47 | + [Benchmark] |
| 48 | + public int EncodeCompressed() |
| 49 | + { |
| 50 | + _encodeBuffer.Position = 0; |
| 51 | + return _source.EncodeIntoCompressedByteBuffer(_encodeBuffer); |
| 52 | + } |
| 53 | + |
| 54 | + [Benchmark] |
| 55 | + public HistogramBase DecodeCompressed() |
| 56 | + { |
| 57 | + _compressedDecodeBuffer.Position = 0; |
| 58 | + return HistogramEncoding.DecodeFromCompressedByteBuffer(_compressedDecodeBuffer, 0); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments