23
Oct
12

how do I profile an Azure worker role?

Recently I was tasked with speeding up an Azure Worker Role.  Once a night, a Worker Role would recalculate rates for each customer using data in their database.  Some customers had a lot of data and the Worker Role would run for more than 16 hours recalculating their data.  This Worker Role needs to be sped up, but where to start?

The easiest way to figure out where to start looking for optimizations is to profile the app.  How do you do that you say?  Profiling an Worker Role running in Windows Azure is actually pretty easy.

The Sample App

The Sample App provided is extremely small and does basically nothing.  It’s just an example that can be used to show off profiling.

In the solution, there are the following projects:

  • RecalculatorService – the Azure Service
  • RecalculatorWorkerRole – the Worker Role for the service
  • Recalculator – a separate assembly that contains the Recalc class that the Worker Role will use

As the Worker Role runs, it will spin in a loop, constantly creating an new instance of Recalc and calling Recalculate on it.

public override void Run()
{
    Trace.WriteLine("RecalculatorWorkerRole entry point called", "Information");

 

    while (true)
    {
        Recalc recalc = new Recalc();
        recalc.Recalculate();
        Trace.WriteLine("Working", "Information");
    }
}

Here is the Recalc class in its entirety:

public class Recalc
{
    public void Recalculate()
    {
        Debug.WriteLine("Recalculating");
        foreach (DataValue value in values)
        {
            value.Calculate();
        }
    }

    private readonly List<DataValue> values = new List<DataValue>()
    {
        new DataValue(), new DataValue(), new DataValue()
    };
}

And here is the DataValue class:

public class DataValue
{
    public void Calculate()
    {
        Debug.WriteLine("Calculating");
        short int16MaxValue = Int16.MaxValue;
        for (int i = 0; i < int16MaxValue; i++)
            Debug.WriteLine(String.Format("Calculating... {0}", i));
    }
}

Nothing too fancy.  When Recalculate is called, it will loop through its list of DataValues and call Calculate on every one.  The Calculate method of DataValue just loops and writes a message out to Debug.

Prerequisites

If you’re using Visual Studio 2010, you will need Visual Studio Premium or Ultimate.  If you’re using Visual Studio 2012, every version except Test Professional should have the profiling tools available.

This post will be specific to Visual Studio 2010.

You should also have the Windows Azure 1.7 SDK and any of its dependencies.  You can get this through the Web Platform Installer.

You may also want to setup your _NT_SYMBOL_PATH, but it shouldn’t be necessary for this post.

Profiling Locally

Now that we’re all setup and ready to go, open the sample solution and make sure it builds.  Next, make sure that RecalculatorService is set as the Startup Project and run it from Visual Studio.

startupproject

Running this project locally will start the Compute and Storage Emulators on your machine.  When everything is loaded, you’ll start seeing messages in the Debug Output window as I runs.

debugoutput

Now that the service is running, we can profile it.

Open up the Performance Explorer window (View –> Other Windows –> Performance Explorer) and click the Attach button.

perfexplorer

This will open up a window where you can select a process for the performance profiler to attach to.  Scroll way down until you find “WaWorkerHost”, select it and click attach.

attachToWorker

Once it has attached, it will open a view saying that it is profiling.

profiling

Once it’s had a chance to run for a few minutes, click the “Stop profiling”  link.  It will stop the application running in the Azure emulator and prepare a report.

If everything is setup correctly, you should see the profiling report appear.

local profiling report

When you profile the Azure application locally by attaching to it like we did above, you can profile it in two ways: Sampling and Concurrency.

The profiling report we generated we generated is a Sampling report.  This profiling method “samples” the application periodically to see what the application is doing.  It’s non-invasive and can give you a good feel on where to start looking for optimizations because it will tell you where the application seems to be spending most of it’s time.

A Concurrency profiling report will show you what your application threading looks like.  It can be used to see where an application might be blocking.  It can be generated using the same method as above, but when you go to Attach to the WaWorkerHost, you’ll have to create a new Performance  session and select Concurrency in the pull down before clicking the Attach button.

perfexplorer_concurrency

Profiling in the Cloud

Profiling in the cloud is just a little bit easier than profiling locally.  In order to profile in the cloud, you must use Visual Studio to publish your application to the cloud.

When you are going through the Publish wizard, go to the Advanced Settings tab and enable profiling.

publishsettings

You can also go into the profile settings to pick which type of profiling you want to use.

publishprofilesettings

Once you have that setup and your application is published to Azure, you can right click on your service instance in the Server Explorer and select “View Profiling Report”.

viewprofreportinazure

After that, it’s just the same as before.  Visual Studio will download the profiling report, analyze and open it.

Simple, right?

Wrapup

In this post, we’ve seen how we can profile and Azure application both locally and in the cloud.  If you’d like to read more about profiling in general, MSDN has some good documentation.

30
Mar
12

windows 8 on a samsung series 7 tablet

Recently, I’ve been wanting to dig into building windows 8 apps.  Since I didn’t really have any spare hardware to use, I decided to buy a new machine for it.

Ideally, the new machine would be able to run windows 8 (with the snap feature), have a touch screen and be reasonably fast.  The only thing I could find that fit all of those requirements was the Samsung Series 7 slate.

Sure, it’s not the same as the one given out at the //Build conference, but it is pretty much the same.  The features that are missing from it are:

  • Gyro Sensor
  • NFC Sensor
  • GPS
  • HSPA

Pretty much everything else is the same. 

So, off I went to the Microsoft store…

I have to admit that I hate spending money, especially when it comes to computers.  My usual MO is to put something in the cart and talk myself out of it before I get to the check out.  This usually takes about 5 minutes.  So, if I’m not paying for it within 5 minutes of grabbing an item off of the shelf, I usually put it back.

I pretty much had only one option for what I wanted, the Samsung Series 7 Slate, so I bought one.  The Microsoft store had a pretty good deal going when I bought mine: $1299 for the slate, the dock and a $200 gift card.

I ended up using the gift card for the protection plan (aka “the kid protection plan”) and a single license of Office Home/Student came with it.  Cool!

Now the deal is done but I’m feeling regretful.  This is normal for me.  Until I get comfortable with what I’ve bought, I have an uneasy feeling.

Once I’m back in the office, I fire up the tablet and play around with it.  Almost immediately, I hated the experience.  Windows 7 on a tablet just doesn’t cut it. 

This is my first tablet.  I don’t have an iPad or an Android tablet, so I’m coming to this new form factor as a newbie.  The “extensions” (or whatever you want to call them) that were shoved into Windows 7 are clunky. 

Don’t get me wrong, sometimes they are fantastic (the journal app, woot!).  Mostly, though, they are teh lame.

That sinking feeling of wasting money planted itself firmly in my stomach 🙁

Anyway, getting back to the point…I was wanting to write some Windows 8 apps…so I installed the customer preview.

That sinking feeling has completely vanished. 

I love Windows 8.  It runs super great on this machine.  Sure, it has some things that you need to get used to, but once you do, it’s awesome.

I can’t stop playing around with this thing now. 

Anyway, I guess the point of this post is that if you haven’t tried Windows 8 or are on the fence for buying a Windows tablet to try it out, I say go for it.  I’m loving mine.

26
Feb
11

Wp7dev: ad control during development

I forget this all of the time, mainly because I’ve only done it 5 times (the number of apps I’ve submitted using the ad control).  This is for Silverlight projects only.  I haven’t done any XNA on the phone yet, so it might be different.

So, to use the ad control during development:

  1. Add a reference to Microsoft.Advertising.Mobile.UI.dll to the project and add the AdControl to a page.
  2. Set the AdModel to Contextual.
  3. Set the AdUnitId to Image480_80
  4. Set the ApplicationId to test_client

This is what you should see in Blend when it’s all setup:

Blend_AdControl_setup

Oh, and here’s a tip that will hopefully save someone some time.  If you’re adding in the AdControl at the end of your development, make sure you re-run CapabilityDetection to make sure you have all the capabilities you’ll need to display ads. 

If you don’t your AdControl will disappear and you’ll see these errors when debugging:

A first chance exception of type ‘System.UnauthorizedAccessException’ occurred in Microsoft.Phone.dll
A first chance exception of type ‘System.UnauthorizedAccessException’ occurred in Microsoft.Phone.dll
‘taskhost.exe’ (Managed): Loaded ‘System.SR.dll’
A first chance exception of type ‘System.IO.DirectoryNotFoundException’ occurred in mscorlib.dll
A first chance exception of type ‘System.IO.IsolatedStorage.IsolatedStorageException’ occurred in mscorlib.dll
‘taskhost.exe’ (Managed): Loaded ‘System.Windows.debug.resources.dll’
A first chance exception of type ‘System.Net.WebException’ occurred in System.Windows.dll
A first chance exception of type ‘System.Net.WebException’ occurred in System.Windows.dll
A first chance exception of type ‘System.Net.WebException’ occurred in Microsoft.Advertising.Mobile.UI.dll

 

17
Dec
10

WordPress for Windows Phone 7

I’ve never really liked the idea of blogging from a phone. I tried it on my old iPhone once, but the experience was terrible.

If it was so awful, why try again? Well, I find that I do have ideas for posts while at work, but if I dont at least get a draft done, I won’t do anything with it later or just flat out forget.

Here’s what the experience has been like so far, not much better than the last time I tried this.

First, setup was a pain. I kept getting some “Data at the root level is invalid…” when adding a self hosted site. The reason for this was I wasn’t typing in the full domain name (www.mattdavid.net). Once I did, it worked. Also having to type in the xmlrpc.php bit at the end was something the software should’ve handled for me.

<<the rest of the post was finished on the web>>

Creating and editing post is pretty bad.  I had to stop and finish this post on the web.   When creating a post, it apparrently defaults to publish.  While I thought I was saving a draft, it was published instead.  Whoops. 

Next, when typing it doesn’t have auto-correct or the helpful predictive text bar (or whatever you call it).  I have been spoiled by those things.  It was painful for me to go back and correct all of the “i”s and the “wasnt”s, etc. 

Lastly, if you want to go back and edit a post that doesn’t all fit into the textbox, I hope what you need to fix is visible.  I surely couldn’t figure out a way to scroll down to fix the rest of this post.

I’m waiting for an update before I try that again.

Not Posted from WordPress for Windows Phone

14
Dec
10

Color coordinated

Next time you need to find colors that go well together, check out kuler.  It’s an Adobe site dedicated to creating, sharing and exploring color schemes.

13
Dec
10

Time for a reboot

I guess I’ve put it off long enough, it’s time for a reboot.

I never really updated this blog with any regularity before, mainly because I felt like I had to post about something new or whatever.  Yeah, I don’t have the kind of time to be on the bleeding edge of anything.

So, this will mainly be a place for me to brain dump.  It’s not going to be new stuff by any stretch of the imagination (I am a bit behind).  If you read this blog, you’ll probably would’ve seen whatever’s on it before.  Probably written better, too.