|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Buffers; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | + |
| 8 | +namespace Microsoft.AspNetCore.DataProtection.MicroBenchmarks; |
| 9 | + |
| 10 | +[SimpleJob, MemoryDiagnoser] |
| 11 | +public class SpanDataProtectorComparison |
| 12 | +{ |
| 13 | + private IDataProtector _dataProtector = null!; |
| 14 | + private ISpanDataProtector _spanDataProtector = null!; |
| 15 | + |
| 16 | + private byte[] _plaintext5 = null!; |
| 17 | + private byte[] _plaintext50 = null!; |
| 18 | + private byte[] _plaintext100 = null!; |
| 19 | + |
| 20 | + [Params(5, 50, 100)] |
| 21 | + public int PlaintextLength { get; set; } |
| 22 | + |
| 23 | + [GlobalSetup] |
| 24 | + public void Setup() |
| 25 | + { |
| 26 | + // Setup DataProtection as in DI |
| 27 | + var services = new ServiceCollection(); |
| 28 | + services.AddDataProtection(); |
| 29 | + var serviceProvider = services.BuildServiceProvider(); |
| 30 | + |
| 31 | + _dataProtector = serviceProvider.GetDataProtector("benchmark", "test"); |
| 32 | + _spanDataProtector = (ISpanDataProtector)_dataProtector; |
| 33 | + |
| 34 | + // Setup test data for different lengths |
| 35 | + var random = new Random(42); // Fixed seed for consistent results |
| 36 | + |
| 37 | + _plaintext5 = new byte[5]; |
| 38 | + random.NextBytes(_plaintext5); |
| 39 | + |
| 40 | + _plaintext50 = new byte[50]; |
| 41 | + random.NextBytes(_plaintext50); |
| 42 | + |
| 43 | + _plaintext100 = new byte[100]; |
| 44 | + random.NextBytes(_plaintext100); |
| 45 | + } |
| 46 | + |
| 47 | + private byte[] GetPlaintext() |
| 48 | + { |
| 49 | + return PlaintextLength switch |
| 50 | + { |
| 51 | + 5 => _plaintext5, |
| 52 | + 50 => _plaintext50, |
| 53 | + 100 => _plaintext100, |
| 54 | + _ => throw new ArgumentException("Invalid plaintext length") |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + [Benchmark] |
| 59 | + public void ProtectUnprotectRoundtrip() |
| 60 | + { |
| 61 | + var plaintext = GetPlaintext(); |
| 62 | + |
| 63 | + // Traditional approach with allocations |
| 64 | + var protectedData = _dataProtector.Protect(plaintext); |
| 65 | + var unprotectedData = _dataProtector.Unprotect(protectedData); |
| 66 | + } |
| 67 | + |
| 68 | + [Benchmark] |
| 69 | + public void TryProtectTryUnprotectRoundtrip() |
| 70 | + { |
| 71 | + var plaintext = GetPlaintext(); |
| 72 | + |
| 73 | + // Span-based approach with minimal allocations |
| 74 | + var protectedSize = _spanDataProtector.GetProtectedSize(plaintext.Length); |
| 75 | + var protectedBuffer = ArrayPool<byte>.Shared.Rent(protectedSize); |
| 76 | + |
| 77 | + try |
| 78 | + { |
| 79 | + var protectSuccess = _spanDataProtector.TryProtect(plaintext, protectedBuffer, out var protectedBytesWritten); |
| 80 | + if (!protectSuccess) |
| 81 | + { |
| 82 | + throw new InvalidOperationException("TryProtect failed"); |
| 83 | + } |
| 84 | + |
| 85 | + var unprotectedSize = _spanDataProtector.GetUnprotectedSize(protectedBytesWritten); |
| 86 | + var unprotectedBuffer = ArrayPool<byte>.Shared.Rent(unprotectedSize); |
| 87 | + |
| 88 | + try |
| 89 | + { |
| 90 | + var unprotectSuccess = _spanDataProtector.TryUnprotect( |
| 91 | + protectedBuffer.AsSpan(0, protectedBytesWritten), |
| 92 | + unprotectedBuffer, |
| 93 | + out var unprotectedBytesWritten); |
| 94 | + |
| 95 | + if (!unprotectSuccess) |
| 96 | + { |
| 97 | + throw new InvalidOperationException("TryUnprotect failed"); |
| 98 | + } |
| 99 | + } |
| 100 | + finally |
| 101 | + { |
| 102 | + ArrayPool<byte>.Shared.Return(unprotectedBuffer); |
| 103 | + } |
| 104 | + } |
| 105 | + finally |
| 106 | + { |
| 107 | + ArrayPool<byte>.Shared.Return(protectedBuffer); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments