20

I really don't know where to begin with this question, but the site I'm working on at times has some really slow page loads. Especially after doing a build, but not always. I usually have to refresh the page 5-10 times before it actually comes up. I guess I am trying to see where exactly I should begin to look.

ASP.NET MVC 3 Ninject AutoMapper Entity Framework Code First 4.1 SQL Server 2008 Razor

UPDATES

Concerning some of the questions, it can do this long loading on every page, but after it loads its fairly quick on all the pages.

After posting this and getting your replies I started the application and it is still loading and probably won't ever load unless I click reload on the browser.

No caching, and the EF models aren't huge.

I am using Razor and Visual Studio 2010 with 6 GB of memory and an I7 processor.

I am using IIS Express and the default web server when debugging. It also does this on IIS7 on the main server.

I may look into the MVC Profiler and Glimpse to see what I can find.

Below I have some code this runs when it hits the homepage. I would say it never loads when I first start up the server. I put a break point at var model which never gets hit. If I reload the page then it does.

public ActionResult Index()
        {
            var model = new HomeViewModel();

            model.RecentHeadlines = _headlineService.GetHeadlines(1, Config.RecentHeadlinesPageSize, string.Empty);

            return View(model);
        }

Below is my datacontext setup also.

public class DatabaseFactory : Disposable, IDatabaseFactory
    {
        private DataContext _dataContext;
        public DataContext Get()
        {
            return _dataContext ?? (_dataContext = new DataContext());
        }
        protected override void DisposeCore()
        {
            if (_dataContext != null)
                _dataContext.Dispose();
        }
    }

public class Disposable : IDisposable
    {
        private bool isDisposed;

        ~Disposable()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(bool disposing)
        {
            if (!isDisposed && disposing)
            {
                DisposeCore();
            }

            isDisposed = true;
        }

        protected virtual void DisposeCore()
        {
        }
    }

public class UnitOfWork : IUnitOfWork
    {
        private readonly IDatabaseFactory _databaseFactory;
        private DataContext _dataContext;

        public UnitOfWork(IDatabaseFactory databaseFactory)
        {
            _databaseFactory = databaseFactory;
        }

        protected DataContext DataContext
        {
            get { return _dataContext ?? (_dataContext = _databaseFactory.Get()); }
        }

        public void Commit()
        {
            DataContext.Commit();
        }
    }
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • 1
    Need some more details. Is this for hitting the first page? Every page? Only certain pages? Happens consistently or sporadically? – Lester Dec 15 '11 at 20:24
  • How big is your EF model? Is there any caching in your application? Is it especially after build or always when you access the application after build for the first time (application restart)? – Ladislav Mrnka Dec 15 '11 at 20:26
  • It could also be due to a slow machine. How much memory do you have? How many CPU cores? Is it a laptop, or a workstation? – danludwig Dec 15 '11 at 20:26
  • What View Engine are you using? – SliverNinja - MSFT Dec 15 '11 at 20:33

5 Answers5

14

I'd start by checking what the timeouts are set to in IIS for the process to be recycling itself.

I'm also a very big fan of the MVC Mini-Profiler which could show you exactly how long various parts of your page load are taking, definitely take a look at it.

Edit:

It is worth noting that the Glimpse project is also great for this task these days.

sclarson
  • 4,362
  • 3
  • 32
  • 44
  • Well I installed MVC Mini profiler and it gave me a clue. I was making a long polling ajax request to a controller method that was async. Something was happening though, like not getting cleaned up or stopping when I left the page. After commenting this code out the pages started loading, except for initial built which is expected. – Mike Flynn Dec 16 '11 at 06:30
6

Sounds like it might be an issue with IIS AppPool recycling if you're experiencing it after builds or after periods of inactivity.

To help with AppPool timeouts you can utilize a batch file I created to help mitigate the issue.

That won't solve the problem for you after new builds because your ASP.NET MVC application needs to be JIT-compiled upon first run. If you're really eager to eliminate that issue, you can use ASP.NET precompliation.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Aaronontheweb
  • 8,224
  • 6
  • 32
  • 61
  • Wow, this made a big difference. I no longer have to wait 5s for my test server to warm up every time I visit it occasionally. What was the default idleTimeout? – avenmore Jul 18 '13 at 12:27
  • @avenmore that depends on your host; in general I think the default timeout is 20 minutes but I'm not sure offhand. It's a best practice to set it explicitly if you have that level of access to IIS IMHO. – Aaronontheweb Jul 25 '13 at 17:40
1

Try Glimpse or use ASP.NET Tracing.

You could also precompile your views if you are using the Razor view engine via Razor Single File Generator for MVC.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • 2
    Other items you may check are **external MVC dependencies** - such as **Session State**, **Data Services**, etc. that could be causing the slowdown. We just had an issue where excessive SQL Server Session State traffic caused the site to crawl (*addition of 200+ users*). This wasn't showing up in the tracing. – SliverNinja - MSFT Oct 29 '13 at 14:37
0

It depends on what happened in your previous run, sometimes if you throw an error and don't clear that out then you will have issues running the application. It helps to restart the browser every time you build if there was an error.

However, this could be an issue of caching. It is possible that your database is caching due to poorly maintained context disposing. This would cause the lookups to run faster and faster as they were encountered in pages. Make sure you always call .dispose() when done with your database transactions.

Travis J
  • 81,153
  • 41
  • 202
  • 273
0

funny - I've noticed something similar once with unity and mvc but the problem I believe resolved itself. You could also try ants profiler to see if the problem is outside of MVC.

If you let a single request sit there (without requesting 5+ times) what happens? Let a single request run - is ANY of your code hit? (setup logging log4net, nlog, etc) to run application_start, etc to see if any code is getting called after the compile.

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71