PerformanceCounter
特定のメソッドの平均実行時間を測定するように設定しようとしています。私は読み込もうとAverageTimer32
し、多くの例を見てきましたが、正しく理解できないようです。
カテゴリーを設定しました
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 ミリ秒付近で連続的な曲線ではなくスパイクが発生します。
私は誤解しましたAverageTimer32
か?私はそれについて読んだことがありますが、それは少し混乱しています。ただし、実際に私と同じことをしている例を見てきましたので、うまくいくはずです。スパイクしか発生しない理由は何ですか?
編集のみが 5 秒ごとに呼び出される
ことに言及する価値があるかもしれませんがTheMethodIWantToMeasure
、5 秒ごとにスパイクが発生することに気付きました。AverageTimer32
しかし、式 ((N 1 -N 0)/F)/(B 1 -B 0) を使用すると、結果にどのように影響するかわかりません。N と B の値を保存する頻度に依存するべきではありませんか?