Using performance counters in a Windows Azure application

Original author: Windows Azure Team
  • Transfer
  • Tutorial
image

Performance counters can be used in a Windows Azure application to collect data that helps identify system bottlenecks and tune system and application performance. Windows Azure provides several performance counters available for Windows Server 2008, IIS, and ASP.NET. The list of performance counters that can be used for Windows Azure applications, see. In the section "General information about creating and using performance counters in Windows Azure application" .

To implement this task, follow these steps:

  • Step 1. Collecting data from performance counters
  • Step 2. Creating custom performance counters (optional)
  • Step 3. Request data from performance counters

Step 1. Collecting data from performance counters


To configure the collection of data from performance counters, the Windows Azure application uses the GetDefaultInitialConfiguration method , adds the PerformanceCounters data source with an instance of PerformanceCounterConfiguration , and then calls the Start method with the modified configuration. To collect data from performance counters, follow these steps:

Open the source file for the role.

Note . Typically, the following steps add code to the role’s OnStart method .

Get an instance of the diagnostic monitor configuration. The following code example shows how to get the default diagnostic monitor configuration object.

var config = DiagnosticMonitor.GetDefaultInitialConfiguration();

Specify monitored performance counters. The following example shows a performance counter that is added to the diagnostic monitor configuration.

config.PerformanceCounters.DataSources.Add( 
  new PerformanceCounterConfiguration()) 
  { 
      CounterSpecifier = @"\Processor(_Total)\% Processor Time", 
      SampleRate = TimeSpan.FromSeconds(5) 
  });

Run the diagnostic monitor with the reconfigured. The following code example shows how to start the monitor.

DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);

Note . This code example shows the use of a connection string. For more information about connection strings, see "Configuring Connection Strings" .

Save and build the project, and then deploy the application.

After completing these steps, Windows Azure Diagnostic Monitor will begin collecting data from performance counters.

Step 2. Creating custom performance counters (optional)


New custom performance counters can be added from the application to the diagnostic monitor configuration. To do this, use the custom category and counter names to create PerformanceCounterConfiguration instances for each counter and add them to the PerformanceCounters data source collection in DiagnosticMonitorConfiguration . To create custom performance counters, follow these steps:

Open the service definition file (CSDEF) for the application.

Add a Runtime element to the WebRole or WorkerRole element to allow execution with higher privileges.


Save the file. Open the source file for the role. If the next using statement is missing, add it.

using System.Diagnostics;

Create a custom performance counter category in the OnStart method of your role. The following example shows how to create a custom category with two counters (if one is missing).

if (!PerformanceCounterCategory.Exists("MyCustomCounterCategory")) 
{ 
    CounterCreationDataCollection counterCollection = new CounterCreationDataCollection(); 
    // add a counter tracking user button1 clicks 
    CounterCreationData operationTotal1 = new CounterCreationData(); 
   operationTotal1.CounterName = "MyButton1Counter"; 
   operationTotal1.CounterHelp = "My Custom Counter for Button1"; 
   operationTotal1.CounterType = PerformanceCounterType.NumberOfItems32; 
    counterCollection.Add(operationTotal1); 
    // add a counter tracking user button2 clicks 
    CounterCreationData operationTotal2 = new CounterCreationData(); 
   operationTotal2.CounterName = "MyButton2Counter"; 
   operationTotal2.CounterHelp = "My Custom Counter for Button2"; 
   operationTotal2.CounterType = PerformanceCounterType.NumberOfItems32; 
    counterCollection.Add(operationTotal2); 
    PerformanceCounterCategory.Create( 
      "MyCustomCounterCategory", 
      "My Custom Counter Category", 
      PerformanceCounterCategoryType.SingleInstance, counterCollection); 
    Trace.WriteLine("Custom counter category created."); 
} 
else{ 
    Trace.WriteLine("Custom counter category already exists."); 
}

Add new custom performance counters to the diagnostic monitor configuration and run the diagnostic monitor in the OnStart method before calling base.OnStart .

DiagnosticMonitorConfiguration config = 
  DiagnosticMonitor.GetDefaultInitialConfiguration(); 
config.PerformanceCounters.ScheduledTransferPeriod = 
  TimeSpan.FromMinutes(2D); 
config.PerformanceCounters.BufferQuotaInMB = 512; 
TimeSpan perfSampleRate = TimeSpan.FromSeconds(30D); 
// Add configuration settings for custom performance counters. 
config.PerformanceCounters.DataSources.Add( 
  new PerformanceCounterConfiguration() 
{ 
  CounterSpecifier = @"\MyCustomCounterCategory\MyButton1Counter", 
        SampleRate = perfSampleRate 
}); 
config.PerformanceCounters.DataSources.Add( 
  new PerformanceCounterConfiguration() 
{ 
  CounterSpecifier = @"\MyCustomCounterCategory\MyButton2Counter", 
        SampleRate = perfSampleRate 
}); 
// Apply the updated configuration to the diagnostic monitor.     
DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);

Update the counters in the application. The following example shows how to update a custom performance counter in Button1_Click events .

protected void Button1_Click(object sender, EventArgs e) 
{ 
  button1Counter = new PerformanceCounter( 
    "MyCustomCounterCategory", 
    "MyButton1Counter", 
    string.Empty, 
    false); 
  button1Counter.Increment(); 
  this.Button1.Text = "Button 1 count: " + 
    button1Counter.RawValue.ToString(); 
}

Save and build the project, and then deploy the application.

After completing these steps, Windows Azure Diagnostic Monitor will begin collecting data from custom performance counters.

Step 3. Request data from performance counters


After you configure the Windows Azure Diagnostic Monitor to collect and transfer performance counter data to Windows Azure Storage, you can use this data to create reports. Data from performance counters in a Windows Azure application is transferred by listing the results of a CloudTableQuery query in a WADPerformanceCountersTable in Windows Azure Storage. To request data from performance counters, follow these steps:

Open the source file for the role that contains the code. If the following using statements are missing, add them.

using System.Linq; 
using Microsoft.WindowsAzure; 
using Microsoft.WindowsAzure.StorageClient;

Create a table schema view class for querying performance counter tables.

public class PerformanceCountersEntity : TableServiceEntity 
{ 
  public long EventTickCount { get; set; } 
  public string DeploymentId { get; set; } 
  public string Role { get; set; } 
  public string RoleInstance { get; set; } 
  public string CounterName { get; set; } 
  public string CounterValue { get; set; } 
}

Get an instance of the table services context. The following code example demonstrates how to get the default diagnostic monitor table services context.

CloudStorageAccount storageAccount = CloudStorageAccount.Parse( 
  "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"); 
      CloudTableClient cloudTableClient = 
  storageAccount.CreateCloudTableClient(); 
      TableServiceContext serviceContext = 
  cloudTableClient.GetDataServiceContext();

Create a query to specify the returned table entries. The following example shows how to return CPU usage records in the last five minutes for the current role instance.

IQueryable performanceCountersTable = 
  serviceContext.CreateQuery( 
    "WADPerformanceCountersTable"); 
var selection = from row in performanceCountersTable 
  where row.EventTickCount > DateTime.UtcNow.AddMinutes(-5.0).Ticks 
  && row.CounterName.Equals(@"\Processor(_Total)\% Processor Time") 
  select row; 
CloudTableQuery query = 
  selection.AsTableServiceQuery(); 
// Use the Execute command explicitly on the TableServiceQuery to 
// take advantage of continuation tokens automatically and get all the data. 
IEnumerable result = query.Execute();

Note. For more information about query syntax, see LINQ: .NET Language-Integrated Query .

Use the data to analyze and report on application performance.

List list = result.ToList(); 
    // Display list members here.

Save and build the project, and then deploy the application.

After completing these steps, data from the performance counter will be available for reporting.

Additional Resources



Also popular now: