title: How to speed up task<t> with httpclient
body: <p>I have a processes where I need to make ~100 http api calls to a server and process the results. I've put together this commandexecutor which builds a list of commands and then runs them async. To make about 100 calls and parse the result is taking over 1 minute. 1 request using a browser give me a response in ~100ms. You would think that ~100 calls would be around 10 seconds. I believe that I am doing something wrong and that this should go much faster.</p>
  
  <pre><code> public static class CommandExecutor
   {
      private static readonly ThreadLocal&lt;List&lt;Command&gt;&gt; CommandsToExecute =
          new ThreadLocal&lt;List&lt;Command&gt;&gt;(() =&gt; new List&lt;Command&gt;());
      private static readonly ThreadLocal&lt;List&lt;Task&lt;List&lt;Candidate&gt;&gt;&gt;&gt; Tasks =
          new ThreadLocal&lt;List&lt;Task&lt;List&lt;Candidate&gt;&gt;&gt;&gt;(() =&gt; new List&lt;Task&lt;List&lt;Candidate&gt;&gt;&gt;());
  
      public static void ExecuteLater(Command command)
      {
          CommandsToExecute.Value.Add(command);
      }
  
      public static void StartExecuting()
      {
          foreach (var command in CommandsToExecute.Value)
          {
              Tasks.Value.Add(Task.Factory.StartNew&lt;List&lt;Candidate&gt;&gt;(command.GetResult));
          }
  
          Task.WaitAll(Tasks.Value.ToArray());
      }
  
      public static List&lt;Candidate&gt; Result()
      {
          return Tasks.Value.Where(x =&gt; x.Result != null)
                            .SelectMany(x =&gt; x.Result)
                            .ToList();
      }
  }
  </code></pre>
  
  <p>The Command that I am passing into this list creates a new httpclient, calls the getasync on that client with a url, converts the string response to an object then hydrates a field.</p>
  
  <pre><code>    protected void Initialize()
      {
          _httpClient = new HttpClient();
          _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
      }
  
      protected override void Execute()
      {
          Initialize();
  
          var task = _httpClient.GetAsync(string.Format(Url, Input));
          Result = ConvertResponseToObjectAsync(task).Result;
          Result.ForEach(x =&gt; x.prop = value);
      }
  
      private static Task&lt;Model&gt; ConvertResponseToObjectAsync(Task&lt;HttpResponseMessage&gt; task)
      {
          return task.Result.Content.ReadAsAsync&lt;Model&gt;(
             new MediaTypeFormatter[]
             {
                   new Formatter()
             });
      }
  </code></pre>
  
  <p>Can you pick up on my bottleneck or have any suggestions on how to speed this up. </p>
  
  <p><strong>EDIT</strong>
  making these changes made it down to 4 seconds.</p>
  
  <pre><code>protected override void Execute()
      {
          Initialize();
  
          _httpClient.GetAsync(string.Format(Url, Input))
          .ContinueWith(httpResponse =&gt; ConvertResponseToObjectAsync(httpResponse)
          .ContinueWith(ProcessResult));
      }
  
      protected void ProcessResult(Task&lt;Model&gt; model)
      {
          Result = model.Result;
          Result.ForEach(x =&gt; x.prop = value);
      }
  </code></pre>