October 2007


On to choice 3 from my post a while ago. 

Before finishing off the exercise… LEt’s talk a little bit about LWG.  In .Net 2.0 you can create static function on the fly.  Essentially you are building in IL (think assembly language) as set of instructions on the fly.  You then can create a handle to the function you’ve created (called a delegate in .Net).  And away you go. 

I’m enclosing my sample code here in this post.  In this sample I’m generating 2 functions.  One that invokes a static method and one that creates an instance of a class and then invokes a method on it.  In both cases we are not dealing with arguments on the method (for simplicity’s sake).

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
namespace LWG
{
    class Program
    {
        static void Main(string[] args)
        {
            Sample sample = new Sample();
            sample.Get(); Sample.StaticGet();
            //These methods use LWG to generate a method and invoke it.
            Method();
            StaticMethod();
        } 

        //Define the format of the function handle
        public delegate void Dispatch(); 

        static void Method()
        {
            //Get information about a constructor. Instead of hardcoding the type in production you could pass it in by name.
            //This call is quick.
            ConstructorInfo ctorInfo =
            typeof(Sample).GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
            Type.DefaultBinder,
            new Type[] { },
            null);
            //Lookup information about a specific type.
            MethodInfo mInfo = typeof(Sample).GetMethod("Get");
            //Now build the static method.
            DynamicMethod method = new DynamicMethod("SampleGetCall", null, null, typeof(Sample), true);
            ILGenerator generator = method.GetILGenerator();
            //Declare a local type of type "sample"
            LocalBuilder local = generator.DeclareLocal(typeof(Sample));
            // New the described object
            generator.Emit(OpCodes.Newobj, ctorInfo);
            //Store the object in the local type I created (I think that's what it is doing here)
            generator.Emit(OpCodes.Stloc_0);
            // Load the object to the top of the stack.
            generator.Emit(OpCodes.Ldloc_0);
            // call the method on the object
            generator.Emit(OpCodes.Callvirt, mInfo);
            //Finish the method
            generator.Emit(OpCodes.Ret); 

            //Create a handle to the newly generated function
            Dispatch dipatch = (Dispatch)method.CreateDelegate(typeof(Dispatch)); 

            //Invoke it. (or you could keep it around)
            dipatch.Invoke();
        } 

        public static void StaticMethod()
        { //LWG
            MethodInfo mInfo = typeof(Sample).GetMethod("StaticGet"); 

            DynamicMethod method = new DynamicMethod("SampleStaticGetCall", null, null, typeof(Sample), true);
            ILGenerator generator = method.GetILGenerator();
            generator.Emit(OpCodes.Call, mInfo);
            generator.Emit(OpCodes.Ret); 

            Dispatch dipatch = (Dispatch)method.CreateDelegate(typeof(Dispatch)); 

            dipatch.Invoke();
        }
    } 

    class Sample
    {
        public Sample()
        {
            //Default Ctor
            Console.WriteLine("Sample Ctor called.");
        } 

        //SImulate the get method.
        public void Get()
        {
            System.Console.WriteLine("Get invoked.");
        } 

        public static void StaticGet()
        {
            Console.WriteLine("Static Get Called");
        } 

    }
}

So a friend of mine is trying to get tickets to the world series… out in Colorado.

Apparently they had to shut down the ordering system:

http://cbs4denver.com/topstories/local_story_295083342.html

 I recently read that there is software out there that essentially let’s the scalpers get ticket by booting people’s ip addresses off the ordering system.  Not sure how it works but it’s still annoying to see…

Man!  It’s been really annoying at my home lately.  I’ve killed like 12 flies in the last few days and have scooped up 6 dead mice!  I’m thinking the 2 are related (apparently some mice got into our house and we called an exterminator).  I went to my shed and got on the lawn mower/tractor and started mowing and at one point looked down by my feet to see a mouse looking up at me.  It had been making a nest in the engine!   DAMN you foul rodents!

Apparently there was a minor earthquake in Littleton last nite.  I wonder how often this type of thing happens in the North East.  You can check out the latest here:

http://www.boston.com/news/local/massachusetts/articles/2007/10/19/magnitude_25_quake_overnight_in_massachusetts/

Maybe I can use it as excuse to stay home from work :P

Have you heard of Ruby?  How about Rails?  Well I am taking the plunge to learn these technologies.  So I hope to post about my trials and tribulations over the next few months (because of work and home life it’s going to go slower than I’d want). 

Ruby is a dynamic language that’s pretty popular these days.  I think dynamic languages have the ability to help out a LOT when it comes to unit testing and TDD in general. 

 Rails is a Model View Controller web framework.  I just think an MVC web framework is cool.  I’ve been doing some ASP.net stuff latetly and it has a lot of limitations. 

As I learn each of these technologies I’ll post more!

I really have to tip my hat off to James over at Aces.  A post a day is pretty hard.  Anyway life is just pretty darn busy these days.  I just found out that my sister-in-law’s husband has cancer of the esophagus.  He’s only 40.  Here’s hoping that his doctor’s appointement tomorrow goes well.

At work we are using a REST style of interface for requesting data of a web endpoint.  What does that mean?  Well let’s say we have a service that needs to access some data from a central site that has a database.  At work we are using something like:

HttpWebRequest request = WebRequest.Create("http://hostname/asset/getassetinfo/39394") as HttpWebRequest;
HttpWebResponse = request.GetResponse();

In this snippet the thing of interest is everything in the url after hostname. In our case it breaks down like this:
When we get to the host, the word asset gets matched to a class in my server called AssetHandler. the getassetinfo is a method on AssetHandler and 39394 is the asset identifier I want more information on.

So the challenge in the web server is how do I do the mapping? For the sake of this discussion let’s assume that we have routed the request to my AssetHandler class, and in this class we take the method name and argument(s) and translate them into a method invocation.

The question is how should we do this? Well in C# (which is what we are using) I’ve known there were 2 ways, but I’ve recently learned of a third and I thought I’d talk a little about them.

So first let me enumerate the 3 ways:

  1. A switch statement/lookup
  2. Reflection
  3. Lightweight code generation

So as a developer I need to figure out which one to use and here is my thinking:

Switch statement:
With a switch statement we have easily readable code, at least at first we do. If my AssetHandler starts support more and more methods the code becomes a little less maintainable. However the tradeoff is that it’s pretty quick. And I need to know how many requests per second I need to handle. Here is a code snippet of my switch statement:


public void Dispatch(HttpContext context)
{
//assume we have already parsed out the URI into it's parts and it's available in the context
switch (context["method"].GetMethodName())
{
case "getassetinfo":
//pull asset id out of the context and invoke the method.
this.GetAssetInfo(context["method"].GetArgument("assetid"));
break;
}
}

I’m not a big fan of switch statements so I went on to do some method dispatching via Reflection.


public void Dispatch (HttpContext context)
{
MethodInfo info = this.GetType().GetMethod(context["method"].GetMethodName());
info.Invoke(this, context["method"].GetArguments());
}

(Forgive me if I am leaving a lot out I hope to add to this over the next few days).
Reflection is nice. It makes the code more maintainable (I can extend the class with methods without having to extend a switch statement, I can change a method’s name without having to affect the dispatching etc). A big downside is that Method.Invoke is EXPENSIVE. And if your request endpoint is being called MANY times a second then your performance could suffer.

So I just recently learned that I may be able to do the same thing with Lightweight Code Generation in .Net…
So in a post coming up (I still have to implement the solution) I’ll show you what I did and hopefully I’ll have some metrics to share too.

My good friend Brian mentioned that he was using scrum and trying to get a handle on “metrics.”  Well I thought I’d throw out some of the things we do and how we look at metrics and let Brian offer up some insight on what type of metrics he thinks are pertinent.

For us metrics fall in 2 categories… intersprint and intrasprint.  But before talking about metrics it would behoove me to talk a little bit about how we run our sprints.

So after doing story points, we have the product owner prioritize those stories in order of priority.  Then as a team we look at the Top N and create subtasks and estimate those sub tasks.  If we think we can fit the N stories into the sprint we do so.  If not a little bit of negotiation needs to happen.  Either way, we look at the length of a sprint, in our case it’s usually 3 weeks.  We look at ensuring that ALL development is done at the 80% mark (day 12).  After the 80% mark, the only things we can do (as developers) is either fix bugs or put on our QA hats and help the QA team test.  For the 12 days of work that we have to develop we estimate that we really will only be able to get 50%-60% of that total done in estimated tasks.  Essentially we only commit to being able to do tasks that total up to 6 days of effort.  At this point we start the sprint.  If we get it all done we can pull some tasks in from the backlog OR commit to doing a little more work the next sprint.  

I call this our Velocity.  you might be saying 50% seems low, but with meetings, unexpected bug fixing and other things it actually goes pretty fast. 

Now for the metrics.  It really comes down to stories, how many story points can we get done in a sprint?  and over time we’d like to see that number go up (but realizing there IS a ceiling).  Finishing a story means development, unit testing, integration testing, White box, black box testing, and bug fixing.  It’s as a team that we succeed or fail.  there is no “It’s development’s fault” or “it’s QA’s fault”.  (Which I Find cool).

So story “points” per sprint is a metric, also the 50%/60% number is a metric.

Within a sprint we use a burndown chart.  All the tasks we sign up for goes into a tracking system as “total number” of hours of work for the sprint.  As you complete tasks  or work on tasks(you try to have them estimated to a granularity of hours not days) you update the tracking system with time worked.  And you use the burndown tool to see roughly where the team is at. 

I’m sure as time goes on we will be able to see #of bugs created and closed per sprint… But that’s really secondary.

Today at work we had a sprint planning meeting.  Before I go on let me preface with a couple of “definitions”.  At work our product developement process is called Scrum.  As a team we grab a set of Stories that we think we can build and test in a fixed amount of time (a sprint) which is typically 3 weeks long.  At the end of the sprint what we commit to needs to be completed and fully tested.  If you develop software, I find the scrum process really exciting.  It gives you and the product owner more control over developing a product.

As you may guess, one of the challenges is figuring out how much you can get done in a sprint, sometimes the stories are too vague, sometimes your eyes are bigger than your stomach, and you don’t know how much time will be taken up by meetings and other stuff.  Today we started planning for the next sprint and what we decided to do was look at all the stories and assign point totals to them.  The point totals represent relative complexity of the story.  What happened was that we each had a small deck of cards numbered thusly:  0, .5, 1, 2, 3, 5, 8, 13, 20, 40, 100, infinity, question mark.   Then we’d select a story and talk a little bit about it.  Everyone then ”voted” on the complexity by selecting a card and displaying it. For example, one story talked about adding help pages to a web site that is part of the product I am working on.  The goal of the voting is to see what people think about the relative complexity of completing the story.  (you need to create a base line which we did by finding a story we previously completed that we thought was a 2 and then finding another task we thought was a 20).   After the first vote you talk about why the outliers thought like they did, then you vote again, and talk about the results.  The goal is to get the people to agree on the relative complexity (ie everyone votes the same or at least a majority does).  The ? means you don’t have enough to go on to vote.   infinity means you think the story is impossible to get done within a sprint (this means trying to break the story into a couple of stories).

The goal is to do this so that a) you as a team can figure out how many “story points” you can complete in a sprint, and b) the product owner can prioritize over the next few sprints.

After story points are assigned you then break down the stories into tasks which you put actual work estimates on.

Over all it was a really cool meeting today.   I feel as a team we are starting to work better. 

 (dang I missed my goal of a post a day… I’m going to keep trying tho.)

So I got my neighbor to help me measure my window and made a trip to Home Depot.  Found the window I needed however had to order the extension jambs, and he also put on my order the screen for the window.  When I got to the register I had em ring it all up and then handed em my coupon for 10% off.  Because part of my purchase was a special order I would have to goto the “service desk” to get the coupon applied.  Well I get to the service desk and it’s like one person behind the desk with 10 people in line.  

 argh!

So I went back to the regular line and forwent some of my discount.   Which is what I am sure they were hoping when they put in place these arbitrary “rules.”

Next Page »