Reformat / cleanup
All checks were successful
NuGet Publish / build-and-pack (push) Successful in 46s
NuGet Publish / publish-to-gitea (push) Successful in 56s

This commit is contained in:
Joseph Doherty
2026-02-21 08:10:36 -05:00
parent 4c6aaa5a3f
commit a70d8befae
176 changed files with 50555 additions and 49587 deletions

View File

@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using Microsoft.Extensions.Logging;
using Serilog.Context;
@@ -8,7 +7,7 @@ namespace ZB.MOM.WW.CBDD.Tests.Benchmark;
public class ManualBenchmark
{
private static StringBuilder _log = new();
private static readonly StringBuilder _log = new();
private static void Log(ILogger logger, string message = "")
{
@@ -16,11 +15,11 @@ public class ManualBenchmark
_log.AppendLine(message);
}
/// <summary>
/// Tests run.
/// </summary>
/// <param name="logger">Logger for benchmark progress and results.</param>
public static void Run(ILogger logger)
/// <summary>
/// Tests run.
/// </summary>
/// <param name="logger">Logger for benchmark progress and results.</param>
public static void Run(ILogger logger)
{
using var _ = LogContext.PushProperty("Benchmark", nameof(ManualBenchmark));
_log.Clear();
@@ -60,10 +59,7 @@ public class ManualBenchmark
try
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < 1000; i++)
{
readBench.DocumentDb_FindById();
}
for (var i = 0; i < 1000; i++) readBench.DocumentDb_FindById();
sw.Stop();
readByIdMs = sw.ElapsedMilliseconds;
Log(logger, $" CBDD FindById x1000: {readByIdMs} ms ({(double)readByIdMs / 1000:F3} ms/op)");
@@ -101,14 +97,11 @@ public class ManualBenchmark
Log(logger, $"FindById x1000: {readByIdMs} ms");
Log(logger, $"Single Insert: {singleInsertMs} ms");
var artifactsDir = Path.Combine(AppContext.BaseDirectory, "BenchmarkDotNet.Artifacts", "results");
if (!Directory.Exists(artifactsDir))
{
Directory.CreateDirectory(artifactsDir);
}
string artifactsDir = Path.Combine(AppContext.BaseDirectory, "BenchmarkDotNet.Artifacts", "results");
if (!Directory.Exists(artifactsDir)) Directory.CreateDirectory(artifactsDir);
var filePath = Path.Combine(artifactsDir, "manual_report.txt");
string filePath = Path.Combine(artifactsDir, "manual_report.txt");
File.WriteAllText(filePath, _log.ToString());
logger.LogInformation("Report saved to: {FilePath}", filePath);
}
}
}

View File

@@ -1,6 +1,6 @@
using System.Text;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using ZB.MOM.WW.CBDD.Bson;
using ZB.MOM.WW.CBDD.Core.Collections;
using ZB.MOM.WW.CBDD.Core.Compression;
@@ -16,28 +16,29 @@ namespace ZB.MOM.WW.CBDD.Tests.Benchmark;
[JsonExporterAttribute.Full]
public class MixedWorkloadBenchmarks
{
private readonly List<ObjectId> _activeIds = [];
private DocumentCollection<Person> _collection = null!;
private string _dbPath = string.Empty;
private int _nextValueSeed;
private StorageEngine _storage = null!;
private BenchmarkTransactionHolder _transactionHolder = null!;
private string _walPath = string.Empty;
/// <summary>
/// Gets or sets whether periodic online compaction is enabled.
/// Gets or sets whether periodic online compaction is enabled.
/// </summary>
[Params(false, true)]
public bool PeriodicCompaction { get; set; }
/// <summary>
/// Gets or sets the number of operations per benchmark iteration.
/// Gets or sets the number of operations per benchmark iteration.
/// </summary>
[Params(800)]
public int Operations { get; set; }
private string _dbPath = string.Empty;
private string _walPath = string.Empty;
private StorageEngine _storage = null!;
private BenchmarkTransactionHolder _transactionHolder = null!;
private DocumentCollection<Person> _collection = null!;
private readonly List<ObjectId> _activeIds = [];
private int _nextValueSeed;
/// <summary>
/// Prepares benchmark storage and seed data for each iteration.
/// Prepares benchmark storage and seed data for each iteration.
/// </summary>
[IterationSetup]
public void Setup()
@@ -71,7 +72,7 @@ public class MixedWorkloadBenchmarks
}
/// <summary>
/// Cleans up benchmark resources for each iteration.
/// Cleans up benchmark resources for each iteration.
/// </summary>
[IterationCleanup]
public void Cleanup()
@@ -84,7 +85,7 @@ public class MixedWorkloadBenchmarks
}
/// <summary>
/// Benchmarks a mixed insert/update/delete workload.
/// Benchmarks a mixed insert/update/delete workload.
/// </summary>
[Benchmark(Baseline = true)]
[BenchmarkCategory("MixedWorkload")]
@@ -94,7 +95,7 @@ public class MixedWorkloadBenchmarks
for (var i = 1; i <= Operations; i++)
{
var mode = i % 5;
int mode = i % 5;
if (mode is 0 or 1)
{
var id = _collection.Insert(CreatePerson(_nextValueSeed++));
@@ -104,7 +105,7 @@ public class MixedWorkloadBenchmarks
{
if (_activeIds.Count > 0)
{
var idx = random.Next(_activeIds.Count);
int idx = random.Next(_activeIds.Count);
var id = _activeIds[idx];
var current = _collection.FindById(id);
if (current != null)
@@ -119,20 +120,16 @@ public class MixedWorkloadBenchmarks
{
if (_activeIds.Count > 100)
{
var idx = random.Next(_activeIds.Count);
int idx = random.Next(_activeIds.Count);
var id = _activeIds[idx];
_collection.Delete(id);
_activeIds.RemoveAt(idx);
}
}
if (i % 50 == 0)
{
_transactionHolder.CommitAndReset();
}
if (i % 50 == 0) _transactionHolder.CommitAndReset();
if (PeriodicCompaction && i % 200 == 0)
{
_storage.RunOnlineCompactionPass(new CompactionOptions
{
OnlineMode = true,
@@ -141,7 +138,6 @@ public class MixedWorkloadBenchmarks
MaxOnlineDuration = TimeSpan.FromMilliseconds(120),
EnableTailTruncation = true
});
}
}
_transactionHolder.CommitAndReset();
@@ -155,7 +151,7 @@ public class MixedWorkloadBenchmarks
Id = ObjectId.NewObjectId(),
FirstName = $"First_{seed}",
LastName = $"Last_{seed}",
Age = 18 + (seed % 60),
Age = 18 + seed % 60,
Bio = BuildPayload(seed),
CreatedAt = DateTime.UnixEpoch.AddSeconds(seed),
Balance = seed,
@@ -170,7 +166,7 @@ public class MixedWorkloadBenchmarks
private static string BuildPayload(int seed)
{
var builder = new System.Text.StringBuilder(1800);
var builder = new StringBuilder(1800);
for (var i = 0; i < 64; i++)
{
builder.Append("mixed-");
@@ -182,4 +178,4 @@ public class MixedWorkloadBenchmarks
return builder.ToString();
}
}
}

View File

@@ -1,4 +1,5 @@
using System.IO.Compression;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.CBDD.Bson;
@@ -14,21 +15,21 @@ internal static class PerformanceGateSmoke
private const int CompressionDocumentCount = 1_500;
/// <summary>
/// Runs the performance gate smoke probes and writes a report.
/// Runs the performance gate smoke probes and writes a report.
/// </summary>
/// <param name="logger">The logger.</param>
public static void Run(ILogger logger)
{
var compaction = RunCompactionProbe();
var compressionOff = RunCompressionGcProbe(enableCompression: false);
var compressionOn = RunCompressionGcProbe(enableCompression: true);
var compressionOff = RunCompressionGcProbe(false);
var compressionOn = RunCompressionGcProbe(true);
var report = new PerformanceGateReport(
DateTimeOffset.UtcNow,
compaction,
compressionOff,
compressionOn);
var reportPath = WriteReport(report);
string reportPath = WriteReport(report);
logger.LogInformation("Performance gate smoke report written to {ReportPath}", reportPath);
@@ -52,8 +53,8 @@ internal static class PerformanceGateSmoke
private static CompactionProbeResult RunCompactionProbe()
{
var dbPath = NewDbPath("gate_compaction");
var walPath = Path.ChangeExtension(dbPath, ".wal");
string dbPath = NewDbPath("gate_compaction");
string walPath = Path.ChangeExtension(dbPath, ".wal");
try
{
@@ -62,18 +63,12 @@ internal static class PerformanceGateSmoke
var collection = new DocumentCollection<Person>(storage, transactionHolder, new PersonMapper());
var ids = new List<ObjectId>(CompactionDocumentCount);
for (var i = 0; i < CompactionDocumentCount; i++)
{
ids.Add(collection.Insert(CreatePerson(i, includeLargeBio: true)));
}
for (var i = 0; i < CompactionDocumentCount; i++) ids.Add(collection.Insert(CreatePerson(i, true)));
transactionHolder.CommitAndReset();
storage.Checkpoint();
for (var i = 0; i < ids.Count; i += 3)
{
collection.Delete(ids[i]);
}
for (var i = 0; i < ids.Count; i += 3) collection.Delete(ids[i]);
for (var i = 0; i < ids.Count; i += 5)
{
@@ -117,8 +112,8 @@ internal static class PerformanceGateSmoke
private static CompressionGcProbeResult RunCompressionGcProbe(bool enableCompression)
{
var dbPath = NewDbPath(enableCompression ? "gate_gc_on" : "gate_gc_off");
var walPath = Path.ChangeExtension(dbPath, ".wal");
string dbPath = NewDbPath(enableCompression ? "gate_gc_on" : "gate_gc_off");
string walPath = Path.ChangeExtension(dbPath, ".wal");
var compressionOptions = enableCompression
? new CompressionOptions
{
@@ -140,16 +135,13 @@ internal static class PerformanceGateSmoke
GC.WaitForPendingFinalizers();
GC.Collect();
var g0Before = GC.CollectionCount(0);
var g1Before = GC.CollectionCount(1);
var g2Before = GC.CollectionCount(2);
var allocBefore = GC.GetTotalAllocatedBytes(true);
int g0Before = GC.CollectionCount(0);
int g1Before = GC.CollectionCount(1);
int g2Before = GC.CollectionCount(2);
long allocBefore = GC.GetTotalAllocatedBytes(true);
var ids = new ObjectId[CompressionDocumentCount];
for (var i = 0; i < CompressionDocumentCount; i++)
{
ids[i] = collection.Insert(CreatePerson(i, includeLargeBio: true));
}
for (var i = 0; i < CompressionDocumentCount; i++) ids[i] = collection.Insert(CreatePerson(i, true));
transactionHolder.CommitAndReset();
@@ -166,17 +158,17 @@ internal static class PerformanceGateSmoke
transactionHolder.CommitAndReset();
var readCount = collection.FindAll().Count();
int readCount = collection.FindAll().Count();
transactionHolder.CommitAndReset();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var g0After = GC.CollectionCount(0);
var g1After = GC.CollectionCount(1);
var g2After = GC.CollectionCount(2);
var allocAfter = GC.GetTotalAllocatedBytes(true);
int g0After = GC.CollectionCount(0);
int g1After = GC.CollectionCount(1);
int g2After = GC.CollectionCount(2);
long allocAfter = GC.GetTotalAllocatedBytes(true);
return new CompressionGcProbeResult(
enableCompression,
@@ -198,11 +190,11 @@ internal static class PerformanceGateSmoke
private static string WriteReport(PerformanceGateReport report)
{
var outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "BenchmarkDotNet.Artifacts", "results");
string outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "BenchmarkDotNet.Artifacts", "results");
Directory.CreateDirectory(outputDirectory);
var reportPath = Path.Combine(outputDirectory, "PerformanceGateSmoke-report.json");
var json = JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true });
string reportPath = Path.Combine(outputDirectory, "PerformanceGateSmoke-report.json");
string json = JsonSerializer.Serialize(report, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(reportPath, json);
return reportPath;
}
@@ -214,7 +206,7 @@ internal static class PerformanceGateSmoke
Id = ObjectId.NewObjectId(),
FirstName = $"First_{i}",
LastName = $"Last_{i}",
Age = 20 + (i % 50),
Age = 20 + i % 50,
Bio = includeLargeBio ? BuildBio(i) : $"bio-{i}",
CreatedAt = DateTime.UnixEpoch.AddMinutes(i),
Balance = 100 + i,
@@ -239,7 +231,7 @@ internal static class PerformanceGateSmoke
private static string BuildBio(int seed)
{
var builder = new System.Text.StringBuilder(4500);
var builder = new StringBuilder(4500);
for (var i = 0; i < 150; i++)
{
builder.Append("bio-");
@@ -253,14 +245,13 @@ internal static class PerformanceGateSmoke
}
private static string NewDbPath(string prefix)
=> Path.Combine(Path.GetTempPath(), $"{prefix}_{Guid.NewGuid():N}.db");
{
return Path.Combine(Path.GetTempPath(), $"{prefix}_{Guid.NewGuid():N}.db");
}
private static void TryDelete(string path)
{
if (File.Exists(path))
{
File.Delete(path);
}
if (File.Exists(path)) File.Delete(path);
}
private sealed record PerformanceGateReport(
@@ -284,4 +275,4 @@ internal static class PerformanceGateSmoke
int Gen1Delta,
int Gen2Delta,
long AllocatedBytesDelta);
}
}