Performance counters can be used in a Windows Azure application to collect data, allowing you to identify system bottlenecks and tune system and application performance. Windows Azure provides several performance counters available for Windows Server 2008, IIS, and ASP.NET. For a list of performance counters that can be used for Windows Azure applications, see the
"Overview of creating and using performance counters in a Windows Azure application."
To accomplish this task, follow these steps.
')
- Step 1. Collect data from performance counters
- Step 2. Create custom performance counters (optional)
- Step 3. Request data from performance counters
Step 1. Collect data from performance counters
To set up data collection from performance counters in a Windows Azure application, use the
GetDefaultInitialConfiguration method, add the
PerformanceCounters data source with an instance of
PerformanceCounterConfiguration , and then call the
Start method with the changed configuration. To collect data from performance counters, follow these steps:
Open the source file for the role.
Note Typically, in the following steps, the code is added to the
OnStart method of the role.
Get a copy 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 the performance counter added to the diagnostic monitor configuration.
config.PerformanceCounters.DataSources.Add( new PerformanceCounterConfiguration()) { CounterSpecifier = @"\Processor(_Total)\% Processor Time", SampleRate = TimeSpan.FromSeconds(5) });
Start the diagnostics monitor with the changed configuration. The following code example shows the launch of a 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 the
“Configuring Connection Strings” section .
Save and build the project, and then deploy the application.
After completing these steps, the Windows Azure Diagnostics Monitor will begin collecting data from the performance counters.
Step 2. Create custom performance counters (optional)
New customizable 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 the
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.
<Runtime executionContext="elevated" />
Save the file. Open the source file for the role. If the following
using statement is missing, add it.
using System.Diagnostics;
Create a custom performance counter category in your role's
OnStart method. The following example shows how to create a custom category with two counters (if there is none).
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 start 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, the Windows Azure Diagnostics Monitor will begin collecting data from customizable performance counters.
Step 3. Request data from performance counters
After you configure the Windows Azure Diagnostic Monitor to collect and transfer performance counters 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 executing the
CloudTableQuery query in the
WADPerformanceCountersTable to the Windows Azure storage. To request data from performance counters, follow these steps:
Open the source file for the role containing 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 to query the 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 shows 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 returned table entries. The following example shows how to return CPU usage records for the last five minutes for the current role instance.
IQueryable<PerformanceCountersEntity> performanceCountersTable = serviceContext.CreateQuery<PerformanceCountersEntity>( "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<PerformanceCountersEntity> query = selection.AsTableServiceQuery<PerformanceCountersEntity>(); // Use the Execute command explicitly on the TableServiceQuery to // take advantage of continuation tokens automatically and get all the data. IEnumerable<PerformanceCountersEntity> 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<PerformanceCountersEntity> list = result.ToList();
Save and build the project, and then deploy the application.
After performing these steps, the data from the performance counter will be available for generating reports.
Additional resources