|
1 |
| -using System.Text.RegularExpressions; |
2 |
| -using BenchmarkDotNet.Attributes; |
3 |
| -using BenchmarkDotNet.Running; |
| 1 | +using BenchmarkDotNet.Running; |
4 | 2 |
|
5 | 3 | namespace Serilog.Enrichers.Sensitive.Tests.Benchmark
|
6 | 4 | {
|
7 | 5 | internal class Program
|
8 | 6 | {
|
9 |
| - private static void Main() |
| 7 | + private static void Main(string[] args) |
10 | 8 | {
|
11 |
| - BenchmarkRunner.Run<BenchmarkCompiledRegex>(); |
| 9 | + BenchmarkRunner.Run<BenchmarkCompiledEmailRegex>(); |
| 10 | + BenchmarkRunner.Run<BenchmarkCompiledIbanRegex>(); |
12 | 11 | BenchmarkRunner.Run<CreditCardMarkingBenchmarks>();
|
13 | 12 | }
|
14 | 13 | }
|
15 |
| - |
16 |
| - public class CreditCardMarkingBenchmarks |
17 |
| - { |
18 |
| - private readonly Regex CreditCardPartialReplaceRegex = |
19 |
| - new Regex(@"(?<leading4>\d{4}(?<sep>[ -]?))(?<toMask>\d{4}\k<sep>*\d{2})(?<trailing6>\d{2}\k<sep>*\d{4})", |
20 |
| - RegexOptions.IgnoreCase); |
21 |
| - private readonly Regex CreditCardFullReplaceRegex = |
22 |
| - new Regex(@"(?<toMask>\d{4}(?<sep>[ -]?)\d{4}\k<sep>*\d{4}\k<sep>*\d{4})", |
23 |
| - RegexOptions.IgnoreCase); |
24 |
| - private readonly Regex CreditCardPartialReplaceRegexCompiled = |
25 |
| - new Regex(@"(?<leading4>\d{4}(?<sep>[ -]?))(?<toMask>\d{4}\k<sep>*\d{2})(?<trailing6>\d{2}\k<sep>*\d{4})", |
26 |
| - RegexOptions.IgnoreCase | RegexOptions.Compiled); |
27 |
| - private readonly Regex CreditCardFullReplaceRegexCompiled = |
28 |
| - new Regex(@"(?<toMask>\d{4}(?<sep>[ -]?)\d{4}\k<sep>*\d{4}\k<sep>*\d{4})", |
29 |
| - RegexOptions.IgnoreCase | RegexOptions.Compiled); |
30 |
| - |
31 |
| - private const string CreditCardInput = "4111111111111111"; |
32 |
| - private const string MaskValue = "***MASKED***"; |
33 |
| - |
34 |
| - [Benchmark] |
35 |
| - public string CreditCardPartialRegexReplace() |
36 |
| - { |
37 |
| - string result = null; |
38 |
| - for (int i = 0; i < 10000; i++) |
39 |
| - result = CreditCardPartialReplaceRegex.Replace(CreditCardInput, MaskValue); |
40 |
| - |
41 |
| - return result; |
42 |
| - } |
43 |
| - |
44 |
| - [Benchmark] |
45 |
| - public string CreditCardRegexPartialCompiledReplace() |
46 |
| - { |
47 |
| - string result = null; |
48 |
| - for (int i = 0; i < 10000; i++) |
49 |
| - result = CreditCardPartialReplaceRegex.Replace(CreditCardInput, MaskValue); |
50 |
| - |
51 |
| - return result; |
52 |
| - } |
53 |
| - |
54 |
| - [Benchmark] |
55 |
| - public string CreditCardFullRegexReplace() |
56 |
| - { |
57 |
| - string result = null; |
58 |
| - for (int i = 0; i < 10000; i++) |
59 |
| - result = CreditCardFullReplaceRegex.Replace(CreditCardInput, MaskValue); |
60 |
| - |
61 |
| - return result; |
62 |
| - } |
63 |
| - |
64 |
| - [Benchmark] |
65 |
| - public string CreditCardRegexFullCompiledReplace() |
66 |
| - { |
67 |
| - string result = null; |
68 |
| - for (int i = 0; i < 10000; i++) |
69 |
| - result = CreditCardFullReplaceRegex.Replace(CreditCardInput, MaskValue); |
70 |
| - |
71 |
| - return result; |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - |
76 |
| - public class BenchmarkCompiledRegex |
77 |
| - { |
78 |
| - private readonly Regex EmailRegex = new Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"); |
79 |
| - private readonly Regex EmailRegexCompiled = new Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])", RegexOptions.Compiled); |
80 |
| - private readonly Regex IbanRegex = new Regex("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}"); |
81 |
| - private readonly Regex IbanRegexCompiled = new Regex("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}", RegexOptions.Compiled); |
82 |
| - |
83 |
| - private const string MaskValue = "***MASKED***"; |
84 |
| - private const string EmailInput = "[email protected]"; |
85 |
| - private const string IbanInput = "NL02ABNA0123456789"; |
86 |
| - |
87 |
| - [Benchmark] |
88 |
| - public string EmailRegexReplace() |
89 |
| - { |
90 |
| - string result = null; |
91 |
| - for (int i = 0; i < 10000; i++) |
92 |
| - result = EmailRegex.Replace(EmailInput, MaskValue); |
93 |
| - |
94 |
| - return result; |
95 |
| - } |
96 |
| - |
97 |
| - [Benchmark] |
98 |
| - public string EmailRegexCompiledReplace() |
99 |
| - { |
100 |
| - string result = null; |
101 |
| - for (int i = 0; i < 10000; i++) |
102 |
| - result = EmailRegexCompiled.Replace(EmailInput, MaskValue); |
103 |
| - |
104 |
| - return result; |
105 |
| - } |
106 |
| - |
107 |
| - [Benchmark] |
108 |
| - public string IbanRegexReplace() |
109 |
| - { |
110 |
| - string result = null; |
111 |
| - for (int i = 0; i < 10000; i++) |
112 |
| - result = IbanRegex.Replace(IbanInput, MaskValue); |
113 |
| - |
114 |
| - return result; |
115 |
| - } |
116 |
| - |
117 |
| - [Benchmark] |
118 |
| - public string IbanRegexCompiledReplace() |
119 |
| - { |
120 |
| - string result = null; |
121 |
| - for (int i = 0; i < 10000; i++) |
122 |
| - result = IbanRegexCompiled.Replace(IbanInput, MaskValue); |
123 |
| - |
124 |
| - return result; |
125 |
| - } |
126 |
| - } |
127 | 14 | }
|
0 commit comments