Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 性能计数器干扰他人_C#_Windows_Performancecounter - Fatal编程技术网

C# 性能计数器干扰他人

C# 性能计数器干扰他人,c#,windows,performancecounter,C#,Windows,Performancecounter,我需要为应用程序定义性能计数器。我编写了两个单元测试,以确保它们按预期工作。对于每种不同的计数器类型,我都有一个测试类,比如AverageCountTest(AverageCount64)或InstanceCountTest(NumberOfItems64)或RateCounterTest(RateOfCountersSecond64)。当我分别运行它们时,它们通过。如果我同时运行它们,大多数都会由于计数器报告0值而失败 对于NumberOfItems64,测试如下所示: counter.Nex

我需要为应用程序定义性能计数器。我编写了两个单元测试,以确保它们按预期工作。对于每种不同的计数器类型,我都有一个测试类,比如AverageCountTest(
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)来隔离它们。

如何/何时为测试创建性能计数器?如何/何时为测试创建性能计数器?