The intention of this article is to compare performance between ASP.NET WebForms and ASP.NET MVC. The main reasons for moving from Webforms to MVC are performance, performance and performance.
FYI: In case you are new to MVC tutorials, you can start from the video given at the end of the article.
Code for the Test
For this test, we created two projects, one in ASP.NET MVC and the other in ASP.NET Webforms. We have kept the test absolutely simple. Both the projects display 20 textboxes and there is a grid loaded with 1000 records.
Below is the ASP.NET Webform code behind which binds with a grid server control.
In ASP.NET MVC project also, we have similar kind of logic. We have 20 textboxes created using HTML control and a simple loop which creates an HTML table.
@{
List obj = new List();
for (int i = 0; i < 1000; i++)
{
obj.Add(DateTime.Now.ToString());
}
}
@{
foreach (string str in obj)
{
}
}
Item@str
Out of Scope
In this test, we have not used JSON, Jquery, Ajax because we wanted to test performance of these platforms only and not when they get clubbed with other technical things like HTML 5, Ajax, etc.
How Was the Test Conducted?
The test was conducted with the below specifications using VSTS and telerik load test software:
User load 25 users.
Run duration of test was 10 minutes.
Machine config DELL 8 GB Ram, Core i3
Project was hosted in IIS 8.
Project was created using MVC 5.
Network LAN connection was assumed. So this test does not account for network lag for now.
Browser in the test selected Chrome and Internet Explorer.
Multiple readings were taken during the test to average unknown events. 7 readings where taken and all readings are published in this article as reading 1 , 2 and so on.
Performance Criteria
Performance was measured using two criteria, Average page response time and Response content in bytes.
Average Page response time: This value is the average time the 25 users get the page output when the load testing is executed.
Response content length: Number of average total bytes transferred for every request. This criteria is taken because we suspect the server controls generate more amount of HTML as compared to when we write custom HTML.
Conclusion of Test
Response Time
Response time of MVC was much better as compared to Webform. We found ASP.NET MVC response time was two times better than Webforms.
The reason is but obvious. When a request is sent to Webforms, there is a complex ASP.NET page life cycle which gets executed in the code behind. This code behind is nothing but sort of conversion logic which converts the ASP.NET server controls to HTML.
In ASP.NET MVC, there is no code behind and no such conversion is required as the controls are in HTML format themselves.
Content Length
For the same kind of logic, the HTML content length generated from Webform was twice compared to MVC.
When we viewed the view source, we saw huge view state data generated by Webform which was not present in MVC. This means more bandwidth will be consumed when we surf ASP.NET Webform sites as compared to ASP.NET MVC sites.
ASP.NET MVC as Solution
If you see the conclusion from the load test, we need a solution which does not have code behind and server controls. So when you create ASP.NET MVC projects, you will not find NOcode behind and server controls.
Below is a snapshot of MVC views and you can see there is .CSHTML but there is no CSHTML.CS.
If you go a MVC view and click on toolbox, it has HTML tab only and all the server controls have gone off completely.
Below are all the seven readings pasted for both Webform and MVC. These readings would provide you more insight on the performance factor between these technologies.
An existing connection was forcibly closed by the remote host
So if you are convinced that ASP.NET MVC is the way to go ahead, how about starting to learn MVC right away now. Below is an awesome ASP.NET MVC video which teaches MVC in 16 hours flat.
(MUF) Must Used Feature Approach of Testing
Lot of developers would raise eyebrows on the above test stating that:
We have the option of disabling viewstate
We have the option of using HTML over server controls
and so on...
Yes, we agree completely that webforms can be tweaked to run in an optimal way and they can have nearly the same performance as ASP.NET MVC.
But this test followed the most used feature approach. In general, when Webform is used 99% times, developers do not disable view state, they use code behind, they use server controls left and right and so on.
Because if we go that route of testing, then ASP.NET MVC also has lot of best practices like jQuery, JSON, etc. to improve performance.
So this test was conducted keeping what is the most used feature in both Webform and MVC. Both these technologies where kept at ground zero and compared using their most used feature in the market.
Option vs Compulsion
If you are taken to a bar and you are given a choice of drinking wine or juice. There is high possibility you will drink wine… temptation, human nature whatever you term it as.
But if you are taken to a juice shop, you do not have options and you are compelled to drink only juice.
ASP.NET Webform gives you an option while MVC forces you to follow best practices. So with Webform, I have options of using HTML, disable view state but the human temptation of RAD is so profound that most of the time, people end up using these features.
On the other hand, MVC does not give you the option of code behind, server controls, view state, etc. thus compelling you to use pure HTML and without behind code architecture.
Further Reading
We encourage you to read the below articles as well to enhance your ASP.NET MVC knowledge further.