Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 26, 2012 06:31 PM by BrockAllen
Mar 26, 2012 02:30 PM|LINK
Recently we have converted the ASP.NET MVC2(VS 2008) project to ASP.NET MVC4(VS 2010, Razor View).
In ASP.NET MVC2, we have used LINQ to SQL. In ASP.NET MVC4, we are using ENTITY Framework. When comparing the loading speed, ASP.NET MVC4 runs slower than ASP.NET MVC2.
I have compared a basic page that just pulls the list of Contacts and both are having the same functionality and flow(Controller -> BLL -> Repository(DAL)). But ASP.NET MVC4 loads slower(about 5 times slower in some pages) than MVC2.
Above is screenshot of firebug from MVC2 application
Above is screenshot of firebug from MVC4 application
Both are the same functionality, but it differs in the time they load.
How to find the loading issue in my ASP.NET MVC4? Is there any tool that points out any issues/leakage? Please suggest
Mar 26, 2012 02:44 PM|LINK
I'd suggest doing some profiling. Glimpse might be able to give some insight (not sure if it'll work with MVC4 -- I've not tried yet).
Mar 26, 2012 04:02 PM|LINK
Honestly, with a difference of about 400 milliseconds I'd say that it's very likely that the bottleneck is the data access code. According to this chart Linq to Sql has a slight performance benefit over Entity framework. However, you get much more functionality
out of of EF.
I'd run a profiler against the database to see what kind of sql query is being generated. You could be selecting lots of data and that would hurt performance. Consider using Dapper for this page, it's signifcantly faster than EF, but requires you to do a
lot more setup work
Also consider introducing a caching layer so that you are not hitting the database regularly.
Mar 26, 2012 05:58 PM|LINK
I almost hit through the issue which is causing the delayed loading. Its due to the Interfaces in my controller. When i comment the Interfaces in my controller, the page loads faster. I am using the Unity Application Block for Dependency Injection.
Below is the screenshot(Controller with Interfaces) from the Glimpse:
public class UserController : BaseController
private IUserService _userService;
private IPaymentService _paymentService;
private ICommonService _commonService;
private IAdminService _adminService;
private IAUserService _adminUserService;
public UserController(IUserService userService, IPaymentService paymentService, ICommonService commonService, IAdminService adminService, IAUserService adminUserService)
this._userService = userService;
this._paymentService = paymentService;
this._commonService = commonService;
this._adminService = adminService;
this._adminUserService = adminUserService;
Below is the screenshot(Controller without Interfaces) from the Glimpse:
public class UserController : BaseController
From the above screeshots, there's a clear difference in the speed with and without interfaces. Is there anything to be noted here to fix? I am little confused on the differnce in loading time of pages with and without Interfaces.
Both the screenshots i took is without any database calls.
Though I am using the same latest Unity Application Block(V2.1.505.0) in both MVC2 and MVC4, it works fine with MVC2, but not with the MVC3 or MVC4.
Mar 26, 2012 06:01 PM|LINK
Not exactly sure what you mean by "Controller with Interfaces" -- are you talking about using DI? Can you show the before and after code snippets?
Mar 26, 2012 06:13 PM|LINK
I have updated my previous post with the before and after code snippets. It loads slower when i use more Interfaces in my Constructor as in my code snippet(With Interfaces)
Mar 26, 2012 06:17 PM|LINK
And you're using dependency injection to satisify all the interface dependencies?
So for your profiling, did you test the first request or subsequent requests once the DI container has been "warmed up"?
So two variables changed for you: 1) the DB engine, and 2) MVC2 -> MVC4. I'd suggest eliminating one of these variables to really determine which one caused the perf issues.
Mar 26, 2012 06:25 PM|LINK
I am using the Dependency Injection perfectly in global.asax.
The screenshot i took is from the least time the page loaded(After 2nd or 3rd time of reloading the pages)
1). I checked even after commenting the call to database(the screenshot from my previous post). 2). The issue is when using these interfaces with my upgraded MVC4 application
Mar 26, 2012 06:31 PM|LINK
So then something in MVC4 changed about how the dependency resolved is used, perhaps? I'd suggest submitting this as feedback to the MVC4 team.