perf(metrics): cache MxEventFamily names for per-event metric tags

WorkerClient.StageWorkerEvent and StreamEvents called Enum.ToString per
event (per subscriber on the stream path). MxEventFamilyNames serves the
contract-fixed names from a frozen lookup, falling back to ToString for
undefined values so exported tag values are unchanged.
This commit is contained in:
Joseph Doherty
2026-10-22 06:30:57 -04:00
parent 6ae5ed905f
commit 68d6b5fd6f
4 changed files with 71 additions and 2 deletions
@@ -0,0 +1,41 @@
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Metrics;
namespace ZB.MOM.WW.MxGateway.Tests.Metrics;
public sealed class MxEventFamilyNamesTests
{
/// <summary>
/// Every defined family maps to exactly the name <see cref="Enum.ToString()"/> produces, so
/// switching the per-event metric tags to the cached lookup changes no exported tag value.
/// </summary>
[Fact]
public void Get_ForEveryDefinedFamily_MatchesEnumToString()
{
foreach (MxEventFamily family in Enum.GetValues<MxEventFamily>())
{
Assert.Equal(family.ToString(), MxEventFamilyNames.Get(family));
}
}
/// <summary>A defined family returns the same cached string instance on every call (no per-event allocation).</summary>
[Fact]
public void Get_ForDefinedFamily_ReturnsCachedInstance()
{
Assert.Same(
MxEventFamilyNames.Get(MxEventFamily.OnDataChange),
MxEventFamilyNames.Get(MxEventFamily.OnDataChange));
}
/// <summary>
/// A numeric value the contract does not define (a newer worker, or a malformed frame) still
/// produces the <see cref="Enum.ToString()"/> form rather than throwing.
/// </summary>
[Fact]
public void Get_ForUndefinedValue_FallsBackToToString()
{
MxEventFamily undefined = (MxEventFamily)987654;
Assert.Equal(undefined.ToString(), MxEventFamilyNames.Get(undefined));
}
}