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