|
| 1 | +using System.Buffers; |
| 2 | +using BenchmarkDotNet.Attributes; |
| 3 | +using BenchmarkDotNet.Configs; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using UiPath.Caching.Config; |
| 6 | +using UiPath.Caching.MessagePack; |
| 7 | +using UiPath.Caching.Serialization; |
| 8 | + |
| 9 | +namespace UiPath.Caching.Benchmarks; |
| 10 | + |
| 11 | +/// <summary>The MessagePack serializer with compression off against its default threshold, at three payload sizes of compressible rows.</summary> |
| 12 | +[MemoryDiagnoser] |
| 13 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 14 | +[CategoriesColumn] |
| 15 | +public class MessagePackCompressionBenchmark |
| 16 | +{ |
| 17 | + private readonly ArrayBufferWriter<byte> _target = new(); |
| 18 | + private ServiceProvider _plainServices = default!; |
| 19 | + private ServiceProvider _compressedServices = default!; |
| 20 | + private ICacheValueSerializer _plain = default!; |
| 21 | + private ICacheValueSerializer _compressed = default!; |
| 22 | + private Payload _value = default!; |
| 23 | + private byte[] _plainBytes = default!; |
| 24 | + private byte[] _compressedBytes = default!; |
| 25 | + private int _compressedId; |
| 26 | + |
| 27 | + [Params(512, 1024, 16 * 1024, 128 * 1024)] |
| 28 | + public int Bytes { get; set; } |
| 29 | + |
| 30 | + [GlobalSetup] |
| 31 | + public void Setup() |
| 32 | + { |
| 33 | + _plainServices = Build(o => o.CompressionThresholdBytes = int.MaxValue); |
| 34 | + _compressedServices = Build(_ => { }); |
| 35 | + _plain = ValueSerializerRegistration.BuildRegistry(_plainServices).Entries.Single().Serializer; |
| 36 | + _compressed = ValueSerializerRegistration.BuildRegistry(_compressedServices).Entries.Single().Serializer; |
| 37 | + _value = Payload.OfSize(Bytes); |
| 38 | + _plainBytes = Write(_plain, out _); |
| 39 | + _compressedBytes = Write(_compressed, out _compressedId); |
| 40 | + Console.WriteLine($"size {Bytes}: plain {_plainBytes.Length}, compressed {_compressedBytes.Length} (id 0x{_compressedId:X2})"); |
| 41 | + } |
| 42 | + |
| 43 | + [GlobalCleanup] |
| 44 | + public void Cleanup() |
| 45 | + { |
| 46 | + _plainServices.Dispose(); |
| 47 | + _compressedServices.Dispose(); |
| 48 | + } |
| 49 | + |
| 50 | + [BenchmarkCategory("Write"), Benchmark(Baseline = true)] |
| 51 | + public int Write_Plain() |
| 52 | + { |
| 53 | + _target.ResetWrittenCount(); |
| 54 | + return _plain.Serialize(_value, _target); |
| 55 | + } |
| 56 | + |
| 57 | + [BenchmarkCategory("Write"), Benchmark] |
| 58 | + public int Write_Compressed() |
| 59 | + { |
| 60 | + _target.ResetWrittenCount(); |
| 61 | + return _compressed.Serialize(_value, _target); |
| 62 | + } |
| 63 | + |
| 64 | + [BenchmarkCategory("Read"), Benchmark(Baseline = true)] |
| 65 | + public Payload? Read_Plain() => _plain.Deserialize<Payload>(CacheValueFormats.MessagePack, new ReadOnlySequence<byte>(_plainBytes)); |
| 66 | + |
| 67 | + [BenchmarkCategory("Read"), Benchmark] |
| 68 | + public Payload? Read_Compressed() => _compressed.Deserialize<Payload>(_compressedId, new ReadOnlySequence<byte>(_compressedBytes)); |
| 69 | + |
| 70 | + private static ServiceProvider Build(Action<MessagePackValueSerializerOptions> configure) => |
| 71 | + new ServiceCollection() |
| 72 | + .AddCaching(b => b.AddMessagePackSerializer(configure: configure), o => o.AppShortName = "bench") |
| 73 | + .BuildServiceProvider(); |
| 74 | + |
| 75 | + private byte[] Write(ICacheValueSerializer serializer, out int id) |
| 76 | + { |
| 77 | + var target = new ArrayBufferWriter<byte>(); |
| 78 | + id = serializer.Serialize(_value, target); |
| 79 | + return target.WrittenMemory.ToArray(); |
| 80 | + } |
| 81 | + |
| 82 | + public sealed class Payload |
| 83 | + { |
| 84 | + public List<Row> Rows { get; set; } = []; |
| 85 | + |
| 86 | + public static Payload OfSize(int bytes) |
| 87 | + { |
| 88 | + var payload = new Payload(); |
| 89 | + var random = new Random(42); |
| 90 | + string[] statuses = ["active", "pending", "suspended", "closed"]; |
| 91 | + for (var size = 0; size < bytes; size += 60) |
| 92 | + { |
| 93 | + payload.Rows.Add(new Row |
| 94 | + { |
| 95 | + Id = payload.Rows.Count, |
| 96 | + Name = $"customer-{random.Next(100_000)}", |
| 97 | + Status = statuses[random.Next(statuses.Length)], |
| 98 | + Amount = Math.Round(random.NextDouble() * 1000, 2), |
| 99 | + Created = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMinutes(random.Next(500_000)), |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + return payload; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public sealed class Row |
| 108 | + { |
| 109 | + public int Id { get; set; } |
| 110 | + |
| 111 | + public string? Name { get; set; } |
| 112 | + |
| 113 | + public string? Status { get; set; } |
| 114 | + |
| 115 | + public double Amount { get; set; } |
| 116 | + |
| 117 | + public DateTime Created { get; set; } |
| 118 | + } |
| 119 | +} |
0 commit comments