Creating and Using Custom Performance Counters in Windows Azure

Building software, especially software running on servers, requires some way to look “inside” the running application. Using the debugger is one way, but you cannot use a debugger on production applications. A better way is to use performance counters. These give you a way to see things, like how hard the CPU working, but also how many orders have been processed by your system. The first performance counter is provided by the system, the latter you can build yourself.

Before Azure SDK 1.3 you couldn’t create your own performance counters because your code doesn’t get write access to the region of the registry where you register your custom performance counters. But with elevated startup tasks this is easy. In the blog post I will show you how you can create a startup task to create custom performance counters, and how to use them in your role.

Commence by creating a new Visual Studio Cloud project. Add a single worker role. We’ll use this role to illustrate using a performance counter.

Add another project, now a Console project (call it InstallPerfCounters). We’ll use this console application as the startup task.

Implement the InstallPerfCounters console project as follows:

Code Snippet
  1. class Program
  2. {
  3.   static void Main(string[] args)
  4.   {
  5.     const string categoryName = "U2U";
  6.     const string categoryHelp = "U2U demo counters";
  8.     if (!PerformanceCounterCategory.Exists(categoryName))
  9.     {
  10.       var counters = new System.Diagnostics.CounterCreationDataCollection();
  11.       counters.Add(new CounterCreationData
  12.       {
  13.         CounterName = "# secs/running",
  14.         CounterHelp = "How long has this been running",
  15.         CounterType = PerformanceCounterType.NumberOfItems32
  16.       });
  18.       var category = PerformanceCounterCategory.Create(categoryName,
  19.         categoryHelp, PerformanceCounterCategoryType.MultiInstance, counters);
  20.     }
  21.   }
  22. }

This uses the same kind of code you would use anywhere else to create new performance counters.

Now we need to install this as a startup task. Add a folder to the worker role project, calling it startup. We need to add two files here, one the console project we just made, and a command file.

To copy the executable, let’s make first sure we’re using the release version at all time. Open the configuration manager:


Select Release as the configuration:


Build your project, ensuring everything compiles nicely.

Now right-click the startup folder from the worker role project and select Add Existing Item… Browse to the release folder of the console project, select the executable and choose Add As Link from the drop-down:


This should add the executable to the startup folder. Select it, and select Copy Always in the properties folder:


Now we are ready to add the command file. Don’t use Visual Studio for this, because it will add the Byte Order Mark which is not supported by Azure. The easiest way to do this is by right-clicking the startup folder, and select “Open Folder in Windows Explorer”. Then right-click the folder’s contents, and add a new text document:


Rename it to installcmd.cmd. Go back to Visual Studio. In Solution Explorer select “Show All Files”:


The installcmd.cmd should appear, you can now right-click it and select “Include in project”. Edit it to the following contents:

Code Snippet
  1. %~dp0InstallPerfCounters.exe /q /log %~dp0pc_install.htm
  2. exit /b 0


Now open the ServiceDefinition.csdef file from your cloud project and add a startup task:

Code Snippet
  1. <Startup>
  2.   <Task commandLine="startup\installCmd.cmd" executionContext="elevated" taskType="simple" />
  3. </Startup>


This should take care of installing the performance counter. Now let’s use it in our worker role. First we need to create the performance counter instance, and then update it. In this simple example we’ll make the counter increment once each second. So implement the worker role’s run as follows:

Code Snippet
  1. public override void Run()
  2. {
  3.   // This is a sample worker implementation. Replace with your logic.
  4.   Trace.TraceInformation("UsingPerfCounters entry point called");
  6.   const string categoryName = "U2U";
  8.   PerformanceCounter secsRunning = new PerformanceCounter()
  9.   {
  10.     CategoryName = categoryName,
  11.     CounterName = "# secs/running",
  12.     MachineName = "." /* current machine */,
  13.     InstanceName = Environment.MachineName,
  14.     ReadOnly = false
  15.   };
  16.   var counterExists = PerformanceCounterCategory.Exists(categoryName);
  18.   while (true)
  19.   {
  20.     Thread.Sleep(TimeSpan.FromSeconds(1));
  21.     if (counterExists)
  22.     {
  23.       secsRunning.Increment();
  24.     }
  25.     Trace.WriteLine("Working", "Information");
  26.   }
  27. }


Publish this solution in Azure, not forgetting to turn on Remove desktop.


Also note that I turn in IntelliTrace, whichh is great for debugging those nasty deployment problems…

When you complete publishing you can now remote desktop to the instance and use PerfMon to look at your custom performance counter. Or you can use Azure Diagnostics….

Azure Toolkit released for iOS.

Reblogging from Wade Wagner, but I ‘m too excited just to include the link:


I’m extremely excited to announce the immediate availability of the Windows Azure Toolkit for iOS (v1.0.0).  This toolkit contains resources and services designed to make it easier for iOS developers to use Windows Azure.  This release pushes forward the momentum around Mobile + Cloud, and expands it such that we can make it easier to target Windows Azure by offering native libraries for non-Microsoft platforms.


You can see the official announcements here:

§ Official Microsoft Blog: Microsoft Announces Windows Azure Toolkits for iOS, Android and Windows Phone

§ Windows Azure team blog: NOW AVAILABLE: Windows Azure Toolkit for iOS

§ Wade Wegner’s blog: Getting Started with the Windows Azure Toolkit for iOS

Last March we released the Windows Azure Toolkit for Windows Phone 7 (see attached email), which not only provided a set of generic services for Windows Azure but also provided resources to build native applications for Windows Phone 7 that target those services.  Adoption and momentum has been steady and consistent – in fact, we released a v1.1.0 updated at MIX11 that included push notification support for WP7.  So far the WP7 toolkit has had around 2,500 downloads, two sessions at MIX11 that focused on the toolkit (here & here), a presentation at Mobile Connections, picked up by tech journals (e.g. Mary Jo Foley and InfoQ), and we have a slew of activities planned around TechEd (along with Windows Azure & Windows Phone MVPs).

You can get the Windows Azure Toolkit for iOS—and all the source code—on github:





The Windows Azure Toolkit for iOS works in two ways: you access Windows Azure storage directly, or alternatively, can go through a proxy service. The proxy service code is the same code as used in the Windows Azure Toolkit for Windows Phone 7 and negates the need for the developer to store the Azure storage credentials locally on the device. The release of the Windows Azure Toolkit for iOS is a significant milestone, and reinforces my opinion that Windows Azure is a great place to run services for mobile applications.

In addition to the iOS toolkit release, we are also excited to share the news that development for the Windows Azure Toolkit for Android has already begun – we expect to ship the first version next Month.


Lastly, at TechEd North America 2011 we will ship an updated version of the Windows Azure Toolkit for Windows Phone 7 (v1.2.0) which will include:

§ Integration with the Access Control Service (e.g. a wizard, automatic setup, tooling, and code)

§ Full support for Windows Azure Storage Queues

§ Updated UI/UX for the supporting web application

For more information, see my blog post: Updates Coming Soon to the Windows Azure Toolkit for Windows Phone 7.

Here are some resources to help you get started:

§ Blog posts:

o Getting Started with the Windows Azure Toolkit for iOS : Details how to get started using the Windows Azure Toolkit for iOS

o Updates Coming Soon to the Windows Azure Toolkit for Windows Phone 7 : What’s coming with the v1.2 release of the Windows Azure Toolkit for Windows Phone 7

o Using Windows Azure for Windows Phone 7 Push Notification Support : Details on how v1.1 of Windows Azure Toolkit for Windows Phone 7 provides out-of-the-box support for Push Notifications

o Windows Azure Toolkit for Windows Phone 7 1.2 will Integrate with ACS : Vittorio has blogged about upcoming details related to the Access Control Service 2.0 integration in v1.2

§ Screencasts:

o Getting Started with the Windows Azure Toolkit for iOS : Webcast showing where to get the bits and how to get started

o Deploying the Cloud Ready Packages for Devices : Webcast showing how to deploy the cloud ready package to Windows Azure

§ Windows Azure Toolkit for iOS:




§ Windows Azure Toolkit for Windows Phone 7:


§ Cloud Ready Package for Devices: