Skip to content

Commit 67b61d4

Browse files
Fix benchmark app to use BenchmarkDotNet properly...
1 parent c8eb2d6 commit 67b61d4

File tree

4 files changed

+150
-117
lines changed

4 files changed

+150
-117
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Text.RegularExpressions;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Engines;
4+
5+
namespace Serilog.Enrichers.Sensitive.Tests.Benchmark;
6+
7+
[SimpleJob(RunStrategy.Throughput, warmupCount: 1)]
8+
public class BenchmarkCompiledIbanRegex
9+
{
10+
private const string MaskValue = "***MASKED***";
11+
private const string IbanInput = "NL02ABNA0123456789";
12+
13+
private readonly Regex IbanRegex = new("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}");
14+
15+
private readonly Regex IbanRegexCompiled =
16+
new("[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}", RegexOptions.Compiled);
17+
18+
19+
[Params(10000)] public int N;
20+
21+
[Benchmark(Baseline = true)]
22+
public string IbanRegexReplace()
23+
{
24+
var result = IbanRegex.Replace(IbanInput, MaskValue);
25+
26+
return result;
27+
}
28+
29+
[Benchmark(Baseline = false)]
30+
public string IbanRegexCompiledReplace()
31+
{
32+
var result = IbanRegexCompiled.Replace(IbanInput, MaskValue);
33+
34+
return result;
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Text.RegularExpressions;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Engines;
4+
5+
namespace Serilog.Enrichers.Sensitive.Tests.Benchmark
6+
{
7+
[SimpleJob(RunStrategy.Throughput, warmupCount: 1)]
8+
public class BenchmarkCompiledEmailRegex
9+
{
10+
private const string MaskValue = "***MASKED***";
11+
private const string EmailInput = "[email protected]";
12+
13+
private readonly Regex EmailRegex =
14+
new(
15+
"(?:[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])+)\\])");
16+
17+
private readonly Regex EmailRegexCompiled = new(
18+
"(?:[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])+)\\])",
19+
RegexOptions.Compiled);
20+
21+
[Params(10000)] public int N;
22+
23+
[Benchmark(Baseline = true)]
24+
public string EmailRegexReplace()
25+
{
26+
var result = EmailRegex.Replace(EmailInput, MaskValue);
27+
28+
return result;
29+
}
30+
31+
[Benchmark(Baseline = false)]
32+
public string EmailRegexCompiledReplace()
33+
{
34+
var result = EmailRegexCompiled.Replace(EmailInput, MaskValue);
35+
36+
return result;
37+
}
38+
}
39+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Text.RegularExpressions;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Engines;
4+
5+
namespace Serilog.Enrichers.Sensitive.Tests.Benchmark
6+
{
7+
[SimpleJob(RunStrategy.Throughput, warmupCount: 1)]
8+
public class CreditCardMarkingBenchmarks
9+
{
10+
private const string CreditCardInput = "4111111111111111";
11+
private const string MaskValue = "***MASKED***";
12+
13+
private readonly Regex CreditCardFullReplaceRegex =
14+
new(@"(?<toMask>\d{4}(?<sep>[ -]?)\d{4}\k<sep>*\d{4}\k<sep>*\d{4})",
15+
RegexOptions.IgnoreCase);
16+
17+
private readonly Regex CreditCardFullReplaceRegexCompiled =
18+
new(@"(?<toMask>\d{4}(?<sep>[ -]?)\d{4}\k<sep>*\d{4}\k<sep>*\d{4})",
19+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
20+
21+
private readonly Regex CreditCardPartialReplaceRegex =
22+
new(@"(?<leading4>\d{4}(?<sep>[ -]?))(?<toMask>\d{4}\k<sep>*\d{2})(?<trailing6>\d{2}\k<sep>*\d{4})",
23+
RegexOptions.IgnoreCase);
24+
25+
private readonly Regex CreditCardPartialReplaceRegexCompiled =
26+
new(@"(?<leading4>\d{4}(?<sep>[ -]?))(?<toMask>\d{4}\k<sep>*\d{2})(?<trailing6>\d{2}\k<sep>*\d{4})",
27+
RegexOptions.IgnoreCase | RegexOptions.Compiled);
28+
29+
[Params(10000)] public int N;
30+
31+
[Benchmark]
32+
public string CreditCardPartialRegexReplace()
33+
{
34+
string result = null;
35+
for (var i = 0; i < 10000; i++)
36+
result = CreditCardPartialReplaceRegex.Replace(CreditCardInput, MaskValue);
37+
38+
return result;
39+
}
40+
41+
[Benchmark]
42+
public string CreditCardRegexPartialCompiledReplace()
43+
{
44+
string result = null;
45+
for (var i = 0; i < 10000; i++)
46+
result = CreditCardPartialReplaceRegex.Replace(CreditCardInput, MaskValue);
47+
48+
return result;
49+
}
50+
51+
[Benchmark]
52+
public string CreditCardFullRegexReplace()
53+
{
54+
string result = null;
55+
for (var i = 0; i < 10000; i++)
56+
result = CreditCardFullReplaceRegex.Replace(CreditCardInput, MaskValue);
57+
58+
return result;
59+
}
60+
61+
[Benchmark]
62+
public string CreditCardRegexFullCompiledReplace()
63+
{
64+
string result = null;
65+
for (var i = 0; i < 10000; i++)
66+
result = CreditCardFullReplaceRegex.Replace(CreditCardInput, MaskValue);
67+
68+
return result;
69+
}
70+
}
71+
}
Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,14 @@
1-
using System.Text.RegularExpressions;
2-
using BenchmarkDotNet.Attributes;
3-
using BenchmarkDotNet.Running;
1+
using BenchmarkDotNet.Running;
42

53
namespace Serilog.Enrichers.Sensitive.Tests.Benchmark
64
{
75
internal class Program
86
{
9-
private static void Main()
7+
private static void Main(string[] args)
108
{
11-
BenchmarkRunner.Run<BenchmarkCompiledRegex>();
9+
BenchmarkRunner.Run<BenchmarkCompiledEmailRegex>();
10+
BenchmarkRunner.Run<BenchmarkCompiledIbanRegex>();
1211
BenchmarkRunner.Run<CreditCardMarkingBenchmarks>();
1312
}
1413
}
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-
}
12714
}

0 commit comments

Comments
 (0)