From 13dc1368dde03cd1add4adbcadd002474916ff36 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 20 Jul 2026 02:31:10 +0100 Subject: [PATCH] Trace CultMesh local startup phases --- .../DirectoryMessagePackBackingStore.cs | 17 +++++++++++++++++ src/GameCult.Networking/CultNetLocal.cs | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/GameCult.Caching.MessagePack/DirectoryMessagePackBackingStore.cs b/src/GameCult.Caching.MessagePack/DirectoryMessagePackBackingStore.cs index 003b4863..fb058620 100644 --- a/src/GameCult.Caching.MessagePack/DirectoryMessagePackBackingStore.cs +++ b/src/GameCult.Caching.MessagePack/DirectoryMessagePackBackingStore.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Security.Cryptography; @@ -55,6 +56,18 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore /// public override void PullAll() { + var traceStartup = string.Equals( + Environment.GetEnvironmentVariable("CULTCACHE_TRACE_STARTUP_PHASES"), + "1", + StringComparison.Ordinal); + var startupPhase = Stopwatch.StartNew(); + void Trace(string phase) + { + if (traceStartup) + Console.WriteLine($"CultCache directory-pull phase {phase} took {startupPhase.Elapsed.TotalMilliseconds:0.###}ms."); + startupPhase.Restart(); + } + _recordDirectory.Refresh(); if (!File.Exists(_manifestFile.FullName) && !_recordDirectory.Exists) { @@ -64,6 +77,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore } var manifest = ReadManifest(); + Trace($"manifest records={manifest.Records.Length}"); _durableCatalog = manifest.SchemaCatalog; var reports = new List(); var loaded = new Dictionary(StringComparer.Ordinal); @@ -77,6 +91,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore .ToDictionary(group => group.Key, group => group.Last(), StringComparer.Ordinal); LoadIndexedRecords(manifest, loaded, reports); + Trace($"indexed-pages loaded={loaded.Count}"); } else { @@ -88,6 +103,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore .Where(record => !string.IsNullOrWhiteSpace(record.Key)) .GroupBy(record => record.Key, StringComparer.Ordinal) .ToDictionary(group => group.Key, group => ToIndexRecord(group.Last()), StringComparer.Ordinal); + Trace($"legacy-pages loaded={loaded.Count}"); } foreach (var pair in loaded) @@ -124,6 +140,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore SetLastSchemaMigrationReports(reports); IsDirty = !_dirtyKeys.IsEmpty || !_deletedKeys.IsEmpty; + Trace("publish"); } /// diff --git a/src/GameCult.Networking/CultNetLocal.cs b/src/GameCult.Networking/CultNetLocal.cs index a134cfcf..e884cd8d 100644 --- a/src/GameCult.Networking/CultNetLocal.cs +++ b/src/GameCult.Networking/CultNetLocal.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using GameCult.Caching; @@ -130,12 +131,28 @@ namespace GameCult.Networking } options ??= new CultNetHostOptions(); + var traceStartup = string.Equals( + Environment.GetEnvironmentVariable("CULTMESH_TRACE_STARTUP_PHASES"), + "1", + StringComparison.Ordinal); + var startupPhase = Stopwatch.StartNew(); + void Trace(string phase) + { + if (traceStartup) + Console.WriteLine($"CultMesh local-host phase {phase} took {startupPhase.Elapsed.TotalMilliseconds:0.###}ms."); + startupPhase.Restart(); + } + var cache = await CultCacheMessagePack.OpenAsync(cachePath, options.CacheOptions).ConfigureAwait(false); + Trace("cache-open"); var store = cache.BackingStores.OfType().FirstOrDefault(); var server = new Server(cache, options.Security ?? ServerSecurityOptions.Development()); + Trace("server"); var databaseOptions = options.DatabaseOptions ?? new CultNetDatabaseOptions(); var database = new CultNetDatabase(cache, databaseOptions); + Trace("database"); var databaseServer = new CultNetDatabaseServer(server, database, options.DatabaseServerOptions); + Trace("database-server"); options.ConfigureServer?.Invoke(server); var host = new CultNetHost(cache, store, server, database, databaseServer); @@ -143,6 +160,7 @@ namespace GameCult.Networking { host.Start(); } + Trace("server-start"); return host; }