|
2 | 2 | using BenchmarkDotNet.Order; |
3 | 3 | #pragma warning disable CA1822 |
4 | 4 |
|
5 | | -namespace NuExt.System.Benchmarks |
| 5 | +namespace NuExt.System.Benchmarks; |
| 6 | + |
| 7 | +[Config(typeof(MultipleRuntimesConfig))] |
| 8 | +[MemoryDiagnoser] |
| 9 | +[Orderer(SummaryOrderPolicy.Declared)] |
| 10 | +[RankColumn] |
| 11 | +public class EnumParserBenchmark |
6 | 12 | { |
7 | | - [Config(typeof(MultipleRuntimesConfig))] |
8 | | - [MemoryDiagnoser] |
9 | | - [Orderer(SummaryOrderPolicy.Declared)] |
10 | | - [RankColumn] |
11 | | - public class EnumParserBenchmark |
12 | | - { |
13 | | - private const string ValidKey = nameof(ConsoleKey.Applications); |
14 | | - private const ConsoleKey ValidKeyValue = ConsoleKey.Applications; |
15 | | - private const string InvalidKey = "App"; |
| 13 | + private const string ValidKey = nameof(ConsoleKey.Applications); |
| 14 | + private const ConsoleKey ValidKeyValue = ConsoleKey.Applications; |
| 15 | + private const string InvalidKey = "App"; |
16 | 16 |
|
17 | | - //GetValue/Parse Valid |
| 17 | + //GetValue/Parse Valid |
18 | 18 |
|
19 | | - [Benchmark] |
20 | | - public void EnumHelper_GetValue_Valid() |
21 | | - { |
22 | | - var key = EnumHelper<ConsoleKey>.GetValue(ValidKey); |
23 | | - ValidateResult(key, ValidKeyValue); |
24 | | - } |
| 19 | + [Benchmark] |
| 20 | + public void EnumHelper_GetValue_Valid() |
| 21 | + { |
| 22 | + var key = EnumHelper<ConsoleKey>.GetValue(ValidKey); |
| 23 | + ValidateResult(key, ValidKeyValue); |
| 24 | + } |
25 | 25 |
|
26 | | - [Benchmark(Baseline = true)] |
27 | | - public void Enum_Parse_Valid() |
28 | | - { |
29 | | - var key = (ConsoleKey)Enum.Parse(typeof(ConsoleKey), ValidKey); |
30 | | - ValidateResult(key, ValidKeyValue); |
31 | | - } |
| 26 | + [Benchmark(Baseline = true)] |
| 27 | + public void Enum_Parse_Valid() |
| 28 | + { |
| 29 | + var key = (ConsoleKey)Enum.Parse(typeof(ConsoleKey), ValidKey); |
| 30 | + ValidateResult(key, ValidKeyValue); |
| 31 | + } |
32 | 32 |
|
33 | | - //TryGetValue/TryParse Valid |
| 33 | + //TryGetValue/TryParse Valid |
34 | 34 |
|
35 | | - [Benchmark] |
36 | | - public void EnumHelper_TryGetValue_Valid() |
37 | | - { |
38 | | - EnumHelper<ConsoleKey>.TryGetValue(ValidKey, out var key); |
39 | | - ValidateResult(key, ValidKeyValue); |
40 | | - } |
| 35 | + [Benchmark] |
| 36 | + public void EnumHelper_TryGetValue_Valid() |
| 37 | + { |
| 38 | + EnumHelper<ConsoleKey>.TryGetValue(ValidKey, out var key); |
| 39 | + ValidateResult(key, ValidKeyValue); |
| 40 | + } |
41 | 41 |
|
42 | | - [Benchmark] |
43 | | - public void Enum_TryParse_Valid() |
44 | | - { |
45 | | - Enum.TryParse<ConsoleKey>(ValidKey, true, out var key); |
46 | | - ValidateResult(key, ValidKeyValue); |
47 | | - } |
| 42 | + [Benchmark] |
| 43 | + public void Enum_TryParse_Valid() |
| 44 | + { |
| 45 | + Enum.TryParse<ConsoleKey>(ValidKey, true, out var key); |
| 46 | + ValidateResult(key, ValidKeyValue); |
| 47 | + } |
48 | 48 |
|
49 | | - //GetValue/Parse Invalid |
| 49 | + //GetValue/Parse Invalid |
50 | 50 |
|
51 | | - [Benchmark] |
52 | | - public void EnumHelper_GetValue_Invalid() |
| 51 | + [Benchmark] |
| 52 | + public void EnumHelper_GetValue_Invalid() |
| 53 | + { |
| 54 | + try |
53 | 55 | { |
54 | | - try |
55 | | - { |
56 | | - var key = EnumHelper<ConsoleKey>.GetValue(InvalidKey); |
57 | | - ValidateResult(key, default); |
58 | | - } |
59 | | - catch (Exception) |
60 | | - { |
61 | | - // Expecting an exception as the key is invalid |
62 | | - } |
| 56 | + var key = EnumHelper<ConsoleKey>.GetValue(InvalidKey); |
| 57 | + ValidateResult(key, default); |
63 | 58 | } |
64 | | - |
65 | | - [Benchmark] |
66 | | - public void Enum_Parse_Invalid() |
| 59 | + catch (Exception) |
67 | 60 | { |
68 | | - try |
69 | | - { |
70 | | - var key = (ConsoleKey)Enum.Parse(typeof(ConsoleKey), InvalidKey, true); |
71 | | - ValidateResult(key, default); |
72 | | - } |
73 | | - catch (Exception) |
74 | | - { |
75 | | - // Expecting an exception as the key is invalid |
76 | | - } |
| 61 | + // Expecting an exception as the key is invalid |
77 | 62 | } |
| 63 | + } |
78 | 64 |
|
79 | | - //TryGetValue/TryParse Invalid |
80 | | - |
81 | | - [Benchmark] |
82 | | - public void EnumHelper_TryGetValue_Invalid() |
| 65 | + [Benchmark] |
| 66 | + public void Enum_Parse_Invalid() |
| 67 | + { |
| 68 | + try |
83 | 69 | { |
84 | | - EnumHelper<ConsoleKey>.TryGetValue(InvalidKey, out var key); |
| 70 | + var key = (ConsoleKey)Enum.Parse(typeof(ConsoleKey), InvalidKey, true); |
85 | 71 | ValidateResult(key, default); |
86 | 72 | } |
87 | | - |
88 | | - [Benchmark] |
89 | | - public void Enum_TryParse_Invalid() |
| 73 | + catch (Exception) |
90 | 74 | { |
91 | | - Enum.TryParse<ConsoleKey>(InvalidKey, true, out var key); |
92 | | - ValidateResult(key, default); |
| 75 | + // Expecting an exception as the key is invalid |
93 | 76 | } |
| 77 | + } |
94 | 78 |
|
95 | | - private static void ValidateResult<TEnum>(TEnum actual, TEnum expected) where TEnum : struct, Enum |
| 79 | + //TryGetValue/TryParse Invalid |
| 80 | + |
| 81 | + [Benchmark] |
| 82 | + public void EnumHelper_TryGetValue_Invalid() |
| 83 | + { |
| 84 | + EnumHelper<ConsoleKey>.TryGetValue(InvalidKey, out var key); |
| 85 | + ValidateResult(key, default); |
| 86 | + } |
| 87 | + |
| 88 | + [Benchmark] |
| 89 | + public void Enum_TryParse_Invalid() |
| 90 | + { |
| 91 | + Enum.TryParse<ConsoleKey>(InvalidKey, true, out var key); |
| 92 | + ValidateResult(key, default); |
| 93 | + } |
| 94 | + |
| 95 | + private static void ValidateResult<TEnum>(TEnum actual, TEnum expected) where TEnum : struct, Enum |
| 96 | + { |
| 97 | + if (!EqualityComparer<TEnum>.Default.Equals(actual, expected)) |
96 | 98 | { |
97 | | - if (!EqualityComparer<TEnum>.Default.Equals(actual, expected)) |
98 | | - { |
99 | | - throw new InvalidOperationException($"Expected {expected}, but got {actual}"); |
100 | | - } |
| 99 | + throw new InvalidOperationException($"Expected {expected}, but got {actual}"); |
101 | 100 | } |
102 | 101 | } |
103 | 102 | } |
0 commit comments