WF4 Performance

I have been playing around with the idea of modeling business rules as a set of small workflows (Flowcharts in WF4).  In the application, this would result in small workflows being called a large number of times.  I decided to investigate the performance overhead of invoking a workflow in WF4, the workflow technology in .NET 4.  Since I am using Visual Studio 2010 Ultimate Beta 2, this also seemed like a good opportunity to evaluate the performance profiler included in this edition of VS 2010.

For this test, I implemented a simple application that performs a calculation 10,000 times, then outputs that value to the console.  As a reference, I first implemented this in simple C# code as follows:

Code1

To determine the performance, I used the Instrumentation Profiler.  The simple C# code took approximately 800 milliseconds to execute.  Most of the time was spent writing to the console.

Profile1

 

Next, I implemented the calculation functionality in a simple flowchart workflow.  My flowchart has a single InArgument y, and an OutArgument x.

Workflow

In the Main method for my application, I modified the loop to call the simple workflow to perform the calculation.

Code2

The performance from this implementation seems a little disappointing at first.  We went from a total execution time of less than 1 second to approximately over 36 seconds.

Profile2

But if we really think about what’s happening here, this performance isn’t that bad.  We know that it takes about 1 second to perform the calculations and output the results.  That means that the remaining 35 seconds was spent on “workflow” related processing.  In that 35 seconds, we were able to create and execute 10,000 workflows.  Clearly this is not fast enough to accomplish what I was originally trying to do.

After digging through the profiler, I noticed that the application seemed to be spending a large percentage of the time initializing the workflow.  This led me to investigate if we could just create the workflow object one time and reuse it whenever we invoke that workflow.  It turns out that we can, and the results are dramatic.

 

The code is as follows:

Code4

Notice that I am now passing the input to the workflow using a Dictionary<string,object>.  I’m not sure why, but when I tried setting the input using workflow.y = i, the workflow would always use the first value that was assigned to y.  In this case, my output was always 0 because the workflow was always using y=0.  Passing the workflow inputs as a Dictionary seems to work around this problem.

Using this method of invoking the workflow, the total execution time was just over 1.15 seconds.

Profile4

Now this looks much better.  By caching the instance of the workflow object, we are able to significantly reduce the overhead of executing a workflow.  In this case, we spent just over 1/2 the time calling Console.WriteLine().  That means we were able to execute 10,000 workflows in the remaining .65 seconds.  Considering the flexibility this can provide in an application, I would consider this overhead to be minimal.  In most real-world applications, the difference in performance will not be noticeable to the end user.

When running the application in release mode with no profiler attached, it took 14.48 seconds to execute the workflow 1,000,000 times.  That works out to 69,039 workflows per second!

I found the performance profiler in VS 2010 to be useful.  While it is much better than no profiler at all, I was a little disappointed.  There are much better profilers on the market today.