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:
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user