BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

Custom Diagnoser for average CPU and RAM usage during the test

Open JohannesDeml opened this issue 3 years ago • 1 comments

I would like to add columns to the benchmarks for the average CPU and RAM usage during the test (I'm benchmarking network libraries). I tried using AddDiagnoser(new EventPipeProfiler(EventPipeProfile.CpuSampling));, but due to the sheer number of calls in my benchmark, the resulting file is multiple gigabytes in size and takes a long time to process.

I was thinking about creating a custom diagnoser, that uses PerformanceCounter for CPU and RAM and takes samples every X ms and calculates an average at the end. Is this possible by extending the IDiagnoser or can I achieve this in any other way?

JohannesDeml avatar Mar 09 '21 16:03 JohannesDeml

I wanted something similar to show the difference in CPU utilization (not just execution time) between a single-threaded and multi-threaded implementation. This is what I came up with. I have no idea how accurate it is, but the timings seem to match up pretty closely with the single-threaded synchronous implementation execution time reported by the standard BDN timings. I suspect this is only going to be accurate if the timings are in in the tens of milliseconds or more. I should point out that this only works with "InProc" execution, which is not the default for BDN. This seemed good enough for my needs anyway.

Copy that file into your project and add [CpuDiagnoser] to a benchmark class and it will show up in the output results.

MarkPflug avatar Dec 06 '21 04:12 MarkPflug