Reduce wait time with tasks

In our project we group multiple machines and request information from each one. Sometimes this takes very long.

All machines are contacted in sequence. When one machine is offline the request will timeout in 10 seconds (default WCF behavior) This way the user must wait very long when some machines are offline. The graph below shows the messages captured with fiddler when one machine is offline. Look for the exclamation mark.

Sequential calls captured with fiddler

We could call the services asynchronous but that would mean handling events and messing up the code.

Using tasks we now call the services in parallel. In the code sample below are the old and new sources.

// old code
foreach(var machine in machines) CallTheService(machine);

// new code
var tasks = machines.Select(machine => 
   Task.Factory.StartNew(() => CallTheService(machine))
).ToArray();
Task.WaitAll(tasks);

This way we don’t reduce the wait for an offline machine, but make better use of the idle time by doing other requests. The graph below shows the messages captured with fiddler of the same group of machine as the graph above. The total process took 16 seconds in stead of 35.

fiddler.parallel.with.tasks

When a user has to wait it is always to long. Make sure you do the best you can to reduce the wait.

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.