0

I'm using NSubstitute to mock my DB calls for my benchmarking. For my benchmarking i'm using BenchmarkDotNet. Im getting memory allocation although there is no memory allocated in my substituted method. I need to know what causes this memory allocation and according to what the number of bytes is calculated.

    [SimpleJob(RunStrategy.Monitoring, targetCount: 100)]
    [MemoryDiagnoser]
    [Orderer(SummaryOrderPolicy.FastestToSlowest)]
    public class PlayerHistory
    {  
        private static ILocalCache _localCache = Substitute.For<ILocalCache>();

        [GlobalSetup]
        public async Task Setup()
        {
            LocalCache.Instance.SetInstance(_localCache);
            Mock(); 
        }

      
        private void Mock()
        {
            LocalCacheController.Instance.GetRealLeagueGroupId("").ReturnsForAnyArgs(BenchmarkConstants.RealLeagueId);
        }

        [Benchmark]
        public async Task GetUsersInfoByUsers()
        {
            await TeamManager.Instance.GetPlayerHistory(BenchmarkConstants.RealLeagueId);
        }
    }

In my TeamManager:

    public async Task<string> GetPlayerHistory(string realLeagueId)
    {
        return LocalCacheController.Instance.GetRealLeagueGroupId(realLeagueId);      
    }

This is my benchmark results:

|              Method |     Mean |    Error |   StdDev |   Median | Gen 0 | Gen 1 | Gen 2 | Allocated |
|-------------------- |---------:|---------:|---------:|---------:|------:|------:|------:|----------:|
| GetUsersInfoByUsers | 63.66 us | 20.96 us | 61.79 us | 29.82 us |     - |     - |     - |     376 B |

can you tell me why although i'm substituting the method there is still memory allocated and why how it's calculated?

rerez
  • 111
  • 5
  • 1
    NSubstitute is recording the calls made to the mocked members, for later verification. As far as I know you have no way of turning this off so I think you have no other recourse than to either live with the memory allocations, or to not use NSubstitute. – Lasse V. Karlsen Jun 29 '21 at 08:53
  • @LasseV.Karlsen There is another alternative where I can do benchmarking on a method along with mocking the methods that access the DB? – rerez Jun 29 '21 at 08:58
  • 1
    Create the mock manually? – Lasse V. Karlsen Jun 29 '21 at 10:04

1 Answers1

0

I need to know what causes this memory allocation

For that you need to use a Memory Profiler.

and according to what the number of bytes is calculated.

https://adamsitnik.com/the-new-Memory-Diagnoser/

Adam Sitnik
  • 1,256
  • 11
  • 15