Skip to content

Commit

Permalink
Refactor benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
gfs committed Mar 29, 2022
1 parent 5873c3b commit fce9cdb
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 20 deletions.
102 changes: 102 additions & 0 deletions StreamRegex.Benchmarks/BufferSizeBenchmarks.cs
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public void IterationCleanup()
[Params("175MB.txt")]
public string TestFileName { get; set; }

[Benchmark(Baseline = true)]
[BenchmarkCategory("Regex")]
[Benchmark]
public void CompiledRegex()
{
var content = new StreamReader(_stream).ReadToEnd();
Expand All @@ -41,7 +42,43 @@ public void CompiledRegex()
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()
{
Expand All @@ -62,14 +99,4 @@ public void NFAStateMachine()
throw new Exception("The regex didn't match");
}
}

[Benchmark]
public void RegexExtension()
{
var content = new StreamReader(_stream);
if (!_compiled.IsMatch(content))
{
throw new Exception($"The regex didn't match.");
}
}
}
2 changes: 1 addition & 1 deletion StreamRegex.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using StreamRegex.Lib.DFA;
using StreamRegex.Lib.NFA;

// var summary = BenchmarkRunner.Run<PerformanceVsStandard>();
var summary = BenchmarkRunner.Run<PerformanceVsStandard>();
// NFATest();
void ExtensionsTest()
{
Expand Down
111 changes: 111 additions & 0 deletions StreamRegex.Benchmarks/VariousPositions.cs
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");
}
}
}
10 changes: 6 additions & 4 deletions StreamRegex.Extensions/SlidingBufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public static class SlidingBufferExtensions
/// <param name="comparison"></param>
/// <param name="options"></param>
/// <returns></returns>
public static bool Contains(this Stream toMatch, string target, StringComparison comparison, SlidingBufferOptions? options = null)
public static bool Contains(this Stream toMatch, string target, StringComparison? comparison = null, SlidingBufferOptions? options = null)
{
return new StreamReader(toMatch).IsMatch(contentChunk => contentChunk.Contains(target, comparison), options);
return new StreamReader(toMatch).Contains(target, comparison, options);
}

/// <summary>
Expand All @@ -23,9 +23,11 @@ public static bool Contains(this Stream toMatch, string target, StringComparison
/// <param name="comparison"></param>
/// <param name="options"></param>
/// <returns></returns>
public static bool Contains(this StreamReader toMatch, string target, StringComparison comparison, SlidingBufferOptions? options = null)
public static bool Contains(this StreamReader toMatch, string target, StringComparison? comparison = null, SlidingBufferOptions? options = null)
{
return toMatch.IsMatch(contentChunk => contentChunk.Contains(target, comparison), options);
return comparison is { } notNullComparison ?
toMatch.IsMatch(contentChunk => contentChunk.Contains(target, notNullComparison), options) :
toMatch.IsMatch(contentChunk => contentChunk.Contains(target), options);
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions StreamRegex.Extensions/SlidingBufferMatchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace StreamRegex.Extensions;

/// <summary>
/// A collection holding unique <see cref="SlidingBufferMatch"/>.
/// A collection holding unique <see cref="SlidingBufferMatch"/> for a single resource. The matches are Records which are deduplicated automatically.
/// </summary>
public class SlidingBufferMatchCollection<T> : IEnumerable<T> where T : SlidingBufferMatch
{
Expand Down Expand Up @@ -46,15 +46,15 @@ public void AddMatches(IEnumerable<T> matchCollection)
}

/// <summary>
/// Update the index position of the matches in this collection by a specific offset and return the collection.
/// Update the index position of the matches in this collection by a specific offset and return the modified collection. Does not make a copy.
/// </summary>
/// <param name="offset">The offset to apply</param>
/// <returns>This collection with the matches modified</returns>
public SlidingBufferMatchCollection<T> WithOffset(long offset)
{
foreach (var SlidingBufferMatch in _collection)
foreach (var slidingBufferMatch in _collection)
{
SlidingBufferMatch.Index += offset;
slidingBufferMatch.Index += offset;
}

return this;
Expand Down

0 comments on commit fce9cdb

Please sign in to comment.