Skip to content

Commit

Permalink
Reworked testing for all versions of the ExpireAfter operator, to i…
Browse files Browse the repository at this point in the history
…mprove functional coverage and cover various existing defects. (#821)
  • Loading branch information
JakenVeina committed Jan 31, 2024
1 parent d6d748e commit 52f1b14
Show file tree
Hide file tree
Showing 9 changed files with 2,590 additions and 290 deletions.
62 changes: 62 additions & 0 deletions src/DynamicData.Benchmarks/Cache/ExpireAfter_Cache_ForSource.cs
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 src/DynamicData.Benchmarks/Cache/ExpireAfter_Cache_ForStream.cs
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; }
}
}
53 changes: 53 additions & 0 deletions src/DynamicData.Benchmarks/List/ExpireAfter_List.cs
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;
}

0 comments on commit 52f1b14

Please sign in to comment.