GetAsync gets high response time in load requests

48 Views Asked by At

I am using LazyCache.AspNetCore package from nuget to store some data in memory and read from memory. this is my sample code:

IAppCache cache = new CachingService();
StopWatch _watch;

public async Task TestAsync(UserInfo user){

    var key = await CreateCustomTokenAsync(user.X);

    _watch = new Stopwatch();
    _watch.Start();

    var val = await cache.GetAsync<string>(key);
    if (val== user.Y) {

       var timeTaken = _watch.Elapsed.TotalMilliseconds;
       log(timeTaken);
       _watch.Stop();

       return;
    }   

    // ...
}

but the response time gets high more than 20 ms. I am wondering how I can fix this problem because reading from memory is supposed to be less than 20 ms.

1

There are 1 best solutions below

0
On

This is an N = 1 test: you do only one measurement. There will be much deviation due to startup behavior, thus it's not representative. For example: the just-in-time compiler is just spinning up, so you get additional delay of loading the associated assemblies and compiling it.

You should do a proper benchmark test. E.g. try BenchmarkDotNet