generated from Nexus-Mods/NexusMods.App.Template
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #89 from Nexus-Mods/observeall
Add `ObserveAll` tests and benchmarks
- Loading branch information
Showing
9 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
benchmarks/NexusMods.MnemonicDB.Benchmarks/Benchmarks/ObserveAllBenchmarks.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,57 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Engines; | ||
using NexusMods.MnemonicDB.Abstractions.TxFunctions; | ||
using NexusMods.MnemonicDB.TestModel; | ||
|
||
namespace NexusMods.MnemonicDB.Benchmarks.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
[SimpleJob(runStrategy: RunStrategy.Monitoring, warmupCount: 1)] | ||
public class ObserveAllBenchmarks : ABenchmark | ||
{ | ||
private string[] _names = []; | ||
|
||
[Params(100, 1_000, 10_000)] | ||
public int N { get; set; } | ||
|
||
[GlobalSetup] | ||
public async ValueTask GlobalSetup() | ||
{ | ||
await InitializeAsync(); | ||
_names = Enumerable.Range(start: 0, count: N).Select(i => $"Loadout {i}").ToArray(); | ||
} | ||
|
||
[IterationCleanup] | ||
public void IterationCleanup() | ||
{ | ||
using var tx = Connection.BeginTransaction(); | ||
|
||
foreach (var entity in Loadout.All(Connection.Db)) | ||
{ | ||
tx.Delete(entity, recursive: false); | ||
} | ||
|
||
tx.Commit().GetAwaiter().GetResult(); | ||
} | ||
|
||
[Benchmark] | ||
public async ValueTask ObserveAll() | ||
{ | ||
using var disposable = Loadout.ObserveAll(Connection).Subscribe(_ => { }); | ||
|
||
using var tx = Connection.BeginTransaction(); | ||
|
||
foreach (var name in _names) | ||
{ | ||
_ = new Loadout.New(tx) | ||
{ | ||
Name = name, | ||
}; | ||
} | ||
|
||
await tx.Commit(); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using DynamicData; | ||
using NexusMods.MnemonicDB.Abstractions.TxFunctions; | ||
using NexusMods.MnemonicDB.TestModel; | ||
|
||
namespace NexusMods.MnemonicDB.Tests; | ||
|
||
public class ObservableTests : AMnemonicDBTest | ||
{ | ||
public ObservableTests(IServiceProvider provider) : base(provider) { } | ||
|
||
[Fact] | ||
public async Task TestObserveAll() | ||
{ | ||
using var disposable = Loadout | ||
.ObserveAll(Connection) | ||
.Transform(loadout => loadout.Name) | ||
.Bind(out var list) | ||
.Subscribe(); | ||
|
||
list.Should().BeEmpty(); | ||
|
||
await Add("Loadout 1"); | ||
|
||
list.Should().ContainSingle(static name => name.Equals("Loadout 1")); | ||
|
||
await Add("Loadout 2"); | ||
|
||
list.Should().HaveCount(2).And.ContainInOrder(["Loadout 1", "Loadout 2"]); | ||
|
||
await Add("Loadout 3", "Loadout 4"); | ||
|
||
list.Should().HaveCount(4).And.ContainInOrder(["Loadout 1", "Loadout 2", "Loadout 3", "Loadout 4"]); | ||
|
||
await Delete("Loadout 2"); | ||
|
||
list.Should().HaveCount(3).And.ContainInOrder(["Loadout 1", "Loadout 3", "Loadout 4"]); | ||
|
||
await Delete("Loadout 1", "Loadout 4"); | ||
|
||
list.Should().ContainSingle(static name => name.Equals("Loadout 3")); | ||
|
||
await Delete("Loadout 3"); | ||
|
||
list.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public async Task StressTest() | ||
{ | ||
using var disposable = Loadout | ||
.ObserveAll(Connection) | ||
.Transform(loadout => loadout.Name) | ||
.Bind(out var list) | ||
.Subscribe(); | ||
|
||
var stress = Enumerable.Range(start: 0, count: 100).Select(i => $"Loadout {i}").ToArray(); | ||
await Add(stress); | ||
|
||
list.Should().ContainInOrder(stress); | ||
|
||
await Delete(stress); | ||
|
||
list.Should().BeEmpty(); | ||
} | ||
|
||
private async ValueTask Add(params string[] names) | ||
{ | ||
using var tx = Connection.BeginTransaction(); | ||
|
||
foreach (var name in names) | ||
{ | ||
_ = new Loadout.New(tx) | ||
{ | ||
Name = name, | ||
}; | ||
} | ||
|
||
|
||
await tx.Commit(); | ||
} | ||
|
||
private async ValueTask Delete(params string[] names) | ||
{ | ||
var db = Connection.Db; | ||
using var tx = Connection.BeginTransaction(); | ||
|
||
foreach (var name in names) | ||
{ | ||
var entities = Loadout.FindByName(db, name); | ||
entities.Should().ContainSingle(); | ||
|
||
tx.Delete(entities.First(), recursive: false); | ||
} | ||
|
||
await tx.Commit(); | ||
} | ||
} |