Skip to content

be accurate about allocated bytes/op #200

@ig-sinicyn

Description

@ig-sinicyn

Bench.Net 0.9.7

Code:

    class Program
    {
        static void Main(string[] args)
        {
            new SimpleBenchmark().BaselineOutput();
            var config = new ManualConfig();
            config.Add(DefaultConfig.Instance);
            config.Add(new MemoryDiagnoser());

            BenchmarkRunner.Run<SimpleBenchmark>(config);
        }
    }

    public class SimpleBenchmark
    {
        private volatile int x;

        [Benchmark]
        public int Baseline()
        {
            x = 0;
            for (int i = 0; i < 10000000; i++)
            {
                x++;
            }
            return x;
        }

        public void BaselineOutput()
        {
            var m = GC.GetTotalMemory(false);
            var sw = Stopwatch.StartNew();

            Baseline();

            sw.Stop();
            var m2 = GC.GetTotalMemory(false);
            Console.WriteLine($"Allocated: {m2 - m}, elapsed {sw.Elapsed.TotalMilliseconds}ms");
        }
    }

Output:

Allocated: 0, elapsed 25,7643ms
...
   Method |     Median |    StdDev | Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
--------- |----------- |---------- |------ |------ |------ |------------------- |
 Baseline | 24.9992 ms | 0.1676 ms |     - |     - |     - |           2 696,37 |

The code obviously runs without allocations. The issue is blocker for us as it means we cannot trust output from the diagnoser. If you had any idea or workaround, write a comment please:)

As far as I can see this can be fixed only with introduction of two-way notifications before and after each of benchmark run.

There's alternate solution: run the diagnosers in-process with the benchmark. Sadly, it fires a new issue: there should be a way to report results from the diagnoser back to the main process. Ouch:)

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions