Created
November 6, 2020 01:36
-
-
Save eerhardt/db06d97c93faaae9b9aec79811c94559 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using Microsoft.Azure.Amqp; | |
using Microsoft.Azure.Amqp.Encoding; | |
using System; | |
using System.Runtime.InteropServices; | |
[MemoryDiagnoser] | |
public class AmqpBench | |
{ | |
public static readonly byte[] RandomBytes1MB = new byte[1024 * 1024]; | |
public static readonly int[] RandomInt32Array1M = new int[1024 * 1024]; | |
public static readonly int[] RandomInt32Array1K = new int[1024]; | |
public static readonly AmqpSymbol[] RandomAmqpSymbolArray100K = new AmqpSymbol[1024 * 10]; | |
public static readonly AmqpSymbol[] RandomAmqpSymbolArray1K = new AmqpSymbol[1024]; | |
// buffers for 1M values | |
public static readonly ByteBuffer ScratchByteBuffer = new ByteBuffer(new byte[1024 * 1024 * 10], autoGrow: false); | |
public static readonly byte[] ScratchArray = new byte[1024 * 1024 * 10]; | |
public static readonly int[] ScratchInt32Array = new int[1024 * 1024 * 2]; | |
public static readonly byte[] EncodedBytes1MB; | |
public static readonly ByteBuffer EncodedBytes1MBBuffer; | |
public static readonly byte[] EncodedInt32Array1M; | |
public static readonly byte[] EncodedInt32Array1K; | |
public static readonly ByteBuffer EncodedInt32Array1MBuffer; | |
public static readonly ByteBuffer EncodedInt32Array1KBuffer; | |
public static readonly ByteBuffer EncodedAmqpSymbolArray1MBuffer; | |
public static readonly ByteBuffer EncodedAmqpSymbolArray1KBuffer; | |
static AmqpBench() | |
{ | |
Random rng = new Random(0); | |
rng.NextBytes(RandomBytes1MB); | |
rng.NextBytes(MemoryMarshal.AsBytes(RandomInt32Array1M.AsSpan())); | |
rng.NextBytes(MemoryMarshal.AsBytes(RandomInt32Array1K.AsSpan())); | |
FillSymbolArray(rng, RandomAmqpSymbolArray100K); | |
FillSymbolArray(rng, RandomAmqpSymbolArray1K); | |
ScratchByteBuffer.Reset(); | |
AmqpCodec.EncodeBinary(RandomBytes1MB, ScratchByteBuffer); | |
EncodedBytes1MB = ScratchByteBuffer.Buffer.AsMemory(0, ScratchByteBuffer.WritePos).ToArray(); | |
EncodedBytes1MBBuffer = new ByteBuffer(EncodedBytes1MB, 0, EncodedBytes1MB.Length); | |
ScratchByteBuffer.Reset(); | |
AmqpCodec.EncodeArray(RandomInt32Array1M, ScratchByteBuffer); | |
EncodedInt32Array1M = ScratchByteBuffer.Buffer.AsMemory(0, ScratchByteBuffer.WritePos).ToArray(); | |
EncodedInt32Array1MBuffer = new ByteBuffer(EncodedInt32Array1M, 0, EncodedInt32Array1M.Length); | |
ScratchByteBuffer.Reset(); | |
AmqpCodec.EncodeArray(RandomInt32Array1K, ScratchByteBuffer); | |
EncodedInt32Array1K = ScratchByteBuffer.Buffer.AsMemory(0, ScratchByteBuffer.WritePos).ToArray(); | |
EncodedInt32Array1KBuffer = new ByteBuffer(EncodedInt32Array1K, 0, EncodedInt32Array1K.Length); | |
EncodedAmqpSymbolArray1MBuffer = new ByteBuffer(new byte[1024 * 1024 * 10], autoGrow: true); | |
AmqpCodec.EncodeArray(RandomAmqpSymbolArray100K, EncodedAmqpSymbolArray1MBuffer); | |
EncodedAmqpSymbolArray1KBuffer = new ByteBuffer(new byte[1024 * 1024 * 10], autoGrow: true); | |
AmqpCodec.EncodeArray(RandomAmqpSymbolArray1K, EncodedAmqpSymbolArray1KBuffer); | |
} | |
private static void FillSymbolArray(Random rng, AmqpSymbol[] symbolArray) | |
{ | |
for (int i = 0; i < symbolArray.Length; i++) | |
{ | |
int length = rng.Next(10); | |
symbolArray[i] = new AmqpSymbol(string.Create(length, rng, (s, innerRng) => | |
{ | |
char c = (char)innerRng.Next('a', 'z'); | |
s.Fill(c); | |
})); | |
} | |
} | |
[Benchmark] | |
public int ArrayAmqpSymbolDecode_1M_MAA() | |
{ | |
EncodedAmqpSymbolArray1MBuffer.Seek(0); | |
var result = AmqpCodec.DecodeArray<AmqpSymbol>(EncodedAmqpSymbolArray1MBuffer); | |
return result.Length; | |
} | |
[Benchmark] | |
public int ArrayAmqpSymbolDecode_1K_MAA() | |
{ | |
EncodedAmqpSymbolArray1KBuffer.Seek(0); | |
var result = AmqpCodec.DecodeArray<AmqpSymbol>(EncodedAmqpSymbolArray1KBuffer); | |
return result.Length; | |
} | |
[Benchmark] | |
public void ArrayAmqpSymbolEncode_100K_MAA() | |
{ | |
EncodedAmqpSymbolArray1MBuffer.Reset(); | |
AmqpCodec.EncodeArray(RandomAmqpSymbolArray100K, EncodedAmqpSymbolArray1MBuffer); | |
} | |
[Benchmark] | |
public void ArrayAmqpSymbolEncode_1K_MAA() | |
{ | |
EncodedAmqpSymbolArray1KBuffer.Reset(); | |
AmqpCodec.EncodeArray(RandomAmqpSymbolArray1K, EncodedAmqpSymbolArray1KBuffer); | |
} | |
[Benchmark] | |
public ReadOnlyMemory<byte> Bytes_Encode_MAA() | |
{ | |
ScratchByteBuffer.Reset(); | |
AmqpCodec.EncodeBinary(RandomBytes1MB, ScratchByteBuffer); | |
return ScratchByteBuffer.Buffer.AsMemory(0, ScratchByteBuffer.WritePos); | |
} | |
[Benchmark] | |
public ArraySegment<byte> Bytes_Decode_MAA() | |
{ | |
EncodedBytes1MBBuffer.Seek(0); | |
var result = AmqpCodec.DecodeBinary(EncodedBytes1MBBuffer); | |
return result; | |
} | |
[Benchmark] | |
public ReadOnlyMemory<byte> ArrayInt32Encode_MAA_1M() | |
{ | |
ScratchByteBuffer.Reset(); | |
AmqpCodec.EncodeArray(RandomInt32Array1M, ScratchByteBuffer); | |
return ScratchByteBuffer.Buffer.AsMemory(0, ScratchByteBuffer.WritePos); | |
} | |
[Benchmark] | |
public ReadOnlyMemory<byte> ArrayInt32Encode_MAA_1K() | |
{ | |
ScratchByteBuffer.Reset(); | |
AmqpCodec.EncodeArray(RandomInt32Array1K, ScratchByteBuffer); | |
return ScratchByteBuffer.Buffer.AsMemory(0, ScratchByteBuffer.WritePos); | |
} | |
[Benchmark] | |
public int ArrayInt32Decode_1M_MAA() | |
{ | |
EncodedInt32Array1MBuffer.Seek(0); | |
var result = AmqpCodec.DecodeArray<int>(EncodedInt32Array1MBuffer); | |
return result.Length; | |
} | |
[Benchmark] | |
public int ArrayInt32Decode_1K_MAA() | |
{ | |
EncodedInt32Array1KBuffer.Seek(0); | |
var result = AmqpCodec.DecodeArray<int>(EncodedInt32Array1KBuffer); | |
return result.Length; | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BenchmarkRunner.Run(typeof(Program).Assembly); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment