Home
Options

bug? When using the Ctrl+ShiftB benchmarking support, AllocatedBytes always shows "null"

edited June 2023

Reproduce with:

10.ToString().Dump();

mark the line, hit Ctrl+ShiftB, and note that AllocatedBytes shows "null", even though this line does allocate a string:

IL_0000 ldc.i4.s    0A  // 10
IL_0002 stloc.0 
IL_0003 ldloca.s    00 
IL_0005 call    Int32.ToString ()                             // <-- here
IL_000A call    Extensions.Dump<String> (String)              // <-- and I assume here
IL_000F pop 
IL_0010 ret

It seems that in the following line of code:

public long? BytesPerOperation => TotalOperations == 0 ? null : TotalAllocatedBytes / TotalOperations;

TotalOperations is always 0, as is TotalAllocatedBytes, though I'm not familiar enough with how BenchMarkDotNet reports data to look further into why.

Reproduced with LINQPad Beta 7.8.1 and release 7.7.15.

Comments

  • Options
    edited June 2023

    When testing the configuration using the following code, I do get output that contains memory allocations, so the configuration of BenchmarkDotNet is correct:

    | Method |      Mean |    Error |   StdDev |     Gen0 |  Allocated |
    |------- |----------:|---------:|---------:|---------:|-----------:|
    |      A | 736.88 us | 7.247 us | 6.779 us | 254.8828 | 3124.69 KB |
    |      B |  77.14 us | 1.239 us | 1.159 us |  25.3906 |  312.19 KB |
    

    code:

    #load "BenchmarkDotNet"
    
    void Main()
    {
        var config = GetBenchmarkConfig()
            .AddLogger(new BenchmarkDotNet.Loggers.ConsoleLogger());
        BenchmarkDotNet.Running.BenchmarkRunner.Run<Tests>(config);
    }
    
    public class Tests
    {
        [Benchmark]
        public void A()
        {
            int total = 0;
            for (int i = 0; i < 100000; i++)
            {
                total += i.ToString().Length;
            }
            if (total == 0)
                throw new InvalidOperationException();
        }
    
        [Benchmark]
        public void B()
        {
            int total = 0;
            for (int i = 0; i < 10000; i++)
            {
                total += i.ToString().Length;
            }
            if (total == 0)
                throw new InvalidOperationException();
        }
    }
    
Sign In or Register to comment.