GetNeededByteBufferCapacity() public method

Get the capacity needed to encode this histogram into a ByteBuffer
public GetNeededByteBufferCapacity ( ) : int
return int
Beispiel #1
0
        private static HistogramBase CompressedEncodeDecode(HistogramBase source)
        {
            var targetBuffer = ByteBuffer.Allocate(source.GetNeededByteBufferCapacity());

            source.EncodeIntoCompressedByteBuffer(targetBuffer);
            targetBuffer.Position = 0;
            return(HistogramEncoding.DecodeFromCompressedByteBuffer(targetBuffer, 0));
        }
        private void WriteHistogram(HistogramBase histogram)
        {
            var targetBuffer     = ByteBuffer.Allocate(histogram.GetNeededByteBufferCapacity());
            var compressedLength = histogram.EncodeIntoCompressedByteBuffer(targetBuffer);

            byte[] compressedArray = new byte[compressedLength];
            targetBuffer.BlockGet(compressedArray, 0, 0, compressedLength);

            var startTimeStampSec = histogram.StartTimeStamp / 1000.0;
            var endTimeStampSec   = histogram.EndTimeStamp / 1000.0;
            var intervalLength    = endTimeStampSec - startTimeStampSec;
            var maxValueUnitRatio = 1000000.0;
            var intervalMax       = histogram.GetMaxValue() / maxValueUnitRatio;

            var binary  = Convert.ToBase64String(compressedArray);
            var payload = $"{startTimeStampSec:F3},{intervalLength:F3},{intervalMax:F3},{binary}";

            _log.WriteLine(payload);
            _log.Flush();
        }
        /// <summary>
        /// Encode this histogram in compressed form into a <see cref="ByteBuffer"/>.
        /// </summary>
        /// <param name="source">The histogram to encode</param>
        /// <param name="targetBuffer">The buffer to write to</param>
        /// <returns>The number of bytes written to the buffer</returns>
        public static int EncodeIntoCompressedByteBuffer(this HistogramBase source, ByteBuffer targetBuffer)
        {
            int neededCapacity = source.GetNeededByteBufferCapacity();
            var intermediateUncompressedByteBuffer = ByteBuffer.Allocate(neededCapacity);

            source.Encode(intermediateUncompressedByteBuffer, HistogramEncoderV2.Instance);

            int initialTargetPosition = targetBuffer.Position;

            targetBuffer.PutInt(GetCompressedEncodingCookie());
            int compressedContentsHeaderPosition = targetBuffer.Position;

            targetBuffer.PutInt(0); // Placeholder for compressed contents length
            var compressedDataLength = targetBuffer.CompressedCopy(intermediateUncompressedByteBuffer, targetBuffer.Position);

            targetBuffer.PutInt(compressedContentsHeaderPosition, compressedDataLength); // Record the compressed length
            int bytesWritten = compressedDataLength + 8;

            targetBuffer.Position = (initialTargetPosition + bytesWritten);
            return(bytesWritten);
        }
        private void WriteHistogram(HistogramBase histogram)
        {
            var targetBuffer = ByteBuffer.Allocate(histogram.GetNeededByteBufferCapacity());
            var compressedLength = histogram.EncodeIntoCompressedByteBuffer(targetBuffer);
            byte[] compressedArray = new byte[compressedLength];
            targetBuffer.BlockGet(compressedArray, 0, 0, compressedLength);

            var startTimeStampSec = histogram.StartTimeStamp / 1000.0;
            var endTimeStampSec = histogram.EndTimeStamp / 1000.0;
            var intervalLength = endTimeStampSec - startTimeStampSec;
            var maxValueUnitRatio = 1000000.0;
            var intervalMax = histogram.GetMaxValue() / maxValueUnitRatio;

            var binary = Convert.ToBase64String(compressedArray);
            var payload = $"{startTimeStampSec:F3},{intervalLength:F3},{intervalMax:F3},{binary}";
            _log.WriteLine(payload);
            _log.Flush();
        }