ApplicationInsights-dotnet icon indicating copy to clipboard operation
ApplicationInsights-dotnet copied to clipboard

CacheHelper.GetCounterValue performance tuning

Open jirikanda opened this issue 6 years ago • 6 comments

Actual Behavior

public long GetCounterValue(string name, AzureWebApEnvironmentVariables environmentVariable)
{
    if (!CacheHelper.Instance.IsInCache(name))
    {
        PerformanceCounterImplementation client = new PerformanceCounterImplementation();
        string uncachedJson = client.GetAzureWebAppEnvironmentVariables(environmentVariable);

        if (uncachedJson == null)
        {
            return 0;
        }

        CacheHelper.Instance.SaveToCache(name, uncachedJson, DateTimeOffset.Now.AddMilliseconds(500));
    }
    ...

Issue

  • GetCounterValue method is called a lot of times.
  • The method reads a value from an environment variable.
  • There are only four environment variables.
  • The value of an environment variable is stored to the cache under the key name.
  • The reuse of the value is limited due the key name.

Expected Behavior - cache key (high priority)

  • It would be more efficient to store the environment value to the cache under environmentVariable key (or somehow derived key).
  • The value of the envinronment variable would be reused for a lot of method calls (during the 500ms expiration)

Expected Behavior - environment variable does not exists (low priority)

  • When the environment variable does not exists, this information is not cached
  • Repetitive method calls call expensive Environment.GetEnvironmentVariable.

Version Info

SDK Version : 2.9.1

jirikanda avatar May 21 '19 09:05 jirikanda

This issue is stale because it has been open 300 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Sep 18 '21 00:09 github-actions[bot]

Hm, I see I need to fix it myself. Lets try some PR...

jirikanda avatar Sep 22 '21 07:09 jirikanda

This issue is stale because it has been open 300 days with no activity. Remove stale label or this will be closed in 7 days. Commenting will instruct the bot to automatically remove the label.

github-actions[bot] avatar Jul 22 '22 00:07 github-actions[bot]

Hm, I see I need to fix it myself. Lets try some PR...

jirikanda avatar Jul 22 '22 09:07 jirikanda

This issue is stale because it has been open 300 days with no activity. Remove stale label or this will be closed in 7 days. Commenting will instruct the bot to automatically remove the label.

github-actions[bot] avatar May 20 '23 00:05 github-actions[bot]

Leave it open.

jirikanda avatar May 22 '23 08:05 jirikanda