title: Better performance of a service call than a library was not expected
body: <p>I was trying to figure out some performance values of two scenarios. I thought I was only going to declare the obvious at first. But when I got the results I got a little confused. And now I am looking for a justification for the case.</p>
  <p>I have a library which makes couple of queries through a MongoDb database and Active Directory services, then returns the results to client, which are:</p>
  <ul>
  <li>GetUserType - to MongoDb - there is a collection which has username and type fields in its all documents. In the query I give the username and ask for the type field.</li>
  <li>LoginCheck - to Active Directory - given the username and the password from the client, I create a <code>PrincipalContext</code> object to access to AD server and call <code>ValidateCredentials</code> upon it.</li>
  </ul>
  <p>This job is performing on an existing MVC application at the moment. And we are going to create a new desktop application and employ it with the same job.</p>
  <p>We were curios about how different can these two scenarios perform? We thought that a direct call to a library without any http connection would perform better than a service request without an hesitation. But we still wondered how much difference is there, and if it was acceptable we are going to make it work through the rest MVC service - because of reasons :)</p>
  <p>Hence we tested out the following architectures:</p>
  <p><strong>Scenario 1:</strong></p>
  <p><a href="https://i.stack.imgur.com/LQ9UU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LQ9UU.jpg" alt="Case 1" /></a></p>
  <p><strong>Scenario 2:</strong></p>
  <p><a href="https://i.stack.imgur.com/aGPVF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aGPVF.jpg" alt="Case 2" /></a></p>
  <p>Basically, what I do for performance test is this:</p>
  <p><strong>For scenario 1:</strong></p>
  <pre><code>for(var i = 0; i&lt;10000; i++)
  {
      new Class1().HeavyMethod();
  }
  </code></pre>
  <p><strong>For scenario 2:</strong></p>
  <pre><code>// client side
  for(var i = 0; i&lt;10000; i++)
  {
      using ( var client = new HttpClient() )
      {
          var values = new Dictionary&lt;string, string&gt;();
          var content = new FormUrlEncodedContent(values);
          var response = client.PostAsync(&quot;http://localhost:654/Home/HeavyLift&quot;, content).Result;
          var responseString = response.Content.ReadAsStringAsync().Result;
      }
  }
  
  // MVC rest service
  public class HomeController : Controller
  {
      public JsonResult HeavyLift()
      {
          return Json(new Class1().HeavyMethod(), JsonRequestBehavior.AllowGet);
      }
  }
  </code></pre>
  <p><strong>Common Class:</strong></p>
  <pre><code>public class Class1
  {
      public string HeavyMethod ()
      {
          var userName = &quot;asdfasdfasd&quot;;
          var password = &quot;asdfasdfasdf&quot;;
          try
          {
              // this call is to MongoDB
              var userType = Personnel.GetPersonnelsType(userName).Result;  
              // this call is to Active Directory  
              var user = new ADUser(new Session 
              { 
                  UserType = userType.Type, 
                  UserName = userName, 
                  Password = password 
              });
  
              return userType.Type + &quot;-&quot; + user.Auth();
          }
          catch ( Exception e )
          {
              return e.Message;
          }
      }
  }
  </code></pre>
  <p>The results for 10000 consecutive calls are confusingly shocking:</p>
  <blockquote>
  <p><strong>Scenario 1:</strong> 159181 ms</p>
  <p><strong>Scenario 2:</strong> 13952 ms</p>
  </blockquote>
  <p><code>Scenario 1</code> starts off pretty quicly for the first few dosens of calls, then it starts to slow down.
  <code>Scenario 2</code> though offers a constant response time through 10k calls.</p>
  <p>What is actually happening here?</p>
  <p><strong>Note:</strong> I checked the memory and cpu usages of the server that this scenarios runs on(everything runs on the same server) but there is nothing interesting actually, they are behaving just the same in terms of memory and cpu resources.</p>