|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +using BenchmarkDotNet.Attributes; |
| 7 | + |
| 8 | +using RabbitMQ.Client; |
| 9 | +using RabbitMQ.Client.Impl; |
| 10 | + |
| 11 | +namespace RabbitMQ.Benchmarks |
| 12 | +{ |
| 13 | + [Config(typeof(Config))] |
| 14 | + [BenchmarkCategory("DataTypes")] |
| 15 | + public class DataTypeSerialization |
| 16 | + { |
| 17 | + protected readonly Memory<byte> _buffer = new Memory<byte>(new byte[16384]); |
| 18 | + protected readonly AmqpTimestamp _timestamp = new AmqpTimestamp(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); |
| 19 | + |
| 20 | + [GlobalSetup] |
| 21 | + public virtual void SetUp() { } |
| 22 | + } |
| 23 | + |
| 24 | + public class DateTypeArraySerialization : DataTypeSerialization |
| 25 | + { |
| 26 | + readonly List<object> _emptyArray = new List<object>(); |
| 27 | + Memory<byte> _emptyArrayBuffer; |
| 28 | + Memory<byte> _populatedArrayBuffer; |
| 29 | + List<object> _array; |
| 30 | + |
| 31 | + public override void SetUp() |
| 32 | + { |
| 33 | + _array = new List<object> { "longstring", 1234, 12.34m, _timestamp }; |
| 34 | + _emptyArrayBuffer = new byte[WireFormatting.GetArrayByteCount(_emptyArray)]; |
| 35 | + WireFormatting.WriteArray(_emptyArrayBuffer.Span, _emptyArray); |
| 36 | + |
| 37 | + _populatedArrayBuffer = new byte[WireFormatting.GetArrayByteCount(_array)]; |
| 38 | + WireFormatting.WriteArray(_populatedArrayBuffer.Span, _array); |
| 39 | + } |
| 40 | + |
| 41 | + [Benchmark] |
| 42 | + public IList ArrayReadEmpty() => WireFormatting.ReadArray(_emptyArrayBuffer.Span, out _); |
| 43 | + |
| 44 | + [Benchmark] |
| 45 | + public IList ArrayReadPopulated() => WireFormatting.ReadArray(_populatedArrayBuffer.Span, out _); |
| 46 | + |
| 47 | + [Benchmark] |
| 48 | + public int ArrayWriteEmpty() => WireFormatting.WriteArray(_buffer.Span, _emptyArray); |
| 49 | + |
| 50 | + [Benchmark] |
| 51 | + public int ArrayWritePopulated() => WireFormatting.WriteArray(_buffer.Span, _array); |
| 52 | + } |
| 53 | + |
| 54 | + public class DataTypeTableSerialization : DataTypeSerialization |
| 55 | + { |
| 56 | + IDictionary<string, object> _emptyDict = new Dictionary<string, object>(); |
| 57 | + IDictionary<string, object> _populatedDict; |
| 58 | + Memory<byte> _emptyDictionaryBuffer; |
| 59 | + Memory<byte> _populatedDictionaryBuffer; |
| 60 | + |
| 61 | + public override void SetUp() |
| 62 | + { |
| 63 | + _populatedDict = new Dictionary<string, object> |
| 64 | + { |
| 65 | + { "string", "Hello" }, |
| 66 | + { "int", 1234 }, |
| 67 | + { "uint", 1234u }, |
| 68 | + { "decimal", 12.34m }, |
| 69 | + { "timestamp", _timestamp }, |
| 70 | + { "fieldtable", new Dictionary<string, object>(){ { "test", "test" } } }, |
| 71 | + { "fieldarray", new List<object> { "longstring", 1234, 12.34m, _timestamp } } |
| 72 | + }; |
| 73 | + |
| 74 | + _emptyDictionaryBuffer = new byte[WireFormatting.GetTableByteCount(_emptyDict)]; |
| 75 | + WireFormatting.WriteTable(_emptyDictionaryBuffer.Span, _emptyDict); |
| 76 | + |
| 77 | + _populatedDictionaryBuffer = new byte[WireFormatting.GetTableByteCount(_populatedDict)]; |
| 78 | + WireFormatting.WriteTable(_populatedDictionaryBuffer.Span, _populatedDict); |
| 79 | + } |
| 80 | + |
| 81 | + [Benchmark] |
| 82 | + public int TableReadEmpty() => WireFormatting.ReadDictionary(_emptyDictionaryBuffer.Span, out _); |
| 83 | + |
| 84 | + [Benchmark] |
| 85 | + public int TableReadPopulated() => WireFormatting.ReadDictionary(_populatedDictionaryBuffer.Span, out _); |
| 86 | + |
| 87 | + [Benchmark] |
| 88 | + public int TableWriteEmpty() => WireFormatting.WriteTable(_buffer.Span, _emptyDict); |
| 89 | + |
| 90 | + [Benchmark] |
| 91 | + public int TableWritePopulated() => WireFormatting.WriteTable(_buffer.Span, _populatedDict); |
| 92 | + } |
| 93 | + |
| 94 | + public class DataTypeLongStringSerialization : DataTypeSerialization |
| 95 | + { |
| 96 | + readonly string _longString = new string('X', 4096); |
| 97 | + readonly Memory<byte> _emptyLongStringBuffer = GenerateLongStringBuffer(string.Empty); |
| 98 | + readonly Memory<byte> _populatedLongStringBuffer = GenerateLongStringBuffer(new string('X', 4096)); |
| 99 | + |
| 100 | + [Benchmark] |
| 101 | + public int LongstrReadEmpty() => WireFormatting.ReadLongstr(_emptyLongStringBuffer.Span, out _); |
| 102 | + |
| 103 | + [Benchmark] |
| 104 | + public int LongstrReadPopulated() => WireFormatting.ReadLongstr(_populatedLongStringBuffer.Span, out _); |
| 105 | + |
| 106 | + [Benchmark] |
| 107 | + public int LongstrWriteEmpty() => WireFormatting.WriteLongstr(_buffer.Span, string.Empty); |
| 108 | + |
| 109 | + [Benchmark] |
| 110 | + public int LongstrWritePopulated() => WireFormatting.WriteLongstr(_buffer.Span, _longString); |
| 111 | + |
| 112 | + private static byte[] GenerateLongStringBuffer(string val) |
| 113 | + { |
| 114 | + byte[] _buffer = new byte[5 + Encoding.UTF8.GetByteCount(val)]; |
| 115 | + WireFormatting.WriteLongstr(_buffer, val); |
| 116 | + return _buffer; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public class DataTypeShortStringSerialization : DataTypeSerialization |
| 121 | + { |
| 122 | + readonly string _shortString = new string('X', 255); |
| 123 | + readonly Memory<byte> _emptyShortStringBuffer = GenerateStringBuffer(string.Empty); |
| 124 | + readonly Memory<byte> _populatedShortStringBuffer = GenerateStringBuffer(new string('X', 255)); |
| 125 | + |
| 126 | + [Benchmark] |
| 127 | + public int ShortstrReadEmpty() => WireFormatting.ReadShortstr(_emptyShortStringBuffer.Span, out _); |
| 128 | + |
| 129 | + [Benchmark] |
| 130 | + public int ShortstrReadPopulated() => WireFormatting.ReadShortstr(_populatedShortStringBuffer.Span, out _); |
| 131 | + |
| 132 | + [Benchmark] |
| 133 | + public int ShortstrWriteEmpty() => WireFormatting.WriteShortstr(_buffer.Span, string.Empty); |
| 134 | + |
| 135 | + [Benchmark] |
| 136 | + public int ShortstrWritePopulated() => WireFormatting.WriteShortstr(_buffer.Span, _shortString); |
| 137 | + |
| 138 | + private static byte[] GenerateStringBuffer(string val) |
| 139 | + { |
| 140 | + byte[] _buffer = new byte[2 + Encoding.UTF8.GetByteCount(val)]; |
| 141 | + WireFormatting.WriteShortstr(_buffer, val); |
| 142 | + return _buffer; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments