C# 性能计数器干扰他人
我需要为应用程序定义性能计数器。我编写了两个单元测试,以确保它们按预期工作。对于每种不同的计数器类型,我都有一个测试类,比如AverageCountTest(C# 性能计数器干扰他人,c#,windows,performancecounter,C#,Windows,Performancecounter,我需要为应用程序定义性能计数器。我编写了两个单元测试,以确保它们按预期工作。对于每种不同的计数器类型,我都有一个测试类,比如AverageCountTest(AverageCount64)或InstanceCountTest(NumberOfItems64)或RateCounterTest(RateOfCountersSecond64)。当我分别运行它们时,它们通过。如果我同时运行它们,大多数都会由于计数器报告0值而失败 对于NumberOfItems64,测试如下所示: counter.Nex
AverageCount64
)或InstanceCountTest(NumberOfItems64
)或RateCounterTest(RateOfCountersSecond64
)。当我分别运行它们时,它们通过。如果我同时运行它们,大多数都会由于计数器报告0
值而失败
对于NumberOfItems64
,测试如下所示:
counter.NextValue(); // sample first and reset the counter
int someValue = random.Next(int.MaxValue);
counter.IncrementBy(someValue);
float expected = someValue;
Assert.AreEqual(expected, counter.NextValue());
counter.NextValue(); // sample first and reset the counter
counter.IncrementBy(1);
Thread.Sleep(2000);
float expected = 1 / (float)2;
Assert.IsTrue(expected, counter.NextValue());
另一个例子可能是RateOfCountsPerSecond64
:
counter.NextValue(); // sample first and reset the counter
int someValue = random.Next(int.MaxValue);
counter.IncrementBy(someValue);
float expected = someValue;
Assert.AreEqual(expected, counter.NextValue());
counter.NextValue(); // sample first and reset the counter
counter.IncrementBy(1);
Thread.Sleep(2000);
float expected = 1 / (float)2;
Assert.IsTrue(expected, counter.NextValue());
什么可能导致计数器报告
0
s?我在每个测试中使用不同的性能计数器类别(随机Guid)来隔离它们。如何/何时为测试创建性能计数器?如何/何时为测试创建性能计数器?