Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0

 When I started developing with ASP.NET CORE, I realized there weresome some response time performance changes compared to ASP.NET Framework. I thought to myself I need to perform a benchmark about it. A response time benchmark that compares the frameworks ASP.NET 4.8 and ASP.NET CORE 3.0 confirmed what I just suspected.
 
I won’t write some complex benchmark with memory and processor data, but some simple stuff, easy to understand. It’s important to say that if you are a new developer or have never seen ASP.NET CORE before, I suggest you read my article .NET CORE for .NET developers first. I didn’t use the BenchmarDotNet library!
 
I have talked to a friend about this article, and he suggested I  use the BenchmarkDotNet library as the main focus for my benchmark. About the BenchmarkDotNet library, you can install it from NuGet and, when you put some attributes in methods, properties, classes, you will be able to run it.
 
It returns lots of information about CLR/Framework, memory, processor, and other stuff. If you want to know more about it, check their website. The BenchMarkDotNet library seems very interesting but is not the focus of my article.
 
The guidelines for the benchmark are,
  • Use the developer’s machine.
  • Use the default Visual Studio template.
  • The application must have only an API.
  • Focus on response time only.
  • All NuGet packages must be updated.
  • Install Newtonsoft JSON from NuGet.
  • Install Dapper from NuGet.
  • Use IIS Express and debug mode.
  • Use Postman for requests.
  • No source code changes permitted.
  • All SQL Server tables have more than one million records.
For this benchmark, I followed those steps,
  1. Start debugging the application.
  2. Open Postman.
  3. Send the request.
  4. Get the response time (in milliseconds).
  5. Repeat these steps three times.
I also apply this benchmark using ASP.NET CORE 2.2 too.
 

#1 Benchmark – Address Collection

 
Perform a query bringing twenty records from an Address SQL Server table using Dapper.
 
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0
 

#2 Benchmark – One Address

 
Perform a query bringing one record from an Address SQL Server table using Dapper.
 
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0 
 

#3 Benchmark – 97.996 addresses in TXT

 
Perform a query bringing 97.996 records from an Address SQL Server table using Dapper, and transform them in TXT format.
 
I have used some methods like String Builder, Reflection, and LINQ.
 
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0
 

#4 Benchmark – 477.397 addresses in TXT

 
Perform the same test as before, but bringing half 477.397 records.
 
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0 
  1. public async Task<string> GetRangeTxtAsync(  
  2.     string start,  
  3.     string end)  
  4. {  
  5.     var addresses = await _addressRepository.GetAddressRangeAsync(  
  6.         start,  
  7.         end);  
  8.    
  9.     string txt;  
  10.    
  11.     if (addresses == null || addresses.Count() == 0)  
  12.     {  
  13.         txt = "addresses NULL or EMPTY";  
  14.     }  
  15.     else  
  16.     {  
  17.         var sb = new StringBuilder();  
  18.    
  19.         sb.Append(GetColumns<Address>());  
  20.         sb.Append("\r\n");  
  21.    
  22.         foreach (var address in addresses)  
  23.         {  
  24.             sb.Append(GetColumns(address));  
  25.             sb.Append("\r\n");  
  26.         }  
  27.    
  28.         txt = sb.ToString();  
  29.     }  
  30.    
  31.     return txt;  
  32. }  
  33.    
  34. private string GetColumns<T>(  
  35.     T data = default)  
  36. {  
  37.     var sb = new StringBuilder();  
  38.     var type = typeof(T);  
  39.     var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);  
  40.    
  41.     foreach (PropertyInfo property in properties)  
  42.     {  
  43.         if (sb.Length > 0)  
  44.         {  
  45.             sb.Append(";");  
  46.         }  
  47.    
  48.         if (data == null)  
  49.         {  
  50.             sb.AppendFormat(  
  51.                 "{0}_{1}",  
  52.                 type.Name,  
  53.                 property.Name);  
  54.         }  
  55.         else  
  56.         {  
  57.             var val = property.GetValue(data);  
  58.    
  59.             sb.Append(val == null ? "" : $" {val.ToString()}");  
  60.         }  
  61.     }  
  62.    
  63.     sb.Append(";");  
  64.    
  65.     return sb.ToString();  
  66. }  

#5 Benchmark – 97.996 addresses in JSON

 
Perform a query bringing 97.996 records from an Address SQL Server table using Dapper and serializes it in JSON format.
 
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0 
 

#6 Benchmark – 477.397 addresses in JSON

 
Perform the same test as before, but bring half  of 477.397 records. 
 
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0
 

#7 Benchmark – Read a 7MB PDF file

 
I put a 7MB PDF file in the App_Data folder and returned as a download response.
 
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0
  1. // ASP.NET 4.8  
  2. public HttpResponseMessage GetFileSystemDownload()  
  3. {  
  4.     var path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/Cartilha_do_Idoso.pdf");  
  5.    
  6.     var response = new HttpResponseMessage(HttpStatusCode.OK)  
  7.     {  
  8.         Content = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.Read))  
  9.     };  
  10.    
  11.     response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")  
  12.     {  
  13.         FileName = Path.GetFileName(path)  
  14.     };  
  15.    
  16.     response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");  
  17.    
  18.     return response;  
  19. }  
  20.    
  21. // ASP.NET CORE  
  22. public IActionResult GetFileSystemDownload()  
  23. {  
  24.     var path = $@"{_env.ContentRootPath}\App_Data\Cartilha_do_Idoso.pdf";  
  25.    
  26.     return new FileStreamResult(new FileStream(path, FileMode.Open, FileAccess.Read), "application/pdf");  
  27. }  

#8 Benchmark – Read all files located at c:\windows

 
Perform a query reading all files located at c:\windows folder.
Benchmark - ASP.NET 4.8 Vs ASP.NET Core 3.0
 

Remarks

 
I have repeated all tests thee times on different days and different hours and, all results were the same.
 
I also performed all tests again in two other developers’ machines and, all results were the same.
 
An interesting thing about the results of my benchmark is, in some cases, the ASP.NET 4.8 is better than ASP.NET CORE (3.0/2.2) and vice-versa.
 
The question is, which framework do you think is better using the guidelines for this article?
 
Thank you for reading 🙂