skip to Main Content

My local laptop has 4 cores and 32GB RAM.

My EC2 in c5.8xlarge – 32 cores and 64GB GB.

I have a job that runs some parallel processes. Here is a piece of representative code.

 Parallel.ForEach(jobList, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, job=>{
    //each job includes parameters for a year to do some work for each month
    
     var subTasks = new List<Task>();
     for (var m = 1; m < 13; m++){
          var subTasks = Task.Factory.StartNew(() =>{
               //process task
               //each task include HTTP calls, SQL process, S3 csv loads and save
          });
          subTasks.Add(subTasks);
     }
     Task.WaitAll(subTasks.ToArray());
 });

When I run the code on my local computer, it takes 30-40 seconds. However, I run on an EC2 server, and it takes over 150 seconds. I deployed my project as a docker container on EC2. In my local, I run the code on IIS Express.

Also, I tested the process inside single task performance on both sides. On EC2, a single task takes less time than my local. If a task takes 10 seconds on my local, it takes 6 seconds on EC2.

Does anyone know the possible reason why this difference occurs? Is it related to the warming-up application?

NOTE:
The question is how a weak local computer can be 4 times faster than a strong server. Namely, my code is run fast on my local computer. But in the server, running is 4 times slower. Why?

There are two structural differences between my local and the server. Docker + nginx and IIS Express. The server capacity, bandwidth, and network distance are better than the local.

2

Answers


  1. Chosen as BEST ANSWER

    I replaced Parallel.Foreach with SemaphoreSlim. And Bingo!!! It works as I expect. Only 14 seconds. This is approximately one month of process running time.

    I think this is related to @JonasH and @Franck comments. Some codes work differently in different environments. I am not sure which difference results in this condition and how. Maybe IIS, maybe Windows, or Docker. I think this is a case for experts.

     var monthlyTasks = new List<Task>();
     using (SemaphoreSlim concurrencySemaphore = new SemaphoreSlim(150))
     {    
        foreach (var process in processList) //processList include all monthly jobs
        {
              concurrencySemaphore.Wait();
              var t = Task.Factory.StartNew(() =>
              {
                   try
                   {
                      RunMontlyProccess(process);
                   }
                   catch (Exception ex)
                   {
                      _log.Error("ParallelProcess: " + process.Id + ", message:" + ex.Message, ex);
                   }
                   finally
                   {
                       concurrencySemaphore.Release();
                   }
              });
              monthlyTasks.Add(t);
        }
        Task.WaitAll(monthlyTasks.ToArray());
    }
    

  2. Your code is related to HTTP, meaning it’s related to network connection speed (bandwidth). Perhaps the data transfer between your EC2 server and the HTTP server is slower compared to the connection from your personal computer to the HTTP server. This is just a possibility!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search