-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked testing for all versions of the
ExpireAfter
operator, to i…
…mprove functional coverage and cover various existing defects. (#821)
- Loading branch information
1 parent
d6d748e
commit 52f1b14
Showing
9 changed files
with
2,590 additions
and
290 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/DynamicData.Benchmarks/Cache/ExpireAfter_Cache_ForSource.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,62 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace DynamicData.Benchmarks.Cache; | ||
|
||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class ExpireAfter_Cache_ForSource | ||
{ | ||
public ExpireAfter_Cache_ForSource() | ||
=> _items = Enumerable | ||
.Range(1, 1_000) | ||
.Select(id => new Item() | ||
{ | ||
Id = id | ||
}) | ||
.ToArray(); | ||
|
||
[Benchmark] | ||
[Arguments(1, 0)] | ||
[Arguments(1, 1)] | ||
[Arguments(10, 0)] | ||
[Arguments(10, 1)] | ||
[Arguments(10, 10)] | ||
[Arguments(100, 0)] | ||
[Arguments(100, 1)] | ||
[Arguments(100, 10)] | ||
[Arguments(100, 100)] | ||
[Arguments(1_000, 0)] | ||
[Arguments(1_000, 1)] | ||
[Arguments(1_000, 10)] | ||
[Arguments(1_000, 100)] | ||
[Arguments(1_000, 1_000)] | ||
public void AddsRemovesAndFinalization(int addCount, int removeCount) | ||
{ | ||
using var source = new SourceCache<Item, int>(static item => item.Id); | ||
|
||
using var subscription = source | ||
.ExpireAfter( | ||
timeSelector: static _ => TimeSpan.FromMinutes(60), | ||
interval: null) | ||
.Subscribe(); | ||
|
||
for (var i = 0; i < addCount; ++i) | ||
source.AddOrUpdate(_items[i]); | ||
|
||
for (var i = 0; i < removeCount; ++i) | ||
source.RemoveKey(_items[i].Id); | ||
|
||
subscription.Dispose(); | ||
} | ||
|
||
private readonly IReadOnlyList<Item> _items; | ||
|
||
private sealed class Item | ||
{ | ||
public int Id { get; init; } | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/DynamicData.Benchmarks/Cache/ExpireAfter_Cache_ForStream.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,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reactive.Subjects; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace DynamicData.Benchmarks.Cache; | ||
|
||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class ExpireAfter_Cache_ForStream | ||
{ | ||
public ExpireAfter_Cache_ForStream() | ||
{ | ||
var additions = new List<IChangeSet<Item, int>>(capacity: 1_000); | ||
var removals = new List<IChangeSet<Item, int>>(capacity: 1_000); | ||
|
||
for (var id = 1; id <= 1_000; ++id) | ||
{ | ||
var item = new Item() | ||
{ | ||
Id = id | ||
}; | ||
|
||
additions.Add(new ChangeSet<Item, int>(capacity: 1) | ||
{ | ||
new(reason: ChangeReason.Add, | ||
key: id, | ||
current: item) | ||
}); | ||
|
||
removals.Add(new ChangeSet<Item, int>() | ||
{ | ||
new(reason: ChangeReason.Remove, | ||
key: item.Id, | ||
current: item) | ||
}); | ||
} | ||
|
||
_additions = additions; | ||
_removals = removals; | ||
} | ||
|
||
[Benchmark] | ||
[Arguments(1, 0)] | ||
[Arguments(1, 1)] | ||
[Arguments(10, 0)] | ||
[Arguments(10, 1)] | ||
[Arguments(10, 10)] | ||
[Arguments(100, 0)] | ||
[Arguments(100, 1)] | ||
[Arguments(100, 10)] | ||
[Arguments(100, 100)] | ||
[Arguments(1_000, 0)] | ||
[Arguments(1_000, 1)] | ||
[Arguments(1_000, 10)] | ||
[Arguments(1_000, 100)] | ||
[Arguments(1_000, 1_000)] | ||
public void AddsRemovesAndFinalization(int addCount, int removeCount) | ||
{ | ||
using var source = new Subject<IChangeSet<Item, int>>(); | ||
|
||
using var subscription = source | ||
.ExpireAfter(static _ => TimeSpan.FromMinutes(60)) | ||
.Subscribe(); | ||
|
||
var itemLifetime = TimeSpan.FromMilliseconds(1); | ||
|
||
var itemsToRemove = new List<Item>(); | ||
|
||
for (var i = 0; i < addCount; ++i) | ||
source.OnNext(_additions[i]); | ||
|
||
for (var i = 0; i < removeCount; ++i) | ||
source.OnNext(_removals[i]); | ||
|
||
subscription.Dispose(); | ||
} | ||
|
||
private readonly IReadOnlyList<IChangeSet<Item, int>> _additions; | ||
private readonly IReadOnlyList<IChangeSet<Item, int>> _removals; | ||
|
||
private sealed class Item | ||
{ | ||
public int Id { get; init; } | ||
} | ||
} |
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,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace DynamicData.Benchmarks.List; | ||
|
||
[MemoryDiagnoser] | ||
[MarkdownExporterAttribute.GitHub] | ||
public class ExpireAfter_List | ||
{ | ||
public ExpireAfter_List() | ||
=> _items = Enumerable | ||
.Range(0, 1_000) | ||
.Select(_ => new object()) | ||
.ToArray(); | ||
|
||
[Benchmark] | ||
[Arguments(1, 0)] | ||
[Arguments(1, 1)] | ||
[Arguments(10, 0)] | ||
[Arguments(10, 1)] | ||
[Arguments(10, 10)] | ||
[Arguments(100, 0)] | ||
[Arguments(100, 1)] | ||
[Arguments(100, 10)] | ||
[Arguments(100, 100)] | ||
[Arguments(1_000, 0)] | ||
[Arguments(1_000, 1)] | ||
[Arguments(1_000, 10)] | ||
[Arguments(1_000, 100)] | ||
[Arguments(1_000, 1_000)] | ||
public void AddsRemovesAndFinalization(int addCount, int removeCount) | ||
{ | ||
using var source = new SourceList<object>(); | ||
|
||
using var subscription = source | ||
.ExpireAfter(static _ => TimeSpan.FromMinutes(60), pollingInterval: null) | ||
.Subscribe(); | ||
|
||
for (var i = 0; i < addCount; ++i) | ||
source.Add(_items[i]); | ||
|
||
var targetCount = addCount - removeCount; | ||
while (source.Count > targetCount) | ||
source.RemoveAt(0); | ||
|
||
subscription.Dispose(); | ||
} | ||
|
||
private readonly IReadOnlyList<object> _items; | ||
} |
Oops, something went wrong.