|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Columns; |
| 3 | +using BenchmarkDotNet.Configs; |
| 4 | +using BenchmarkDotNet.Diagnosers; |
| 5 | +using BenchmarkDotNet.Environments; |
| 6 | +using BenchmarkDotNet.Exporters; |
| 7 | +using BenchmarkDotNet.Exporters.Csv; |
| 8 | +using BenchmarkDotNet.Exporters.Json; |
| 9 | +using BenchmarkDotNet.Jobs; |
| 10 | +using BenchmarkDotNet.Reports; |
| 11 | +using BenchmarkDotNet.Running; |
| 12 | +using BenchmarkDotNet.Validators; |
| 13 | +using Yubico.YubiKit.Core.Abstractions; |
| 14 | +using Yubico.YubiKit.Core.Devices; |
| 15 | +using Yubico.YubiKit.Core.Protocols.Fido.Hid; |
| 16 | +using Yubico.YubiKit.Core.Protocols.SmartCard.Apdu; |
| 17 | +using Yubico.YubiKit.Core.Sessions; |
| 18 | +using Yubico.YubiKit.Core.Transports.Hid; |
| 19 | +using Yubico.YubiKit.Core.Transports.SmartCard; |
| 20 | +using Yubico.YubiKit.Management; |
| 21 | + |
| 22 | +var config = YubiKitBenchmarkConfig.Create(); |
| 23 | +BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config); |
| 24 | + |
| 25 | +internal sealed class YubiKitBenchmarkConfig : ManualConfig |
| 26 | +{ |
| 27 | + private YubiKitBenchmarkConfig() |
| 28 | + { |
| 29 | + var runId = DateTimeOffset.UtcNow.ToString("yyyyMMdd-HHmmss"); |
| 30 | + ArtifactsPath = Path.Combine(FindRepoRoot(), "artifacts", "benchmarks", runId); |
| 31 | + |
| 32 | + AddJob(Job.ShortRun |
| 33 | + .WithRuntime(CoreRuntime.Core10_0) |
| 34 | + .WithId("net10-short")); |
| 35 | + |
| 36 | + AddColumnProvider(DefaultColumnProviders.Instance); |
| 37 | + AddDiagnoser(MemoryDiagnoser.Default); |
| 38 | + AddDiagnoser(ThreadingDiagnoser.Default); |
| 39 | + AddDiagnoser(ExceptionDiagnoser.Default); |
| 40 | + AddDiagnoser(new EventPipeProfiler(EventPipeProfile.CpuSampling)); |
| 41 | + AddExporter(MarkdownExporter.GitHub); |
| 42 | + AddExporter(HtmlExporter.Default); |
| 43 | + AddExporter(CsvExporter.Default); |
| 44 | + AddExporter(CsvMeasurementsExporter.Default); |
| 45 | + AddExporter(JsonExporter.Full); |
| 46 | + AddExporter(RPlotExporter.Default); |
| 47 | + AddValidator(JitOptimizationsValidator.FailOnError); |
| 48 | + AddLogger(DefaultConfig.Instance.GetLoggers().ToArray()); |
| 49 | + } |
| 50 | + |
| 51 | + public static IConfig Create() => new YubiKitBenchmarkConfig(); |
| 52 | + |
| 53 | + private static string FindRepoRoot() |
| 54 | + { |
| 55 | + var current = AppContext.BaseDirectory; |
| 56 | + while (!string.IsNullOrEmpty(current)) |
| 57 | + { |
| 58 | + if (Directory.Exists(Path.Combine(current, ".git"))) |
| 59 | + return current; |
| 60 | + |
| 61 | + current = Directory.GetParent(current)?.FullName; |
| 62 | + } |
| 63 | + |
| 64 | + return Directory.GetCurrentDirectory(); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +public abstract class YubiKeyHardwareBenchmarkBase |
| 69 | +{ |
| 70 | + protected IYubiKey Device { get; private set; } = null!; |
| 71 | + |
| 72 | + protected void SetupDevice(ConnectionType requiredConnection) |
| 73 | + { |
| 74 | + Device = FindDevice(requiredConnection).GetAwaiter().GetResult(); |
| 75 | + } |
| 76 | + |
| 77 | + [GlobalCleanup] |
| 78 | + public void Cleanup() => YubiKeyManager.Shutdown(); |
| 79 | + |
| 80 | + private static async Task<IYubiKey> FindDevice(ConnectionType requiredConnection) |
| 81 | + { |
| 82 | + var devices = await YubiKeyManager.FindAllAsync(ConnectionType.All, forceRescan: true) |
| 83 | + .ConfigureAwait(false); |
| 84 | + |
| 85 | + return devices.FirstOrDefault(device => device.SupportsConnection(requiredConnection)) |
| 86 | + ?? throw new InvalidOperationException( |
| 87 | + $"No connected YubiKey exposes {requiredConnection}. Connect the YubiKey 5.8 device before running benchmarks."); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +[MemoryDiagnoser] |
| 92 | +[ThreadingDiagnoser] |
| 93 | +[ExceptionDiagnoser] |
| 94 | +public class YubiKeyDiscoveryBenchmarks |
| 95 | +{ |
| 96 | + [GlobalSetup] |
| 97 | + public void Setup() |
| 98 | + { |
| 99 | + _ = YubiKeyManager.FindAllAsync(ConnectionType.All, forceRescan: true) |
| 100 | + .GetAwaiter() |
| 101 | + .GetResult(); |
| 102 | + } |
| 103 | + |
| 104 | + [GlobalCleanup] |
| 105 | + public void Cleanup() => YubiKeyManager.Shutdown(); |
| 106 | + |
| 107 | + [Benchmark(Baseline = true)] |
| 108 | + public async Task<int> CachedFindAll() |
| 109 | + { |
| 110 | + var devices = await YubiKeyManager.FindAllAsync(ConnectionType.All, forceRescan: false) |
| 111 | + .ConfigureAwait(false); |
| 112 | + return devices.Count; |
| 113 | + } |
| 114 | + |
| 115 | + [Benchmark] |
| 116 | + public async Task<int> ForcedRescanFindAll() |
| 117 | + { |
| 118 | + var devices = await YubiKeyManager.FindAllAsync(ConnectionType.All, forceRescan: true) |
| 119 | + .ConfigureAwait(false); |
| 120 | + return devices.Count; |
| 121 | + } |
| 122 | + |
| 123 | + [Benchmark] |
| 124 | + public async Task<int> ColdFindAll() |
| 125 | + { |
| 126 | + await YubiKeyManager.ShutdownAsync().ConfigureAwait(false); |
| 127 | + var devices = await YubiKeyManager.FindAllAsync(ConnectionType.All, forceRescan: false) |
| 128 | + .ConfigureAwait(false); |
| 129 | + return devices.Count; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +[MemoryDiagnoser] |
| 134 | +[ThreadingDiagnoser] |
| 135 | +[ExceptionDiagnoser] |
| 136 | +public class SmartCardManagementBenchmarks : YubiKeyHardwareBenchmarkBase |
| 137 | +{ |
| 138 | + [GlobalSetup] |
| 139 | + public void Setup() => SetupDevice(ConnectionType.SmartCard); |
| 140 | + |
| 141 | + [Benchmark(Baseline = true)] |
| 142 | + public async Task<bool> ConnectSmartCard() |
| 143 | + { |
| 144 | + await using var connection = await Device.ConnectAsync<ISmartCardConnection>().ConfigureAwait(false); |
| 145 | + return connection.SupportsExtendedApdu(); |
| 146 | + } |
| 147 | + |
| 148 | + [Benchmark] |
| 149 | + public async Task<int> SelectManagementOverSmartCard() |
| 150 | + { |
| 151 | + var connection = await Device.ConnectAsync<ISmartCardConnection>().ConfigureAwait(false); |
| 152 | + using var protocol = PcscProtocolFactory<ISmartCardConnection>.Create().Create(connection); |
| 153 | + var response = await protocol.SelectAsync(ApplicationIds.Management).ConfigureAwait(false); |
| 154 | + return response.Length; |
| 155 | + } |
| 156 | + |
| 157 | + [Benchmark] |
| 158 | + public async Task<int> GetDeviceInfoOverSmartCard() |
| 159 | + { |
| 160 | + await using var session = await Device.CreateManagementSessionAsync( |
| 161 | + preferredConnection: ConnectionType.SmartCard) |
| 162 | + .ConfigureAwait(false); |
| 163 | + |
| 164 | + var info = await session.GetDeviceInfoAsync().ConfigureAwait(false); |
| 165 | + return info.SerialNumber ?? 0; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +[MemoryDiagnoser] |
| 170 | +[ThreadingDiagnoser] |
| 171 | +[ExceptionDiagnoser] |
| 172 | +public class FidoHidManagementBenchmarks : YubiKeyHardwareBenchmarkBase |
| 173 | +{ |
| 174 | + [GlobalSetup] |
| 175 | + public void Setup() => SetupDevice(ConnectionType.HidFido); |
| 176 | + |
| 177 | + [Benchmark(Baseline = true)] |
| 178 | + public async Task<int> ConnectFidoHid() |
| 179 | + { |
| 180 | + await using var connection = await Device.ConnectAsync<IFidoHidConnection>().ConfigureAwait(false); |
| 181 | + return connection.PacketSize; |
| 182 | + } |
| 183 | + |
| 184 | + [Benchmark] |
| 185 | + public async Task<int> CreateManagementSessionOverFidoHid() |
| 186 | + { |
| 187 | + await using var session = await Device.CreateManagementSessionAsync( |
| 188 | + preferredConnection: ConnectionType.HidFido) |
| 189 | + .ConfigureAwait(false); |
| 190 | + |
| 191 | + return session.FirmwareVersion.Major; |
| 192 | + } |
| 193 | + |
| 194 | + [Benchmark] |
| 195 | + public async Task<int> GetDeviceInfoOverFidoHid() |
| 196 | + { |
| 197 | + await using var session = await Device.CreateManagementSessionAsync( |
| 198 | + preferredConnection: ConnectionType.HidFido) |
| 199 | + .ConfigureAwait(false); |
| 200 | + |
| 201 | + var info = await session.GetDeviceInfoAsync().ConfigureAwait(false); |
| 202 | + return info.SerialNumber ?? 0; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +[MemoryDiagnoser] |
| 207 | +[ThreadingDiagnoser] |
| 208 | +[ExceptionDiagnoser] |
| 209 | +public class OtpHidManagementBenchmarks : YubiKeyHardwareBenchmarkBase |
| 210 | +{ |
| 211 | + [GlobalSetup] |
| 212 | + public void Setup() => SetupDevice(ConnectionType.HidOtp); |
| 213 | + |
| 214 | + [Benchmark(Baseline = true)] |
| 215 | + public async Task<int> ConnectOtpHid() |
| 216 | + { |
| 217 | + await using var connection = await Device.ConnectAsync<IOtpHidConnection>().ConfigureAwait(false); |
| 218 | + return connection.FeatureReportSize; |
| 219 | + } |
| 220 | + |
| 221 | + [Benchmark] |
| 222 | + public async Task<int> CreateManagementSessionOverOtpHid() |
| 223 | + { |
| 224 | + await using var session = await Device.CreateManagementSessionAsync( |
| 225 | + preferredConnection: ConnectionType.HidOtp) |
| 226 | + .ConfigureAwait(false); |
| 227 | + |
| 228 | + return session.FirmwareVersion.Major; |
| 229 | + } |
| 230 | + |
| 231 | + [Benchmark] |
| 232 | + public async Task<int> GetDeviceInfoOverOtpHid() |
| 233 | + { |
| 234 | + await using var session = await Device.CreateManagementSessionAsync( |
| 235 | + preferredConnection: ConnectionType.HidOtp) |
| 236 | + .ConfigureAwait(false); |
| 237 | + |
| 238 | + var info = await session.GetDeviceInfoAsync().ConfigureAwait(false); |
| 239 | + return info.SerialNumber ?? 0; |
| 240 | + } |
| 241 | +} |
0 commit comments