This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from fastwildcard/11-benchmarks
Reworked benchmarks to behave more like real world
- Loading branch information
Showing
14 changed files
with
263 additions
and
126 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
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
Binary file added
BIN
+6.07 KB
reports/FastWildcard.Performance.Benchmarks.LibraryComparison-report.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+28.2 KB
reports/FastWildcard.Performance.Benchmarks.LibraryComparison-report.xlsx
Binary file not shown.
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
10 changes: 6 additions & 4 deletions
10
tests/FastWildcard.Performance.Tests/FastWildcard.Performance.Tests.csproj
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
65 changes: 65 additions & 0 deletions
65
tests/FastWildcard.Performance.Tests/IterationBuilderTests.cs
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,65 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using AutoFixture.Xunit2; | ||
using FastWildcard.Performance.Benchmarks; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace FastWildcard.Performance.Tests | ||
{ | ||
public class IterationBuilderTests | ||
{ | ||
[Theory, AutoData] | ||
public void BuildPattern_InvalidParameter_ThrowsException( | ||
[Range(-100, 0)] int patternLength, | ||
[Range(-100, -1)] int singleCharacterCount, | ||
[Range(-100, -1)] int multiCharacterCount | ||
) | ||
{ | ||
Action action = () => IterationBuilder.BuildPattern(patternLength, singleCharacterCount, multiCharacterCount); | ||
|
||
action.Should().Throw<ArgumentOutOfRangeException>(); | ||
} | ||
|
||
[Theory, AutoData] | ||
public void BuildPattern_OfLength_ReturnsStringWithLength( | ||
[Range(1, 100)] int patternLength, | ||
[Range(0, 100)] int singleCharacterCount, | ||
[Range(0, 100)] int multiCharacterCount | ||
) | ||
{ | ||
var (result, newSingleCount, newMultiCount) = IterationBuilder.BuildPattern(patternLength, singleCharacterCount, multiCharacterCount); | ||
|
||
result.Should().NotBeNullOrWhiteSpace() | ||
.And.HaveLength(patternLength) | ||
.And.Match(s => s.Count(ch => ch == '?') == newSingleCount) | ||
.And.Match(s => s.Count(ch => ch == '*') == newMultiCount); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
[InlineData(" ")] | ||
public void BuildTestString_InvalidParameter_ThrowsException(string pattern) | ||
{ | ||
Action action = () => IterationBuilder.BuildTestString(pattern); | ||
|
||
action.Should().Throw<ArgumentException>(); | ||
} | ||
|
||
[Theory, AutoData] | ||
public void BuildTestString_OfLength_ReturnsStringWithLength( | ||
[Range(1, 100)] int patternLength, | ||
[Range(0, 100)] int singleCharacterCount, | ||
[Range(0, 100)] int multiCharacterCount | ||
) | ||
{ | ||
var (pattern, _, _) = IterationBuilder.BuildPattern(patternLength, singleCharacterCount, multiCharacterCount); | ||
|
||
var result = IterationBuilder.BuildTestString(pattern); | ||
|
||
result.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
tests/FastWildcard.Performance/Benchmarks/IterationBuilder.cs
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,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace FastWildcard.Performance.Benchmarks | ||
{ | ||
public static class IterationBuilder | ||
{ | ||
public static (string result, int newSingleCount, int newMultiCount) BuildPattern(int patternLength, int singleCountSuggestion, int multiCountSuggestion) | ||
{ | ||
const int wildcardPercentage = 10; | ||
|
||
if (patternLength <= 0) | ||
throw new ArgumentOutOfRangeException(nameof(patternLength)); | ||
if (singleCountSuggestion < 0) | ||
throw new ArgumentOutOfRangeException(nameof(singleCountSuggestion)); | ||
if (multiCountSuggestion < 0) | ||
throw new ArgumentOutOfRangeException(nameof(multiCountSuggestion)); | ||
|
||
var adjustedSingleCount = Math.Min(singleCountSuggestion, patternLength) / wildcardPercentage; | ||
var adjustedMultiCount = Math.Min(multiCountSuggestion, patternLength) / wildcardPercentage; | ||
|
||
var patternBuilder = new StringBuilder(new Bogus.Randomizer().AlphaNumeric(patternLength)); | ||
|
||
var random = new Random(); | ||
|
||
var singleCharacterLocations = new List<int>(); | ||
if (singleCountSuggestion > 0) | ||
{ | ||
singleCharacterLocations = Enumerable.Range(0, adjustedSingleCount) | ||
.Select(x => random.Next(0, patternLength)) | ||
.ToList(); | ||
singleCharacterLocations.ForEach(x => patternBuilder[x] = '?'); | ||
} | ||
|
||
var multiCharacterLocations = new List<int>(); | ||
if (multiCountSuggestion > 0) | ||
{ | ||
multiCharacterLocations = Enumerable.Range(0, adjustedMultiCount) | ||
.Select(x => random.Next(0, patternLength)) | ||
.Where(x => !singleCharacterLocations.Contains(x)) | ||
.ToList(); | ||
multiCharacterLocations.ForEach(x => patternBuilder[x] = '*'); | ||
} | ||
|
||
var pattern = patternBuilder.ToString(); | ||
return (pattern, singleCharacterLocations.Count, multiCharacterLocations.Count); | ||
} | ||
|
||
public static string BuildTestString(string pattern) | ||
{ | ||
if (string.IsNullOrWhiteSpace(pattern)) | ||
throw new ArgumentException("Value cannot be null or whitespace.", nameof(pattern)); | ||
|
||
var randomizer = new Bogus.Randomizer(); | ||
|
||
const int noMatchPercentage = 50; | ||
if (WeightedMatch(noMatchPercentage)) | ||
{ | ||
return randomizer.String(); | ||
} | ||
|
||
const int charMatchPercentage = 95; | ||
var strBuilder = new StringBuilder(pattern.Length * 2); | ||
foreach (var patternCh in pattern) | ||
{ | ||
if (patternCh == '?') | ||
{ | ||
strBuilder.Append(randomizer.AlphaNumeric(1)); | ||
} | ||
else if (patternCh == '*') | ||
{ | ||
strBuilder.Append(randomizer.Words()); | ||
} | ||
else | ||
{ | ||
if (WeightedMatch(charMatchPercentage)) | ||
{ | ||
strBuilder.Append(patternCh); | ||
} | ||
} | ||
} | ||
|
||
var str = strBuilder.ToString(); | ||
return str; | ||
|
||
bool WeightedMatch(int percentage) | ||
{ | ||
return randomizer.Bool(percentage / 100f); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.