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

When I started developing with ASP.NET CORE, I realized there were 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, or 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 are permitted.
  • All SQL Server tables have more than one million records.

For this benchmark, I followed the 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.

1. Benchmark Address Collection

Perform a query bringing twenty records from an Address SQL Server table using Dapper.

Address Collection

2. Benchmark One Address

Perform a query bringing one record from an Address SQL Server table using Dapper.

One Address

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 97.996

4. Benchmark 477.397 addresses in TXT

Perform the same test as before, but bringing half 477.397 records.

Benchmark 477.397

public async Task<string> GetRangeTxtAsync(
    string start,
    string end)
{
    var addresses = await _addressRepository.GetAddressRangeAsync(
        start,
        end);

    string txt;

    if (addresses == null || addresses.Count() == 0)
    {
        txt = "addresses NULL or EMPTY";
    }
    else
    {
        var sb = new StringBuilder();

        sb.Append(GetColumns<Address>());
        sb.Append("\r\n");

        foreach (var address in addresses)
        {
            sb.Append(GetColumns(address));
            sb.Append("\r\n");
        }

        txt = sb.ToString();
    }

    return txt;
}

private string GetColumns<T>(
    T data = default)
{
    var sb = new StringBuilder();
    var type = typeof(T);
    var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

    foreach (PropertyInfo property in properties)
    {
        if (sb.Length > 0)
        {
            sb.Append(";");
        }

        if (data == null)
        {
            sb.AppendFormat(
                "{0}_{1}",
                type.Name,
                property.Name);
        }
        else
        {
            var val = property.GetValue(data);

            sb.Append(val == null ? "" : $" {val.ToString()}");
        }
    }

    sb.Append(";");

    return sb.ToString();
}

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 97.996

6. Benchmark 477.397 addresses in JSON

Perform the same test as before but bring half of 477.397 records.

Benchmark 477.397

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 Read a 7MB PDF file

// ASP.NET 4.8
public HttpResponseMessage GetFileSystemDownload()
{
    var path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/Cartilha_do_Idoso.pdf");
    
    var response = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.Read))
    };
    
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = Path.GetFileName(path)
    };
    
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    
    return response;
}

// ASP.NET CORE
public IActionResult GetFileSystemDownload()
{
    var path = $@"{_env.ContentRootPath}\App_Data\Cartilha_do_Idoso.pdf";
    
    return new FileStreamResult(new FileStream(path, FileMode.Open, FileAccess.Read), "application/pdf");
}

8. Benchmark Read all files located at c:\windows

Perform a query reading all files located at c:\windows folder.

files located at windows folder

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!


Similar Articles