-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
262 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System.Text.RegularExpressions; | ||
using BenchmarkDotNet.Attributes; | ||
using StreamRegex.Extensions; | ||
using StreamRegex.Lib.DFA; | ||
using StreamRegex.Lib.NFA; | ||
|
||
namespace StreamRegex.Benchmarks; | ||
[MemoryDiagnoser] | ||
public class BufferSizeBenchmarks | ||
{ | ||
private readonly Regex _compiled; | ||
private const string Pattern = "racecar"; | ||
private Stream _stream = new MemoryStream(); | ||
public BufferSizeBenchmarks() | ||
{ | ||
_compiled = new Regex(Pattern, RegexOptions.Compiled); | ||
} | ||
|
||
[IterationSetup] | ||
public void IterationSetup() | ||
{ | ||
_stream = File.OpenRead(TestFileName); | ||
} | ||
|
||
[IterationCleanup] | ||
public void IterationCleanup() | ||
{ | ||
_stream.Dispose(); | ||
} | ||
|
||
//[Params("TargetStart.txt","TargetMiddle.txt","TargetEnd.txt")] | ||
[Params("175MB.txt")] | ||
public string TestFileName { get; set; } | ||
|
||
[BenchmarkCategory("Regex")] | ||
[Benchmark] | ||
public void CompiledRegex() | ||
{ | ||
var content = new StreamReader(_stream).ReadToEnd(); | ||
if (!_compiled.IsMatch(content)) | ||
{ | ||
throw new Exception($"The regex didn't match."); | ||
} | ||
} | ||
|
||
[BenchmarkCategory("Regex")] | ||
[Benchmark] | ||
public void RegexExtension() | ||
{ | ||
var content = new StreamReader(_stream); | ||
if (!_compiled.IsMatch(content)) | ||
{ | ||
throw new Exception($"The regex didn't match."); | ||
} | ||
} | ||
|
||
[BenchmarkCategory("Contains")] | ||
[Benchmark(Baseline = true)] | ||
|
||
public void SimpleString() | ||
{ | ||
var match = new StreamReader(_stream).ReadToEnd().IndexOf("racecar"); | ||
if (match == -1) | ||
{ | ||
throw new Exception($"The regex didn't match."); | ||
} | ||
} | ||
|
||
[BenchmarkCategory("Contains")] | ||
[Benchmark] | ||
public void StringExtension() | ||
{ | ||
var content = new StreamReader(_stream); | ||
var match = content.IndexOf("racecar"); | ||
if (match == -1) | ||
{ | ||
throw new Exception($"The regex didn't match."); | ||
} | ||
} | ||
|
||
|
||
// [Benchmark] | ||
public void StateMachine() | ||
{ | ||
var stateMachine = StateMachineFactory.CreateStateMachine(Pattern); | ||
if (stateMachine.GetFirstMatchPosition(_stream) == -1) | ||
{ | ||
throw new Exception("The regex didn't match"); | ||
} | ||
} | ||
|
||
// [Benchmark] | ||
public void NFAStateMachine() | ||
{ | ||
var stateMachine = NfaStateMachineFactory.CreateStateMachine(Pattern); | ||
var match = stateMachine.Match(_stream); | ||
if (match is null) | ||
{ | ||
throw new Exception("The regex didn't match"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using BenchmarkDotNet.Attributes; | ||
using StreamRegex.Extensions; | ||
using StreamRegex.Lib.DFA; | ||
using StreamRegex.Lib.NFA; | ||
|
||
namespace StreamRegex.Benchmarks; | ||
[MemoryDiagnoser] | ||
|
||
// Tests checking for the string "racecar" that only occurs at the end of a very large file. | ||
public class LargeFileBenchmarks | ||
{ | ||
private readonly Regex _compiled; | ||
private const string Pattern = "racecar"; | ||
private Stream _stream = new MemoryStream(); | ||
private const int _paddingLength = 1024 * 1024 * 100; // 100 MB | ||
private StringBuilder _testData = new StringBuilder(); | ||
public LargeFileBenchmarks() | ||
{ | ||
while (_testData.Length < _paddingLength) | ||
{ | ||
_testData.Append(Enumerable.Repeat("a", 1024)); | ||
} | ||
|
||
_testData.Append(Pattern); | ||
_compiled = new Regex(Pattern, RegexOptions.Compiled); | ||
} | ||
|
||
[IterationSetup] | ||
public void IterationSetup() | ||
{ | ||
_stream = new MemoryStream(Encoding.UTF8.GetBytes(_testData.ToString())); | ||
} | ||
|
||
[IterationCleanup] | ||
public void IterationCleanup() | ||
{ | ||
_stream.Dispose(); | ||
} | ||
|
||
[BenchmarkCategory("Regex")] | ||
[Benchmark] | ||
public void CompiledRegex() | ||
{ | ||
var content = new StreamReader(_stream).ReadToEnd(); | ||
var match = _compiled.Match(content); | ||
if (!match.Success || match.Index != _paddingLength) | ||
{ | ||
throw new Exception($"The regex didn't match {match.Index}."); | ||
} | ||
} | ||
|
||
[BenchmarkCategory("Regex")] | ||
[Benchmark] | ||
public void RegexExtension() | ||
{ | ||
var content = new StreamReader(_stream); | ||
var match = _compiled.GetFirstMatch(content); | ||
if (!match.Success || match.Index != _paddingLength) | ||
{ | ||
throw new Exception($"The regex didn't match {match.Index}."); | ||
} | ||
} | ||
|
||
[BenchmarkCategory("Contains")] | ||
[Benchmark(Baseline = true)] | ||
|
||
public void SimpleString() | ||
{ | ||
var match = new StreamReader(_stream).ReadToEnd().IndexOf("racecar"); | ||
if (match != _paddingLength) | ||
{ | ||
throw new Exception($"The regex didn't match {match}."); | ||
} | ||
} | ||
|
||
[BenchmarkCategory("Contains")] | ||
[Benchmark] | ||
public void StringExtension() | ||
{ | ||
var content = new StreamReader(_stream); | ||
var match = content.IndexOf("racecar"); | ||
if (match != _paddingLength) | ||
{ | ||
throw new Exception($"The regex didn't match {match}."); | ||
} | ||
} | ||
|
||
|
||
// [Benchmark] | ||
public void StateMachine() | ||
{ | ||
var stateMachine = StateMachineFactory.CreateStateMachine(Pattern); | ||
if (stateMachine.GetFirstMatchPosition(_stream) == -1) | ||
{ | ||
throw new Exception("The regex didn't match"); | ||
} | ||
} | ||
|
||
// [Benchmark] | ||
public void NFAStateMachine() | ||
{ | ||
var stateMachine = NfaStateMachineFactory.CreateStateMachine(Pattern); | ||
var match = stateMachine.Match(_stream); | ||
if (match is null) | ||
{ | ||
throw new Exception("The regex didn't match"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters