Created
May 3, 2024 14:13
-
-
Save sa-es-ir/fb60413412b9d8fb00dabf526a4944cb 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 Avro.IO; | |
using Avro.Reflect; | |
using Avro; | |
using BenchmarkDotNet.Attributes; | |
using MessagePack; | |
using MongoDB.Bson; | |
using ProtoBuf; | |
using System.Text.Json; | |
using BenchmarkDotNet.Columns; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Reports; | |
namespace SerializerComparison; | |
[Config(typeof(Config))] | |
[HideColumns(Column.RatioSD, Column.AllocRatio)] | |
[MemoryDiagnoser(false)] | |
public class SerializerBenchmark | |
{ | |
private Order _order = new Order().Create(); | |
private string orderSchema = @" | |
{ | |
""type"": ""record"", | |
""name"": ""Order"", | |
""fields"": [ | |
{ ""name"": ""Id"", ""type"": ""string"" }, | |
{ ""name"": ""Name"", ""type"": ""string"" }, | |
{ ""name"": ""Category"", ""type"": ""string"" }, | |
{ ""name"": ""User"", ""type"": ""string"" }, | |
{ ""name"": ""TotalAmount"", ""type"": ""long"" } | |
] | |
}"; | |
private ReflectWriter<Order> _avroWriter; | |
private class Config : ManualConfig | |
{ | |
public Config() | |
{ | |
SummaryStyle = SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend); | |
} | |
} | |
[GlobalSetup] | |
public void Setup() | |
{ | |
Schema schema = Schema.Parse(orderSchema); | |
_avroWriter = new ReflectWriter<Order>(schema); | |
} | |
[Benchmark(Baseline = true)] | |
public void Json() | |
=> Newtonsoft.Json.JsonConvert.SerializeObject(_order); | |
[Benchmark] | |
public void JsonText() | |
=> JsonSerializer.Serialize(_order); | |
[Benchmark] | |
public void Protobuf() | |
{ | |
using var protobufMs = new MemoryStream(); | |
Serializer.Serialize(protobufMs, _order); | |
} | |
[Benchmark] | |
public void Avro() | |
{ | |
using var avroMs = new MemoryStream(); | |
_avroWriter.Write(_order, new BinaryEncoder(avroMs)); | |
} | |
[Benchmark] | |
public void MessagePack() => MessagePackSerializer.Serialize(_order); | |
[Benchmark] | |
public void Bson() => _order.ToBson(); | |
} | |
[ProtoContract] | |
[MessagePackObject] | |
public class Order | |
{ | |
[ProtoMember(1)] // protobuf | |
[Key(0)] // messagePack | |
public string Id { get; set; } = Guid.NewGuid().ToString(); | |
[ProtoMember(2)] | |
[Key(1)] | |
public string Name { get; set; } | |
[ProtoMember(3)] | |
[Key(2)] | |
public string Category { get; set; } | |
[ProtoMember(4)] | |
[Key(3)] | |
public long TotalAmount { get; set; } | |
[ProtoMember(5)] | |
[Key(4)] | |
public string User { get; set; } | |
public Order Create() | |
{ | |
return new Order | |
{ | |
Name = "Book Order", | |
Category = "Books", | |
TotalAmount = 100, | |
User = Guid.NewGuid().ToString() | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment