Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpireAfter Testing Rework #821

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading