1

I have two views: Product/List, which displays a list of products, and Product/Details/{id}, which displays the details of the product of the given Id.

Every time when a user clicks on a product item on the Product/List view to go to the Product/Details/{id} view, it takes about 4 seconds. I have been researching what takes so long and would like to make it a little faster.

So, I updated my action method Details as follows, assuming the database call in the constructor ProductDetailsViewModel(int) is causing the delay.

[Route("Product/Details/{id}")]
    public ActionResult Details(int? id) {
        Stopwatch watch = new Stopwatch();
        watch.Start();
        var viewModel = new ProductDetailsViewModel(id);
        watch.Stop();
        int duration = watch.Elapsed.Milliseconds;            
        return View(viewModel);
    }

But, it is not. duration is only about 500 to 600 milliseconds.

I googled and did something like that provided by Nick at http://nickberardi.com/timing-the-execution-time-of-your-mvc-actions/

And I consistently get about 4 seconds for the load time of Product/Details/{id}. See the screenshot (chrome dev tools) below.

I am little puzzled. What else could be causing the execution of the action method to take this long? Any idea? Thanks.

enter image description here

UPDATED WITH Glimpse.Mvc5 timeline: enter image description here

Stack0verflow
  • 1,148
  • 4
  • 18
  • 42
  • 3
    What's in your view? – Michael Dunlap Jun 12 '15 at 18:18
  • You may want to check your code with a tool like MiniProfiler (http://miniprofiler.com/), but I suspect the issue is with the view find process. Your view names are probably too ambiguous, causing multiple views to have to be searched to find the one that matches your model. – Claies Jun 12 '15 at 18:19
  • 1
    Is that a lazy queriable or a fully resolved query for product detail? What does that code look like? Does the product detail have many navigation properties? – Jasen Jun 12 '15 at 18:20
  • Are you doing this in debug mode or after a recompile? It will be significantly slower – Brad C Jun 12 '15 at 18:27
  • Thanks. @MichaelDunlap: The product details has information such as product name, title, manufacture, supplier, contact person. It calls a stored procedure which returns multiple tables. It does not seem to have anything to do with the database call since the duration variable gets a value of only 500 to 600 milliseconds. I suspect it is something else. – Stack0verflow Jun 12 '15 at 18:30
  • @Jasen: It is a stored procedure that returns multiple tables. – Stack0verflow Jun 12 '15 at 18:31
  • @br4d: In either debug mode or release mode on production. The delay is about the same. – Stack0verflow Jun 12 '15 at 18:32
  • @Claies: I've installed glimpse (http://getglimpse.com/), and it reports the same kind of delay for the action method Details like my filter X-Stopwatch shown in the response header. – Stack0verflow Jun 12 '15 at 18:33
  • 1
    your `duration` variable *might not* be showing the actual results of a table fetch. Entity Framework delays execution of SQL calls until the data is actually needed; your `duration` might only be tracking the amount of time it takes to build the query, not the time it takes to actually execute it. – Claies Jun 12 '15 at 18:33
  • @Claies. No entity framework is involved. It's a stored procedure. But if the duration I obtained like that isn't reliable, I will look into my stored procedure. – Stack0verflow Jun 12 '15 at 18:35
  • so you have a SQL call embedded in the constructor of the `ProductDetailsViewModel`? if that's the case, then the issue is definitely with the view. Either it's taking a long time to locate the view, or the view itself has a lot of complex logic in the razor. – Claies Jun 12 '15 at 18:37
  • @Claies: Yes, the constructor makes a call to a stored procedure via the DAL method GetProductDetails(int id). – Stack0verflow Jun 12 '15 at 18:40
  • are you using the `Glimpse.Mvc5` plugin to monitor the internals of MVC? – Claies Jun 12 '15 at 18:40
  • @Claies: Yes, Glimplse.Mvc5, which reports that the Details method call takes about 4 seconds. The view does not have complicated logic. I do use jquery plugin DataTable for the view (no ajax call). I simply razor out the data via model.Item.Blah. – Stack0verflow Jun 12 '15 at 18:42
  • 1
    We really cant answer this question until you post all the relevant code. This would include the ViewModel, DB calls / stored procedures, action filters, the view itself, etc. Most likely the expensive thing is the stored proc and you would be better off looking at the query analyzer. – Brad C Jun 12 '15 at 18:48
  • @br4d: Yes, I am looking into the stored procedure. Will try to post relevant code during the weekend. – Stack0verflow Jun 12 '15 at 18:50
  • If you have glimpse set up properly, the timeline should show you exactly where in the pipeline your application is spending it's time. Do you have any examples of the graph that glimpse generates for this action? – Claies Jun 12 '15 at 18:50
  • specifically, the Timing page. – Claies Jun 12 '15 at 18:52
  • Let me get that screen shot for you. – Stack0verflow Jun 12 '15 at 18:59
  • The glimpse timeline screen shot is attached. Also, I modified the view model constructor by removing database call and simply hard coded some minimum values needed for a minimized razor view. The time spent executing the controller action Details is still 3 to 4 seconds. Tried it in Release mode, only slightly better, bearly noticeable. – Stack0verflow Jun 12 '15 at 19:45
  • 1
    Well the way that glimpse graph reads, the majority of the code time seems to be in instantiating your controller, before the action method you are timing is even reached. – Claies Jun 12 '15 at 22:33
  • @Claies: I did not quite understand the graph, but now that you mention, I seem to understand it. But is there a workaround? My goal is to reduce the delay. Thanks. – Stack0verflow Jun 13 '15 at 02:27
  • 1
    now you're definitely at a stage where we don't have enough data to really know why the controller is taking so long. What does the controller constructor look like? does it make a large number of calls to other services? is it using some sort of IOC container that may be having issues with variable resolution? you'll just have to expand your net of timers and traces. – Claies Jun 13 '15 at 05:01
  • Usually MVC programmers don't create a controller constructor, In other words, I don't have a controller constructor, the base controller may have it. I never bothered to peek into the source code of the base class. Strangely, even if my view model class constructor simply returns two hard coded values for product id and product name, and only 1 action method in the controller class and my view only has two fields product id and product name, it still takes 3.5 to 4 seconds to transition from the Product/List page to the Product/Details page. Really strange. – Stack0verflow Jun 15 '15 at 13:34
  • @Stack0verflow I know this is a few years old but I wanted to correct a statement. I always create a controller constructor if I'm going to inject a dependency. The statement that "Usually MVC programmers don't create a controller constructor" is false. – TheAlgorist Nov 14 '19 at 18:42

0 Answers0