Monday 22 February 2016

Coarse Grained CPU-Profiling for Applications using Process Explorer

Today I wanted to quickly graph an applications CPU performance and compare how different start-up flags affected the CPU performance of the application. I didn't want to open perfmon.exe and go through the rigmarole of selecting the correct perf counters yadda-yadda.. I just wanted something quick and dirty..

As we all know, Process Explorer is the swiss-army knife for all things process based. If you need to do any diagnostics on a process then chances are Process Explorer will be able to help in some way.

Enter Process Explorer with its "Performance Graph" tab and this quick little tip!

With Process Explorer running, go to the process in question and select the "Performance Graph" tab. You will see something like this:

This tab displays the "CPU usage" for the process, the "memory usage" and the "disk I/O". All useful information. As an aside, watching to see if the memory usage increases is a basic way to see if your app has a memory leak.

I was interested in the CPU usage for this process which is the top graph. With the mouse you can hover over the graph to get a time and a CPU usage number but the 3 guidelines show you the 25%, 50% and 75% marks for rough approximation.

As you can see in the image above, the application is behaving quite nicely. For the majority of time it is idling under the 25% mark. Now, let’s alter some of the start-up flags, restart the application and compare the results from the graph below with the graph above.

You can clearly see that these settings increase the CPU usage for this process as the CPU now consistently hovers around 25% mark for the duration of the run.

One thing to be weary of is that these graphs don’t update if Process Explorer is minimized to the tray..

But hey... Like I said, its quick and dirty but that’s good enough for me today!


Contact Me:  ocean.airdrop@gmail.com

Sunday 21 February 2016

To-do or Not-to-do?.. That is the question.

"Note to Self: Remember to write a to-do list"

As a full-stack, dev-ops software developer we have to juggle a lot of moving parts. We work on multiple projects with multiple time-frames in addition to looking after servers, performing system-administration and keeping up with the constant changes in the tech landscape.

In short, we can have a lot going on at any one time.

Who doesn’t love a good to-do list?

Over the years I have used everything from pen-and-paper to test driving the latest tools of the moment to try and keep up-to-date and on top of what I need to do. Everyone out there might have their own system, but if there is one thing we can all agree on, its the importance of keeping some kind of to-do list.

Choosing what to-do from your to-do list

But writing the to-do list is only half the battle.

We’ve all been there. You finish collating everything you need to do, and then, inevitably you sit back and think "Gee. Which one of these should I be doing first?"

Usually it’s the task you leave until last. The big one. The gnarly one. That’s usually the task you should be starting first.

It’s as simple as this. If you have a good to-do list and are able to prioritise your work then you will be more effective in your job as you will be working on the "right" things.

I can’t count the number of times I have been "busy" working on the wrong things. I’m sure we’ve all done it at some point. Spend ages on a small cosmetic bug while there is a humongous bug just sitting there waiting for us to start it.

Identifying Priority's: The Eisenhower Priority Matrix

The biggest problem I have is identifying "what I should do next". Out of everything I have to do, what is my next highest priority task? That’s where the Eisenhower Priority Matrix comes into play. It identifies tasks based on their importance and their urgency.

If you do a Google search for "Eisenhower principle" you will most probably stumble upon this famous quote of his:

"What is important is seldom urgent and what is urgent is seldom important"

His matrix is simple and it goes like this:

  • If it’s important and urgent you need to do it now.
  • If it’s important but not urgent you should plan in time to do it

As an example, if you have identified that your car tax is due (a very important task) but your house is on fire (a very urgent and important task to resolve), I don’t think many people would have trouble identifying what they need to do first. Quick!!!! Phone the fire brigade immediately and run back into the house to save the old Amiga 500, Nintendo and original Xbox! (Actually, the original Xbox could probably survive a nuclear blast but that’s another story)…

Unfortunately, when we scan through our day-to-day to-do list, our items usually don't jump off the page like a burning house.

I like the definition for the Eisenhower Priority Matrix and how to use it provided on the MindTools website. Go here and have a read [here]

Like I say, there are countless to-do apps out there but if you wanted to incorporate the Eisenhower principal in your own to-do list then below is a simple example.

public double CalculatePriortyValue()
{
    CalculatedPriority = 0;
    if ( this.IsComplete == 1)
        return CalculatedPriority;

    // Minus 1 day so that you finish this task a day before its due
    double daysDueIn = ((DueDate - DateTime.Now).TotalDays - 1);

    if (daysDueIn < 1)
    {
        // This date is in the past. flip the value to a positive.  
        // The further in the past it is the more urgent it is.
        daysDueIn = Math.Abs(daysDueIn);

        if ( Math.Abs(daysDueIn) < 0.1)
            daysDueIn = 1;

        // Multiply importance by Urgency then multiply it by the number of days in the past it is!
        // The further in the past it is the later the task is and bigger the priority.
        CalculatedPriority = (Importance * Urgency) * daysDueIn;
    }
    else
    {
        // Multiply the importance and urgency then 
        // divide it over the number of days
        // we have got to do it in.
        CalculatedPriority = (Importance * Urgency) / daysDueIn;
    }

    return CalculatedPriority;
}

Revising your to-do list

The hardest part of any to-do list is keeping it up-to-date. A to-do list is no good if it’s not updated. I know this from experience. I’ve gone through cycles of using them then forgetting about them. However, for your to-do list to be effective you need to live inside it. You need to update it constantly so you know "what’s next". If that’s ticking boxes on a piece of paper or using the latest fandangled mobile app, if you’re not updating it “you’re not doing it right”.

The "Not" to-do List

This brings me nicely onto the not-to-do list.

You see, the great thing about the software dev landscape is that there is always "something new to learn". However, the bad thing about the software dev landscape is that there is always "something new to learn"!

If you’re like me, you probably have lots of new software dev areas that you want to learn and explore. It’s quite common for me to constantly feel like I am playing “catch up” because the amount of “stuff” to learn out there has exploded.

Let’s face it. It’s simply impossible for you to learn everything. There aren't enough hours in the day!

So you end up carrying around a lot of mental "to-do" cards. Things in the back of your mind that you think you should be doing but in reality haven't got the time for. Truth is, you’re probably never going to get the time to do everything. Time is against all of us.

This is where the not-to-do list comes into play and in my mind is just as important as the to-do list.

Think of the not-to-do list as a way of clearing your mental cache.

Before, you would stick X, Y or Z on your to-do list and it would forever expand. You would feel as if you are drowning under the stuff you need to do. But the truth is your never going to get around to doing everything on that list. The tasks that are “ideas”, “stuff to learn”, “nice-to-have features” all should be added to the “not-to-do list”. If it doesn’t have a due date, stick it on the not-to-do list. It can’t be that important.

Before, you ended up carrying around a lot of mental cards in your head, but by adding these items to your not-to-do list your mental cache is cleared. The fog dissipates and you can focus on the important things. At the same time you haven’t forgotten about these tasks. Because the not-to-do list will keep a record of them for you.

Now.... Note to Self: Remember to write my to-do list"

Contact Me:  ocean.airdrop@gmail.com

Sunday 7 February 2016

C# Dynamic Types & Expando Object

Let me get this straight!! You want to declare an Object but you don't know what member variables you need ahead of time. Have you gone mad?

JavaScript Land

You've got to love the dynamic nature of JavaScript and how the language allows you to mutate objects at run-time. You don't need to define class definitions of an object upfront. That's so old-skool!

You can just create them... on the fly!
var movie      = new Object();
movie.name     = "Interstellar";
movie.genre    = "Sci-Fi";
movie.director = "Christopher Nolan";
Later, if you want to add a new member variable or a function at run-time.. Just add it!
movie.Rating = 8;
And of course you could do the same thing using the object literal notation:
var movie2 = {
  name: "Aliens",
  genre:"Action",
  director:"James Cameron"
};
and later:
movie2.Rating = 9;

C# Land

In C# land, things are a little different. A bit more traditional. Our objects need to be declared up front with a class definition. And the blueprint we provide for our classes are very hard to change. I guess they don't call them concrete classes for nothing!.

However, there are times when you would like to have the flexibility of JavaScript in C# and be able to "mutate objects on the fly".

With C# Version 4 that became possible with the dynamic keyword and the ExpandoObject.

Check out this C# code sample below. Its eerily similar to the JavaScript above. The only difference is on the 1st line with the dynamic keyword.
dynamic movie  = new ExpandoObject ();
movie.name     = "Interstellar";
movie.genre    = "Sci-Fi";
movie.director = "Christopher Nolan";
In essence, the dynamic ExpandoObject lets you add (expand) and remove (contract) member properties & functions of an object.

However, if you look at the above code, even though we haven't created a traditional "class definition" for the movie object, the fields are still setup at compile-time. The "name", "genre" and "director" fields are typed into the code at compile-time. Not run-time.

This brings us onto the second cool thing about the ExpandoObject.  It allows you to create an object like above using a dictionary. This means we can dynamically modify an object at run-time, adding or removing properties as required.

For example:

dynamic movie  = new ExpandoObject ();
movie.name     = "Interstellar";
movie.genre    = "Sci-Fi";
movie.director = "Christopher Nolan";

// Later on, lets add some more property via a dictionary
var dictionary = movie as IDictionary ;
dictionary.Add("Rating", "PG-13");
dictionary.Add("ReleaseDate", "2014-11-07");
dictionary.Add("BlahBlah", "SomeValue");

// Opps! Lets remove the field BlahBlah.. 
((IDictionary)movie).Remove("BlahBlah");

Example Usage

So armed with this knowledge, how can we make use of this? Well, a perfect use-case for this is when sending JSON data from a sever to the client. Using this dynamic object the server could tailor the data returned to the client, based on its requirements.

For example, this helper function "ConvertToDynObj" will convert any object to a dynamic ExpandoObject

public static dynamic ConvertToDynObj(object data, List fieldsToCopy = null )
{
    var dynObj = new ExpandoObject() as IDictionary;

    foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(data.GetType()))
    {
        if (fieldsToCopy == null )
        {
            dynObj.Add(property.Name, property.GetValue(data));
        }
        else if (fieldsToCopy.Contains(property.Name) == true)
        {
            // we only want to include this field in the new dynamic type
            dynObj.Add(property.Name, property.GetValue(data));
        }
    }

    return dynObj;
}
With the helper function defined above, we can mutate existing objects in our class library. Lets say we have a business object defined in our system that looks like this:
class BusinessDuberie
{
    public int fieldOne { get; set; }
    public string fieldTwo { get; set; }
    public double fieldThree { get; set; }

    public BusinessDuberie()
    {
        fieldOne = 1; fieldTwo = "2"; fieldThree = 3.3; 
    }
}
We can now convert the object to a dynamic object wholesale like this:
BusinessDuberie obj = new BusinessDuberie();

var dynObj = ConvertToDynObj(obj);

// Now we can add properties on the fly

(dynObj as IDictionary).Add("Qwerty", "Berty");

We could also trim the object and only copy the properties we are interested in.

List fieldsToCopy
fieldsToCopy.add("fieldOne");
fieldsToCopy.add("fieldThree");

var dynObj = ConvertToDynObj( obj, fieldsToCopy );
Now that's what I call sweet.

Contact Me:  ocean.airdrop@gmail.com

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages