Search code examples
c#azure.net-coreazure-web-app-service.net-core-2.2

How do I query an indicator of performance capability in an Azure App Service?


I have some slow-running services doing some fairly heavy processing. These processes run in several different places, including on-prem servers and in Azure, and all write their results to a common database. I'd like to have a note stored in the database tied to the processing results indicating something useful about the performance capabilities of where the processing was ran.

My on-prem servers run the following code successfully, which allows me to log the CPU name (ex: my workstation logs "AMD Ryzen 9 3900X 12-Core Processor" with the following code, which is good enough for our needs):

    public string GetCpu()
    {
        var searcher = new ManagementObjectSearcher("SELECT Name FROM Win32_Processor");

        string results = "Unknown CPU";
        foreach (var item in searcher.Get())
        {
            results = item["Name"].ToString();
        }

        return results;
    }

As you probably suspect, this code fails to run in Azure with a permissions error. What is something else that I can run in Azure to get useful information, even if not identical? I don't necessary need a CPU name, just a useful indicator of processing capabilities. If something like "P1V1 App Hosting Plan" was logged, that would be quite good enough for our needs. I'm not sure if there's something more indicative of processing power than that but I'm open to other suggestions as well. I suspect getting a CPU model might even be deceptive in Azure given the multi-tenant setups going on.


Solution

  • There is a easy way to get it, the Azure has a environment called WEBSITE_SKU store the host plan level. You could go to your Kudu page to check the env.

    You could use Environment.GetEnvironmentVariable("WEBSITE_SKU") to get it.

    enter image description here

    enter image description here


    The above example will output "PremiumV2" for all levels of the Premium V2 configurations. That means it has the same output for P1V2, P2V2, and P3V2 even though each one has twice as many ACUs as the prior. Fortunately, there are a number of other environment variables with useful information in them that can get us more info. With this extra information, an example of a function to gather information in a way that works both on-prem and in an Azure App Service might look like this:

    public string GetComputationalResources()
    {
        string results = null;
    
        try
        {
            var azureSku = Environment.GetEnvironmentVariable("WEBSITE_SKU");
            if (!string.IsNullOrWhiteSpace(azureSku))
            {
                // We're in Azure. Get more information!
    
                var cpuCount = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")?.Trim();
                var cpuId = Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER")?.Trim();
                var computeMode = Environment.GetEnvironmentVariable("WEBSITE_COMPUTE_MODE")?.Trim();
                var websiteMode = Environment.GetEnvironmentVariable("WEBSITE_SITE_MODE")?.Trim();
                results = $"{azureSku} {computeMode} {websiteMode} | {cpuCount}x {cpuId}";
            }
    
        }
        catch
        {
            results = null;
        }
    
        if (results == null)
        {
            try
            {
                using (var searcher = new ManagementObjectSearcher("SELECT Name FROM Win32_Processor"))
                {
                    foreach (var item in searcher.Get())
                    {
                        results = item["Name"].ToString().Trim();
                    }
                }
            }
            catch
            {
                results = "Unknown CPU";
            }
        }
    
        return results;
    }
    

    Example outputs (it's possible to get AMD and not just Intel in Azure, from what I understand):

    • On-Prem: AMD Ryzen 9 3900X 12-Core Processor
    • P1V2: PremiumV2 Dedicated | 1x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • P2V2: PremiumV2 Dedicated | 2x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • P3V2: PremiumV2 Dedicated | 4x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • P1: Premium Dedicated | 1x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • P2: Premium Dedicated | 2x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • P3: Premium Dedicated | 4x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • S1: Standard Dedicated | 1x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • S2: Standard Dedicated | 2x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel
    • S3: Standard Dedicated | 4x Intel64 Family 6 Model 79 Stepping 1, GenuineIntel