Hi, just a quick to say that my talk with .Net Rocks is now avail­able on their web site. In this talk I shared some insights into how F# is used in our stack to help us build the back­end for our social games, specif­i­cally in the areas of:

 

Enjoy!

Share

This is so good I keep going back to it, so to save myself and you the has­sle of search­ing for it every time I thought I’d share it here on my blog, enjoy! Smile

Share

Fol­low­ing my recent webi­nar with SharpCrafters on how to setup pseudo real-time per­for­mance mon­i­tor­ing using Aspect Ori­ented Pro­gram­ming and Ama­zon Cloud­Watch, I’d like to say thanks to the guys for hav­ing me, it was a great fun Smile

For any­one inter­ested, the source code is avail­able at:

http://aop-demo.s3.amazonaws.com/RTPerfMonDemo.zip

If you want to run the demo con­sole app to gen­er­ate some data, you need to put your AWS key and secret in the App.config file in the Demo.ConsoleApp project:

image

Just go to aws.amazon.com and cre­ate an account, you’ll then be given an access key and secret to use.

The slides for the ses­sion is also avail­able to down­load on SlideShare:

Enjoy!

Share

This is some­thing I’ve men­tioned in my recent AOP talks, and I think it’s wor­thy of a wider audi­ence as it can be very use­ful to any­one who’s obsessed with per­for­mance as I am.

At iwi, we take per­for­mance very seri­ously and are always look­ing to improve the per­for­mance of our appli­ca­tions. In order for us to iden­tify the prob­lem areas and focus our efforts on the big wins we first need a way to mea­sure and mon­i­tor the indi­vid­ual per­for­mance of the dif­fer­ent com­po­nents inside our sys­tem, some­times down to a method level.

For­tu­nately, with the help of AOP and AWS Cloud­Watch we’re able to get a pseudo-realtime view on how fre­quently a method is exe­cuted and how much time it takes to exe­cute, down to one minute intervals:

image

With this infor­ma­tion, I can quickly iden­tify meth­ods that are the worst offend­ers and focus my pro­fil­ing and opti­miza­tion efforts around those par­tic­u­lar methods/components.

Whilst I can­not dis­close any imple­men­ta­tion details in this post, it is my hope that it’ll be suf­fi­cient to give you an idea of how you might be able to imple­ment a sim­i­lar mechanism.

AOP

A while back I posted about a sim­ple attribute for watch­ing method exe­cut­ing time and log­ging warn­ing mes­sages when a method takes longer than some pre-defined threshold.

Now, it’s pos­si­ble and indeed easy to mod­ify this sim­ple attribute to instead keep track of the exe­cu­tion times and bun­dle them up into average/min/max val­ues for a given minute. You can then pub­lish these minute-by-minute met­rics to AWS Cloud­Watch from each vir­tual instance and let the Cloud­Watch ser­vice itself han­dle the task of aggre­gat­ing all the data-points.

By encap­su­lat­ing the logic of mea­sur­ing exe­cu­tion time into an attribute, you can start mea­sur­ing a par­tic­u­lar method by sim­ply apply­ing the attribute to that method. Alter­na­tively, Post­Sharp sup­ports point­cut and lets you mul­ti­cast an attribute to many meth­ods at once, and allows you to fil­ter the method tar­get by name as well as vis­i­bil­ity level. It is there­fore pos­si­ble for you to start mea­sur­ing and pub­lish­ing the exe­cu­tion time of ALL pub­lic meth­ods in a class/assembly with only one line of code!

Cloud­Watch

The Cloud­Watch ser­vice should be famil­iar to any­one who has used AWS EC2 before, it’s a mon­i­tor­ing ser­vice pri­mar­ily for AWS cloud resources (vir­tual instances, load bal­ancers, etc.) but it also allows you to pub­lish your own data about your appli­ca­tion. Even if your appli­ca­tion is not being hosted inside AWS EC2, you can still make use of the Cloud­Watch ser­vice as long as you have an AWS account and a valid AWS access key and secret.

Once pub­lished, you can visu­al­ize your data inside the AWS web con­sole, depend­ing on the type of data you’re pub­lish­ing there are a num­ber of dif­fer­ent ways you can view them – Aver­age, Min, Max, Sum, Count, etc.

Note that AWS only keeps up to two weeks worth of data, so if you want to keep the data for longer you’ll have to query and store the data your­self. For instance, it makes sense to keep a his­tory of hourly aver­ages for the method exe­cu­tion times you’re track­ing so that in the future, you can eas­ily see where and when a par­tic­u­lar change has impacted the per­for­mance of those meth­ods. After all, stor­age is cheap and even with thou­sands of data points you’ll only be stor­ing that many rows per hour.

Share

When you’re using the Ama­zon S3 client, have you come across the occa­sional excep­tion that says some­thing like one of these excep­tion messages:

“The request was aborted: The con­nec­tion was closed unexpectedly”

“Unable to read data from the trans­port con­nec­tion: A block­ing oper­a­tion was inter­rupted by a call to WSACancelBlockingCall”

“Unable to read data from the trans­port con­nec­tion: An estab­lished con­nec­tion was aborted by the soft­ware in your host machine “

If you do, then you’re prob­a­bly attempt­ing to return the response stream directly back to the rest of your appli­ca­tion with some­thing like this:

   1: var response = _s3Client.GetObject(request);

   2: return response.ResponseStream;

How­ever, because the stream is com­ing from the Ama­zon S3 ser­vice and is fed to your code in chunks, your code needs to ensure that the con­nec­tion to S3 stays open until all the data has been received. So as men­tioned in the S3 doc­u­men­ta­tion (which inci­den­tally, most of us don’t read in great details…) here, you should be wrap­ping the response you get from the GetO­b­ject method in a using clause.

Depends on what it is you want to do with the stream, you might have to han­dle it dif­fer­ently. For instance, if you just want to read the string con­tent of a text file, you might want to do this:

   1: using (var response = _s3Client.GetObject(request))

   2: {

   3:     using (var reader = new StreamReader(response.ResponseStream))

   4:     {

   5:         return reader.ReadToEnd();

   6:     }

   7: }

Alter­na­tively, if you want to return the response stream itself, you’ll need to first load the stream in its entirety and return the loaded stream. Unfor­tu­nately, at the time of this writ­ing, the AWSSDK library still hasn’t been migrated to .Net 4 and there­fore doesn’t have the uber use­ful CopyTo method added in .Net 4, so you will most likely have to do the heavy lift­ing your­self and read the data out man­u­ally into a mem­ory stream:

   1: using (var response = _s3Client.GetObject(request))

   2: {

   3:     var binaryData = ReadFully(response.ResponseStream);

   4:     return new MemoryStream(binaryData);

   5: }

   6:

   7: /// <summary>

   8: /// See Jon Skeet's article on reading binary data:

   9: /// http://www.yoda.arachsys.com/csharp/readbinary.html

  10: /// </summary>

  11: public static byte[] ReadFully (Stream stream, int initialLength = -1)

  12: {

  13:     // If we've been passed an unhelpful initial length, just

  14:     // use 32K.

  15:     if (initialLength < 1)

  16:     {

  17:         initialLength = 32768;

  18:     }

  19:

  20:     byte[] buffer = new byte[initialLength];

  21:     int read=0;

  22:

  23:     int chunk;

  24:     while ( (chunk = stream.Read(buffer, read, buffer.Length-read)) > 0)

  25:     {

  26:         read += chunk;

  27:

  28:         // If we've reached the end of our buffer, check to see if there's

  29:         // any more information

  30:         if (read == buffer.Length)

  31:         {

  32:             int nextByte = stream.ReadByte();

  33:

  34:             // End of stream? If so, we're done

  35:             if (nextByte==-1)

  36:             {

  37:                 return buffer;

  38:             }

  39:

  40:             // Nope. Resize the buffer, put in the byte we've just

  41:             // read, and continue

  42:             byte[] newBuffer = new byte[buffer.Length*2];

  43:             Array.Copy(buffer, newBuffer, buffer.Length);

  44:             newBuffer[read]=(byte)nextByte;

  45:             buffer = newBuffer;

  46:             read++;

  47:         }

  48:     }

  49:     // Buffer is now too big. Shrink it.

  50:     byte[] ret = new byte[read];

  51:     Array.Copy(buffer, ret, read);

  52:     return ret;

  53: }

Share