-
Notifications
You must be signed in to change notification settings - Fork 7
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
5 changed files
with
150 additions
and
7 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
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
95 changes: 95 additions & 0 deletions
95
tests/QuerySpecification.Benchmarks/Benchmarks/LikeValidatorBenchmark.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,95 @@ | ||
namespace QuerySpecification.Benchmarks; | ||
|
||
// Benchmarks measuring the in-memory Like evaluator implementations. | ||
[MemoryDiagnoser] | ||
public class LikeValidatorBenchmark | ||
{ | ||
public record Customer(int Id, string FirstName, string? LastName); | ||
private class CustomerSpec : Specification<Customer> | ||
{ | ||
public CustomerSpec() | ||
{ | ||
Query | ||
.Like(x => x.FirstName, "%xx%", 1) | ||
.Like(x => x.LastName, "%xy%", 2) | ||
.Like(x => x.LastName, "%xz%", 2); | ||
} | ||
} | ||
|
||
private CustomerSpec _specification = default!; | ||
private Customer _customer = default!; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_specification = new CustomerSpec(); | ||
_customer = new(1, "axxa", "axza"); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public bool ValidateOption1() | ||
{ | ||
var entity = _customer; | ||
|
||
var groups = _specification.LikeExpressions.GroupBy(x => x.Group); | ||
|
||
foreach (var likeGroup in groups) | ||
{ | ||
if (likeGroup.Any(c => c.KeySelectorFunc(entity)?.Like(c.Pattern) ?? false) == false) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
[Benchmark] | ||
public bool ValidateOption2() | ||
{ | ||
var entity = _customer; | ||
|
||
var groups = _specification.LikeExpressions.GroupBy(x => x.Group).ToList(); | ||
|
||
foreach (var group in groups) | ||
{ | ||
var match = false; | ||
foreach (var like in group) | ||
{ | ||
if (like.KeySelectorFunc(entity)?.Like(like.Pattern) ?? false) | ||
{ | ||
match = true; | ||
break; | ||
} | ||
} | ||
|
||
if (match is false) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
[Benchmark] | ||
public bool ValidateOption3() | ||
{ | ||
var entity = _customer; | ||
|
||
var groups = _specification.LikeExpressions.GroupBy(x => x.Group); | ||
|
||
foreach (var group in groups) | ||
{ | ||
var match = false; | ||
foreach (var like in group) | ||
{ | ||
if (like.KeySelectorFunc(entity)?.Like(like.Pattern) ?? false) | ||
{ | ||
match = true; | ||
break; | ||
} | ||
} | ||
|
||
if (match is false) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |