1

I have an asp.net mvc application. On application start up i loop through a large collection of data and for each item in the collection i make a network call to download some data relevant to that item.

Initially i was doing this in a foreach loop but it took up to 26 secs on the average on my dev machine for my application to start up. So i decided to put it in a parallel.ForEach loop. When i measured the time taken to perform the parallel.ForEach loop against the regular foreach loop, i noticed that on the average, the parallel.ForEach loop performed better but my application's performance degraded significantly while performing this operation taking up to 5 mins to start up.

When i checked the iis process within Performance Wizard, i noticed that with the regular foreach loop, iis never exceeded 47 threads in total while performing this operation but with the Parallel.Foreach loop, no of thread spawn gets up to 500 on my machine.

I know that according to the microsoft documentation for the Parallel.Foreach, calls to shared resources will significantly degrade the performance of a parallel loop.

So my question is:

  1. Why does it degrade so much, is it due to the context switching between threads
  2. Why are so many threads being created while executing the loop's body.
  3. How come the parallel loop perform faster than a regular for loop on the average but the overall application's performance on start degrading.
  4. How can i achieve a faster application start up with or without using a parallel foreach loop when making a network service call that may be slow

find below the code of what i am doing

//parallel loop version
Parallel.ForEach(categories.SelectMany(c => c.Billers), service =>
{
    try
    {
        if (service.PaymentItems == null || service.PaymentItems.Any())
        {                  
            service.PaymentItems = GetServiceOptions(service.Id);
        }
        catch (Exception ex)
        {
            _logger.Error(string.Format("Problem getting service options for {0}", service.Name), ex);
        }
});

//regular for loop version
foreach (var service in categories.SelectMany(c => c.Billers))
{
    try
    {
        if (service.PaymentItems == null || service.PaymentItems.Any())
            {
                service.PaymentItems = GetServiceOptions(service.Id);
            }
        }
    catch (Exception ex)
    {
        _logger.Error(string.Format("Problem getting service options for {0}", service.Name), ex);
    }
}

The method GetServiceOptions handles the network call

4
  • Please post some code for us to look at. Commented Jun 2, 2015 at 16:35
  • One point that you might have already done: make sure the network call is done asynchronously so it doesn't block the thread. Commented Jun 2, 2015 at 16:44
  • @BenjaminPaul pls find code above
    – Anwuna
    Commented Jun 2, 2015 at 17:30
  • I think one reason is because you're not specifying ParallelOptions.MaxDegreeOfParallelism, so your machine is going to use up all threads available by default. Limiting it to something should increase your speed while keeping your application performant. At least I would hope.
    – ragerory
    Commented Jun 2, 2015 at 19:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.