Speed up your workers with .net 4.0 parallel extensions

Now you can use .net 4.0 in Windows Azure, it opens up some possibilities to get even more out of your workers. If you have workers that read messages from queues, then do some processing against storage, you may want to consider converting your message processing loops into parallel loops.

Here is the shell of some code and a typical pattern:

foreach (var s in masterList)
{
    // Do some work with s
}

You can very easily convert this to use the parallel extensions by using the following code:

Parallel.ForEach(masterList, s =>
{
    // Do some work with s
}
);

So the code change is easy, but what is the difference?

Well you should certainly consider how much work you are doing for each message – short simple tasks probably won’t benefit from being parallel.

You should also consider how much IO is performed against storage etc. – the more IO performed the more you will benefit from using the extensions.

But David PLEASE GIVE ME SOME FIGURES.

Ok – here is the low down.

The code I have reads some data from a collection (loaded from a blob) then writes a row into table storage for each item in the collection. There is a total of 748 items in the collection spread across 7 partitions.

Without the parallel extension the results look like this across 3 identical runs:

  1 2 3
Start Time 13:55:01.4366386 14:04:59.5006458 14:08:37.6709250
End Time 13:55:36.2911237 14:05:28.4387517 14:09.20.3591933

No I don’t know what happened to the last one, but its around ~35 seconds or about 21 entities per second.

With the parallel extensions the results are:

  1 2 3
Start Time 14:23:51.2536114 14:28:54.6648367 14:31:58.3643092
End Time 14:24.03.0634712 14:29:08.3145661 14:32:10.2021354

That is a tad faster at ~14 seconds or about 53 entities per second (probably faster as I’m being generous here).

The tests were all done using a small VM size (1 core) and the tables etc. were deleted between tests. It’s not terribly scientific, but its good enough to show its worth looking into.

For a simple code change, that is not a bad days work.

You can find out some more about the extensions at http://msdn.microsoft.com/en-us/library/dd460693.aspx as well as the C9 10-4 show at http://channel9.msdn.com/shows/10-4/10-4-Episode-6-Parallel-Extensions/.

THIS POSTING IS PROVIDED “AS IS” WITH NO WARRANTIES, AND CONFERS NO RIGHTS, NOBODY EVER READS THIS BIT EITHER SO I DON’T EVEN KNOW WHY I BOTHER SOMETIMES

2 thoughts on “Speed up your workers with .net 4.0 parallel extensions

Comments are closed.