2013-03-25 222 views
9

我試圖設置一個PerformanceCounter來衡量某個方法的平均執行時間。我試過閱讀AverageTimer32,我已經看了很多例子,但我似乎無法做到。關於AverageTimer32 PerformanceCounter的困惑

我設置的類別

CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); 

// Add the counter. 
CounterCreationData averageTimer32 = new CounterCreationData(); 
averageTimer32.CounterType = PerformanceCounterType.AverageTimer32; 
averageTimer32.CounterName = counterName; 
CCDC.Add(averageTimer32); 

// Add the base counter. 
CounterCreationData averageTimer32Base = new CounterCreationData(); 
averageTimer32Base.CounterType = PerformanceCounterType.AverageBase; 
averageTimer32Base.CounterName = baseCounterName; 
CCDC.Add(averageTimer32Base); 

// Create the category. 
PerformanceCounterCategory.Create(categoryName, "Demonstrates usage of the AverageTimer32 performance counter type", PerformanceCounterCategoryType.SingleInstance, CCDC); 

然後,我創建了櫃檯

PC = new PerformanceCounter(categoryName, counterName, false); 

BPC = new PerformanceCounter(categoryName, baseCounterName, false); 

PC.RawValue = 0; 
BPC.RawValue = 0; 

最後我我的每一個方法被調用

private void TheMethodIWantToMeasure() { 
    Stopwatch stopwatch = Stopwatch.StartNew(); 

    // Fake work that take ~50ms 
    Thread.Sleep(50 + random.Next(-10, 10)); 

    stopwatch.Stop(); 

    PC.IncrementBy(stopwatch.ElapsedTicks); 
    BPC.Increment(); 
} 

否則,如時間記錄經過的時間這最終會導致性能監視器看起來像這樣。我得到尖峯,而不是50毫秒的連續曲線: Picture

我誤解了AverageTimer32?我已經閱讀過,但有點令人困惑。但是,我看過幾個例子和我一樣,所以我猜它應該可以工作。什麼可能是我只有尖峯的原因?

編輯 這可能是值得一提的是TheMethodIWantToMeasure只叫每5秒〜,我才意識到,我讓每個〜秒殺第5秒。但我不明白如果AverageTimer32使用公式((N 1 -N 0)/ F)/(B 1 -B 0),結果會如何影響結果。它不應該取決於我爲N和B存儲值的頻率?

回答

1

你的答案在於你的permon設置的刷新率/採樣率。如果您要取出〜5s間隔或至少將其更改爲〜10ms之類的內容,您可能會注意到該圖看起來更像您最初的預期。或者更新你的性能計數器刷新率到更高的間隔(30秒)將是相同的。 (通過右鍵單擊perfMon圖形 - >屬性 - >常規選項卡 - > Sampe每隔x秒)執行此操作。

原因是perfMon每1秒刷新一次(默認),然後需要顯示您的平均值。因此,需要「全部」您已添加到計數器中的操作,並將其繪製在圖上。

示例:如果你有操作在第二執行(爲0.1ms,0.2ms的& 0.3,MS),PERFMON將顯示您的平均爲0.2ms的,這是正確的。

爲什麼有差距?我相信這是因爲現在在計算平均值之後,你會看到下一秒(當perfMon再次刷新時),它將計算0secs = 0的0次操作的平均值。

我的建議(如果你想真正看到你的TheMethodIWantToMeasure平均運行多長時間的平均值,就是到以你的〜5s間隔完全排除,並且簡單地讓方法連續運行,這應該會有所斬獲,