特定のメソッドの平均実行時間を測定するためにPerformanceCounter
を設定しようとしています。私はAverageTimer32
を読もうとしましたが、多くの例を見てきましたが、それを正しく理解できないようです。AverageTimer32についての混乱PerformanceCounter
私はその後、私はカウンター
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();
}
呼び出されるたびにログカテゴリに
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);
を設定これで、私はこのように見えるパフォーマンスモニタに結果をもたらします。私は50ミリ秒の周りに代わりcontious曲線のスパイクを取得:
私はAverageTimer32
を誤解していますか?私はそれについて読んだが、少し混乱している。しかし、私は実際に私と同じことをやっている例を見てきましたので、うまくいくはずです。私はスパイクだけを得る理由は何でしょうか?
編集 それはTheMethodIWantToMeasure
のみごと〜5秒と呼ばれていることを言及する価値があるかもしれない、と私はちょうど私がスパイクおき〜第五秒を得ることに気づきました。しかし、AverageTimer32
が式((N 1 -N 0)/ F)/(B 1 -B 0)を使用すると、その結果にどのように影響する可能性があるのか分かりません。それはNとBの値をどれくらいの頻度で保存するかに依存してはいけませんか?