Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Host.Tests/RuntimeConfigTests.cs
T
Joseph Doherty b47002b45e perf(host): cap thread-pool spin (UnfairSemaphoreSpinLimit=10) (#146)
Mirror of mxaccessgw#237 (659bb831). DEV vd03 showed 229k context switches
in ZB.MOM.WW.ScadaBridge.Host at ~240 MxGateway events/s. Akka's default
dispatcher, gRPC/Kestrel and async continuations all run on the .NET thread
pool, so a trickle of small work items makes idle pool workers spin with
Sleep(0) (default limit 70) before parking.

- Host csproj: RuntimeHostConfigurationOption
  System.Threading.ThreadPool.UnfairSemaphoreSpinLimit=10, written into
  ZB.MOM.WW.ScadaBridge.Host.runtimeconfig.json (verified in the publish
  output, which docker/Dockerfile copies into the image).
- RuntimeConfigTests pins the value in the built runtimeconfig.
- Component-Host.md: new "Runtime thread-pool settings" section (what, why,
  override/revert via DOTNET_ThreadPool_UnfairSemaphoreSpinLimit).

Local synthetic trickle bench (macOS, ~185 chains/s): involuntary context
switches 17-28k -> 6-13k, CPU 430-560 -> 200-295 ms per 20 s. The vd03
before/after WPR measurement is pending the next vd03 deploy.
2026-11-28 09:15:20 -05:00

42 lines
1.7 KiB
C#

using System.Text.Json;
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
/// <summary>
/// Pins the .NET runtime settings the Host ships in its <c>*.runtimeconfig.json</c>
/// (Component-Host.md -> Runtime thread-pool settings). The Host's runtimeconfig is copied
/// next to this test assembly because the test project references the Host executable, so
/// the file read here is the one the build produces — the same file publish (and therefore
/// the Docker image and the Windows service install) carries.
/// </summary>
public class RuntimeConfigTests
{
private static JsonElement ReadHostConfigProperties()
{
var path = Path.Combine(AppContext.BaseDirectory, "ZB.MOM.WW.ScadaBridge.Host.runtimeconfig.json");
Assert.True(File.Exists(path), $"Host runtimeconfig not found at {path}");
using var doc = JsonDocument.Parse(File.ReadAllText(path));
return doc.RootElement
.GetProperty("runtimeOptions")
.GetProperty("configProperties")
.Clone();
}
/// <summary>
/// #146 (mirror of mxaccessgw#237): the thread pool's idle spin is capped at 10 steps
/// (runtime default 70) so pool workers that find no work park instead of yielding
/// with <c>Sleep(0)</c> dozens of times per work item.
/// </summary>
[Fact]
public void Host_runtimeconfig_caps_thread_pool_unfair_semaphore_spin_at_10()
{
var props = ReadHostConfigProperties();
Assert.True(
props.TryGetProperty("System.Threading.ThreadPool.UnfairSemaphoreSpinLimit", out var value),
"System.Threading.ThreadPool.UnfairSemaphoreSpinLimit missing from the Host runtimeconfig");
Assert.Equal(10, value.GetInt32());
}
}