CodeBehind framework is faster than ASP.NET Core

elanatframework - Oct 19 '23 - - Dev Community

Based on tests conducted by Elanat Framework, the CodeBehind framework is faster than the default cshtml structure in ASP.NET Core.

In this performance test, we examine the performance of the default ASP.NET Core structure compared to CodeBehind. This review was done on .NET Core version 7.0 and CodeBehind version 1.5.2. This review is only focused on view section in MVC; In version 1.5.2 of CodeBehind, we need to specify the Controller class in the view section.

Classes and codes of the examined frameworks

ASP.NET Core

cshtml

@page
@{
    Random rand = new Random();
}

<div>
    <h1>@rand.Next(1000000)</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

The above codes are repeated in 10 pages (page1, page2, page3, ..., page10)

Program.cs class

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

var app = builder.Build();

app.MapRazorPages();

app.Run();
Enter fullscreen mode Exit fullscreen mode

CodeBehind

aspx

<%@ Page Controller="PerformanceTestCodeBehind.DefaultController" %>
<%Random rand = new Random();%>

<div>
    <h1><%=rand.Next(1000000)%></h1>
</div>
Enter fullscreen mode Exit fullscreen mode

The above codes are repeated in 10 pages (page1.aspx, page2.aspx, page3.aspx, ..., page10.aspx)

Controller

using CodeBehind;

namespace PerformanceTestCodeBehind
{
    public partial class DefaultController : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Note: Controller is required in CodeBehind framework version 1.5.2

Program.cs class

using CodeBehind;
using SetCodeBehind;

var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

CodeBehindCompiler.Initialization(true);

app.Run(async context =>
{
    CodeBehindExecute execute = new CodeBehindExecute();
    await context.Response.WriteAsync(execute.Run(context));
});

app.Run();
Enter fullscreen mode Exit fullscreen mode

Test methods

  • Almost 2 minutes before running the tests, we checked the systems installed on the web server at least once to make sure that the systems are not sleeping.
  • In this test, we also checked the number of answers in a fixed time and the elapsed time for the number of fixed answers.

Performance test based on the time elapsed after 10,000 responses

using CodeBehind;

namespace PerformanceTestCsHtmlVSCodeBehind
{
    public partial class DefaultController : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            DateTime startTime = DateTime.Now;
            Random rand = new Random();
            HttpClient webClient = new HttpClient();

            string DataValue = "";

            for (int i = 0; i < 10000; i++)
            {
+                DataValue = webClient.GetStringAsync("http://192.168.1.4/page" + rand.Next(1,10) + ".aspx").Result; // CodeBehind aspx
+                DataValue = webClient.GetStringAsync("http://192.168.56.1/page" + rand.Next(1,10)).Result; // ASP.NET Core Defualt cshtml
            }

            DateTime endTime = DateTime.Now;
            TimeSpan duration = endTime.Subtract(startTime);

            Write("Duration: " + duration.TotalMilliseconds + " ms - LastDataValue: " + DataValue);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Please note that each line of code specified in the class above has been tested separately.

Performance table by miliseconds (Lower is better)

10,000 responses
Performance table by miliseconds - 10,000 responses

CodeBehind is 3.64% better

Average for 20,000 responses
Performance table by miliseconds - 20,000 responses

CodeBehind is 8.1% better

Performance test based on the number of responses after 10 seconds

using CodeBehind;

namespace PerformanceTestCsHtmlVSCodeBehind
{
    public partial class DefaultController : CodeBehindController
    {
        public void PageLoad(HttpContext context)
        {
            DateTime startTime = DateTime.Now;
            Random rand = new Random();
            HttpClient webClient = new HttpClient();

            string DataValue = "";
            int i = 0;

            while ((DateTime.Now - startTime).TotalMilliseconds < 10000)
            {
+                DataValue = webClient.GetStringAsync("http://192.168.1.4/page" + rand.Next(1, 10) + ".aspx").Result; // CodeBehind
+                DataValue = webClient.GetStringAsync("http://192.168.56.1/page" + rand.Next(1,10)).Result; // ASP.NET Core Defualt

                i++;
            }

            Write("RunCount: " + i + " - LastDataValue: " + DataValue);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Please note that each line of code specified in the class above has been tested separately.

Performance table by number of responses (Higher is better)

10 seconds
Performance table by number of responses - 10 seconds

CodeBehind is 6.78% better

Average for 20 seconds
Performance table by number of responses - 20 seconds

CodeBehind is 7.54% better

Conclusion

As it turns out, CodeBehind outperforms the default ASP.NET Core architecture.

Interestingly, the superiority of CodeBehind over the default structure of ASP.NET Core is not a linear graph, and the higher the number of requests over time, the greater the graph of superiority is drawn towards CodeBehind.

Related links

CodeBehind on GitHub:
https://github.com/elanatframework/Code_behind

Get CodeBehind from NuGet:
https://www.nuget.org/packages/CodeBehind/

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .