1
+ using System . Text ;
2
+ using System . Text . RegularExpressions ;
3
+ using BenchmarkDotNet . Attributes ;
4
+ using StreamRegex . Extensions ;
5
+ using StreamRegex . Lib . DFA ;
6
+ using StreamRegex . Lib . NFA ;
7
+
8
+ namespace StreamRegex . Benchmarks ;
9
+ [ MemoryDiagnoser ]
10
+
11
+ // Tests checking for the string "racecar" that only occurs at the end of a very large file.
12
+ public class LargeFileBenchmarks
13
+ {
14
+ private readonly Regex _compiled ;
15
+ private const string Pattern = "racecar" ;
16
+ private Stream _stream = new MemoryStream ( ) ;
17
+ private const int _paddingLength = 1024 * 1024 * 100 ; // 100 MB
18
+ private StringBuilder _testData = new StringBuilder ( ) ;
19
+ public LargeFileBenchmarks ( )
20
+ {
21
+ while ( _testData . Length < _paddingLength )
22
+ {
23
+ _testData . Append ( Enumerable . Repeat ( "a" , 1024 ) ) ;
24
+ }
25
+
26
+ _testData . Append ( Pattern ) ;
27
+ _compiled = new Regex ( Pattern , RegexOptions . Compiled ) ;
28
+ }
29
+
30
+ [ IterationSetup ]
31
+ public void IterationSetup ( )
32
+ {
33
+ _stream = new MemoryStream ( Encoding . UTF8 . GetBytes ( _testData . ToString ( ) ) ) ;
34
+ }
35
+
36
+ [ IterationCleanup ]
37
+ public void IterationCleanup ( )
38
+ {
39
+ _stream . Dispose ( ) ;
40
+ }
41
+
42
+ [ BenchmarkCategory ( "Regex" ) ]
43
+ [ Benchmark ]
44
+ public void CompiledRegex ( )
45
+ {
46
+ var content = new StreamReader ( _stream ) . ReadToEnd ( ) ;
47
+ var match = _compiled . Match ( content ) ;
48
+ if ( ! match . Success || match . Index != _paddingLength )
49
+ {
50
+ throw new Exception ( $ "The regex didn't match { match . Index } .") ;
51
+ }
52
+ }
53
+
54
+ [ BenchmarkCategory ( "Regex" ) ]
55
+ [ Benchmark ]
56
+ public void RegexExtension ( )
57
+ {
58
+ var content = new StreamReader ( _stream ) ;
59
+ var match = _compiled . GetFirstMatch ( content ) ;
60
+ if ( ! match . Success || match . Index != _paddingLength )
61
+ {
62
+ throw new Exception ( $ "The regex didn't match { match . Index } .") ;
63
+ }
64
+ }
65
+
66
+ [ BenchmarkCategory ( "Contains" ) ]
67
+ [ Benchmark ( Baseline = true ) ]
68
+
69
+ public void SimpleString ( )
70
+ {
71
+ var match = new StreamReader ( _stream ) . ReadToEnd ( ) . IndexOf ( "racecar" ) ;
72
+ if ( match != _paddingLength )
73
+ {
74
+ throw new Exception ( $ "The regex didn't match { match } .") ;
75
+ }
76
+ }
77
+
78
+ [ BenchmarkCategory ( "Contains" ) ]
79
+ [ Benchmark ]
80
+ public void StringExtension ( )
81
+ {
82
+ var content = new StreamReader ( _stream ) ;
83
+ var match = content . IndexOf ( "racecar" ) ;
84
+ if ( match != _paddingLength )
85
+ {
86
+ throw new Exception ( $ "The regex didn't match { match } .") ;
87
+ }
88
+ }
89
+
90
+
91
+ // [Benchmark]
92
+ public void StateMachine ( )
93
+ {
94
+ var stateMachine = StateMachineFactory . CreateStateMachine ( Pattern ) ;
95
+ if ( stateMachine . GetFirstMatchPosition ( _stream ) == - 1 )
96
+ {
97
+ throw new Exception ( "The regex didn't match" ) ;
98
+ }
99
+ }
100
+
101
+ // [Benchmark]
102
+ public void NFAStateMachine ( )
103
+ {
104
+ var stateMachine = NfaStateMachineFactory . CreateStateMachine ( Pattern ) ;
105
+ var match = stateMachine . Match ( _stream ) ;
106
+ if ( match is null )
107
+ {
108
+ throw new Exception ( "The regex didn't match" ) ;
109
+ }
110
+ }
111
+ }
0 commit comments