fuaaark
fuaaark

Reputation: 551

Performance measurement of individual threads in WaitAll construction

Say I'm writing a piece of software that simulates a user performaning certain actions on a system. I'm measuring the amount of time it takes for such an action to complete using a stopwatch.

Most of the times this is pretty straighforward: the click of a button is simulated, some service call is associated with this button. The time it takes for this service call to complete is measured.

Now comes the crux, some actions have more than one service call associated with them. Since they're all still part of the same logical action, I'm 'grouping' these using the signalling mechanism offered by C#, like so (pseudo):

var syncResultList = new List<WaitHandle>();

var syncResultOne = service.BeginGetStuff();
var syncResultTwo = service.BeginDoOtherStuff();

syncResultList.Add(syncResultOne.AsyncWaitHandle); 
syncResultList.Add(syncResultTwo.AsyncWaitHandle);

WaitHandle.WaitAll(syncResultList.ToArray());

var retValOne = service.EndGetStuff(syncResultOne);
var retValTwo = service.EndDoOtherStuff(syncResultTwo);

So, GetStuff and DoOtherStuff constitute one logical piece of work for that particular action. And, ofcourse, I can easily measure the amount of time it takes for this conjuction of methods to complete, by just placing a stopwatch around them. But, I need a more fine-grained approach for my statistics. I'm really interested in the amount of time it takes for each of the methods to complete, without losing the 'grouped' semantics provided by WaitHandle.WaitAll.

What I've done to overcome this, was writing a wrapper class (or rather a code generation file), which implements some timing mechanism using a callback, since I'm not that interested in the actual result (save exceptions, which are part of the statistic), I'd just let that return some statistic. But this turned out to be a performance drain somehow.

So, basically, I'm looking for an alternative to this approach. Maybe it's much simpler than I'm thinking right now, but I can't seem to figure it out by myself at the moment.

Upvotes: 3

Views: 165

Answers (2)

Kenneth Ito
Kenneth Ito

Reputation: 5261

If your needs are simple enough, a simple way would be to just record each service call individually, then calculate the logical action based off the individual service calls.

IE if logical action A is made of parallel service calls B and C where B took 2 seconds and C took 1 second, then A takes 2 seconds.

A = Max(B, C)

Upvotes: 0

Nick Butler
Nick Butler

Reputation: 24433

This looks like a prime candidate for Tasks ( assuming you're using C# 4 )

You can create Tasks from your APM methods using MSDN: Task.Factory.FromAsync

You can then use all the rich TPL goodness like individual continuations.

Upvotes: 1

Related Questions