ElkArte Community

Project Support => General ElkArte discussions => Topic started by: Ant59 on November 10, 2014, 06:17:16 am

Title: Intermittent Loading Slowdowns
Post by: Ant59 on November 10, 2014, 06:17:16 am
I've been having problems with intermittent huge loading times for my Elkarte forum. I host other sites on the same Nginx server, which are not affected, so it doesn't seem to be a Nginx/PHP/MySQL problem. As far as I can tell, it's Elkarte's doing.

Every few requests, when I've not made request for at least 30 seconds, the next request will take upwards of 5s to load. This doesn't happen every request - most are less than 200ms with SPDY.

I've screenshotted the output of PHP-FPM's "slowlog" below and a waterfall of the page load from Firebug.

(http://i.imgur.com/BS0ALJL.jpg)

I'm hoping someone might be able to shed some light on this, as to whether these Elkarte functions are to blame or whether it's a server issue of some sort. I can provide as much information as needed.
Title: Re: Intermittent Loading Slowdowns
Post by: emanuele on November 10, 2014, 07:22:29 am
For "random" slowdowns I may think of two reasons:
1) a new caching of styles and scripts (javascripts),
2) a new caching of hostname lookups.

From the screen you are posting looks like the hostname lookup.
It's a bit strange the 30 seconds because the hostnames should be stored for 600 seconds, but... it may also be you are not using cache and it relies maybe on some server-side caching of the DNS record.

You may disable the hostname lookup in admin > config > server > general.
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 10, 2014, 07:27:37 am
Quote from: emanuele – You may disable the hostname lookup in admin > config > server > general.

I've disabled hostname lookup, and I'll see whether it makes a difference or not over the next hour or so. Thanks.
Title: Re: Intermittent Loading Slowdowns
Post by: scripple on November 10, 2014, 07:48:00 am
I see the same problem actually.  Hostname lookup has been disabled and just a pretty simple Apache setup.

If I haven't done a particular request for a while it takes about 4 to 5 seconds to generate the page according to Elk's own page load time output.  After that it goes back to about 0.03 seconds until some unknown timeout or something.
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 10, 2014, 07:50:26 am
Yes, that's exactly what I'm getting. I'm on Nginx, so it seems it's definitely an Elk issue.

I've not been seeing the issue since having disable hostname lookup, but I'll continue testing.
Title: Re: Intermittent Loading Slowdowns
Post by: emanuele on November 10, 2014, 08:02:40 am
If you are not on a live setup[1] you can add to your Settings.php the following line to try to get more info:
Code: [Select]
$db_show_debug = true;
it will output more debug info at the end of the page with also queries execution time and the time at which each query has been executed.

It happens to any account, or maybe just with admins?
Well, you could use it live as well, but it's not really so nice to see and may in certain circumstances reveal full file paths.
Title: Re: Intermittent Loading Slowdowns
Post by: scripple on November 10, 2014, 08:28:49 am
I'm live and an admin.  It is not restricted to admin pages though.  I've seen it on post, who, and unread frequently.
Title: Re: Intermittent Loading Slowdowns
Post by: scripple on November 10, 2014, 08:31:26 am
And sadly it's not readily repeatable for me.  I can't just not view a page for five minutes and know it will be slow every time.
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 10, 2014, 08:35:47 am

Testing now. Throws up a lot of information. I'll keep my eye on CPU time when I get a random slow load.
Title: Re: Intermittent Loading Slowdowns
Post by: emanuele on November 10, 2014, 09:16:46 am
What I'm trying to do is identify a more or less common pattern. For example, admins have some "security checks" done while loading the page (actually we should really do them randomly I think, it's useless to check Settings.php~ at each and every page load), so this may be a problem. Or maybe it happens only to registered members, because... dunno, maybe some ban check.

@ant59 another interesting and potentially useful is to see into "show queries" when the "time into the request" jumps. Usually the times have very small differences from the following one, but is something messes with timing and slows down something, you will notice the "jump" in time between two different queries (for example query 1 "which took 0.0002 seconds at 0.07130003 into request." and query 2 "which took 0.0003 seconds at 3.5 into request.").

Also if you have disabled the hostname look up (that based on the screen you posted before was the problem due to gethostbyaddr), have a look again at the PHP-FPM's "slowlog", it may give some more insights. ;)
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 10, 2014, 09:42:38 am
Refreshing it with "Show queries" open now. I've turned hostname lookup back on, but I'm having trouble replicating the issue now. It's super-weird because this issue has been happening for weeks, but now I can't reproduce it. PHP-FPM's slowlog isn't showing anything either now :S I even ran a long test with Seige on 1 concurrent user and not a single peak over 1 second after about 5 minutes running.

I'll keep going. I've not done anything I can think of that would have fixed it.
Title: Re: Intermittent Loading Slowdowns
Post by: scripple on November 10, 2014, 09:58:59 am
I've seen this problem basically since installing elk beta.  It's very random.  I'm sure it will show up again.

Ema is there a timeout on the css/js cache that will cause it to redo them even if they haven't changed?
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 10, 2014, 11:05:43 am
I understand the issue now. Elkarte is failing to find my hostname using the host or nslookup commands on the system, and is falling back to the PHP function "gethostbyaddr()" which is extremely slow. It finds many users' hostnames fine though, which is why none of my testers have come across it. Seems if you're using a common ISP, it has no problem.

I've simply completely disabled hostname requests from the admin panel. I can't think of any functionality that I lose?
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 10, 2014, 11:09:49 am
Can confirm my findings: Just ran a -c10 -r10 siege test against my site with hostname requests off and not a single request showed up in PHP-FPM's slowlog.

Code: [Select]
Transactions:		         100 hits
Availability:       100.00 %
Elapsed time:        26.05 secs
Data transferred:         3.39 MB
Response time:         1.84 secs
Transaction rate:         3.84 trans/sec
Throughput:         0.13 MB/sec
Concurrency:         7.05
Successful transactions:         100
Failed transactions:            0
Longest transaction:        1.31
Shortest transaction:         0.43
Title: Re: Intermittent Loading Slowdowns
Post by: emanuele on November 10, 2014, 11:38:26 am
Quote from: scripple – Ema is there a timeout on the css/js cache that will cause it to redo them even if they haven't changed?
Not that I can see:
https://github.com/elkarte/Elkarte/blob/v1.0.1/sources/SiteCombiner.class.php#L284
only if _isStale returns true a new compilation is prepared.
Title: Re: Intermittent Loading Slowdowns
Post by: Spuds on November 10, 2014, 04:23:18 pm
In doing some digging, it looks like the daily maintenance scheduled task is emptying out the cache directory.  This of course removes all the css/js hive compilations which then need to be regenerated as demanded, which can be slow. 
Title: Re: Intermittent Loading Slowdowns
Post by: Spuds on November 10, 2014, 04:34:36 pm
Yup, step one of daily maintenance
Code: [Select]
		// First clean out the cache.
clean_cache();

And doing that flushes the hive's etc by default ... Not sure why daily calls that TBH
So
a) remove that
or
b) add a parameter like clean_cache(full = true) and with daily call it as clean_cache(false) (with adjustments to protect the hive)
or
c) something else
Title: Re: Intermittent Loading Slowdowns
Post by: Adrek on November 10, 2014, 04:35:28 pm
I had similar issue, but I thought that it was my shared server... Then I found option Minify Javascript and CSS files, disabled it and site is loading much faster
Title: Re: Intermittent Loading Slowdowns
Post by: Spuds on November 10, 2014, 04:43:05 pm
That makes sense, it can be expensive to create those hives, so you don't want to do it daily.  I think we can just change the call in maintenance to be
Code: [Select]
clean_cache('data');
and that will take care of the problem.  Need to test that though.

ETA: that seems to work fine
Title: Re: Intermittent Loading Slowdowns
Post by: emanuele on November 10, 2014, 04:55:48 pm
Cool digging!
Yep, it doesn't make much sense to clean up everything.

Actually... why clean up the cache daily at all?
Title: Re: Intermittent Loading Slowdowns
Post by: radu81 on November 10, 2014, 07:51:25 pm
similar problem, 6,99 seconds to open the main page, then everything ok
Title: Re: Intermittent Loading Slowdowns
Post by: scripple on November 11, 2014, 02:10:15 am
Daily cache cleaning if it checks and removes really old things might make sense.  But otherwise I'm not sure the point. 
Title: Re: Intermittent Loading Slowdowns
Post by: scripple on November 11, 2014, 11:03:15 am
Made the change suggested by spuds.  Will see if that clears everything up delay wise over the next few days.

Thanks for all the help here.
Title: Re: Intermittent Loading Slowdowns
Post by: Spuds on November 11, 2014, 11:17:46 am
Great .. let us know how that works

For sure if a user / admin came to a site and triggered a hive generation it would be slow to load on that page, like seconds long.  And it would be random since a bot or someone else may have triggered it before,  And there are 100's of combinations, depends on settings and what action is being done.  Definitely something we don't want to throw out each day :P

As to why that call is there, I can see maybe some use for the filebased cache, that could leave crud behind.
Title: Re: Intermittent Loading Slowdowns
Post by: emanuele on November 11, 2014, 11:26:44 am
I was thinking that one option could have been to use a php script to serve the css and js to reduce the time (in the sense that loading the page an hash is computed, and passed as parameter to a file added to the head as script or link:
Code: [Select]
	<link rel="stylesheet" href="http://www.elkarte.net/community/assets.php?hive=89b6b8d4ab3ed05f8104005cc092f02f475877a9;type=css?d77925ee" id="csscombined" />
<script src="http://www.elkarte.net/community/assets.php?hive=9a8029a7644dc0290120719ff63ed5eb69a260c7;type=js?252f4f7b" id="jscombinedtop"></script>
for example, but even that way, it would mean that the css and the js would appear with some 2/3 seconds of delay on the page, resulting in an unpleasant experience.
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 11, 2014, 11:35:58 am
It's neat, but isn't minification and combining somewhat outside the scope of a forum engine? Surely that should fallback to the job of the actual server, using something like the PageSpeed module. I use the CloudFlare CDN with RocketLoader to do all that heavy lifting on my site.

Spoiler (click to show/hide)

I don't mean to be negative, I'm just curious.
Title: Re: Intermittent Loading Slowdowns
Post by: Spuds on November 11, 2014, 05:11:36 pm
I think you will find, for a lot of users, that is going to be to complicated, so we wanted to provide something that was a one click affair.  Nothing else to sign up for,  nothing required, etc.  Mods, themes, etc are just taken care of by the core right now.

What you have, using a CDN, is certainly the best way to go.  I've thought about writing an addon that would do just that (and to verify that we have the hooks in place to do that), meaning to build out the CDN js/css in place of the built in minimizing. 

QuoteI was thinking that one option could have been to use a php script to serve the css and js to reduce the time (in the sense that loading the page an hash is computed, and passed as parameter to a file added to the head as script or link:
I'm not sure what to do TBH ... its a one time slowdown and then its done (but it sucks when its you).  One thought I had was to have a task that gets kicked off once you click that button to start building min files of the js/css then the combiner would find those and use them in the hive with no delay.  Could even ship 1.02 with min files and until you change a file they would be good to use.

Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 12, 2014, 04:46:25 am
Quote from: Spuds – I think you will find, for a lot of users, that is going to be to complicated, so we wanted to provide something that was a one click affair.  Nothing else to sign up for,  nothing required, etc.  Mods, themes, etc are just taken care of by the core right now.

Ah, fair enough. It's certainly quick and easy :)

Quote from: Spuds – I'm not sure what to do TBH ... its a one time slowdown and then its done (but it sucks when its you).  One thought I had was to have a task that gets kicked off once you click that button to start building min files of the js/css then the combiner would find those and use them in the hive with no delay.  Could even ship 1.02 with min files and until you change a file they would be good to use.

What about pages requiring multiple scripts/stylesheets? Doesn't minimizing those in source defeat the purpose, since you've then got to combine and re-minimize them anyway?

The only way to avoid the need for combining sheets and scripts is to use SPDY, which not all web servers can support.
Title: Re: Intermittent Loading Slowdowns
Post by: scripple on November 13, 2014, 01:11:53 am
Just wanted to report that since changing the clear cache call to have the 'data' argument I've not seen the random slowdowns.  Will continue to watch.

Also I do appreciate the ability to have Elk combine and minify things without any external sites.

As to pre-generating the cached files what about a script that calls the predictably most common pages that random visitors are likely to see.  Forum index, category, board, topic, registration, profile, recent topics.  Ones that would show up for new users who might be especially put off by a site that seems slow to respond up front.  Once people are registered infrequent slowdowns that would occur after the site was modified likely would not drive them away.  And even for them you could probably handle most of it with the post page, pm pages, and new / unread topics.
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 19, 2014, 10:19:43 am
It seems that disabling hostname lookups was the cure. Since then, I've had no major slowdowns from script delay. The only thing to show up in the "slowlog" is BadBehaviour, which is a fair trade-off. Not using Elkarte minification or combiner.
Title: Re: Intermittent Loading Slowdowns
Post by: Spuds on November 19, 2014, 01:06:32 pm
Good to hears the source(s) of the slowdowns seem to have been found  :D One we can fix, one not sure we can do anything other than what we have now (checkbox to disable).  Anyone know why those hostname lookups are slow on some systems?  Is there an alternative other than disable?
Title: Re: Intermittent Loading Slowdowns
Post by: Ant59 on November 21, 2014, 09:57:24 am
If it helps, I narrowed the issue down to PHP's built-in gethostbyaddr() function here:

https://github.com/elkarte/Elkarte/blob/master/sources/Subs.php#L3191

The system calls above are instant. But using the PHP method seems to cause long delays.
Title: Re: Intermittent Loading Slowdowns
Post by: IchBin on November 21, 2014, 10:12:31 am
Maybe this is a result that could be cached?
It seems after a little bit of reading that some people are saying it causes the script to slow when an IP isn't resolved.
Title: Re: Intermittent Loading Slowdowns
Post by: emanuele on November 21, 2014, 01:43:38 pm
Reading around on the web it kind of confirms that gethostbyaddr is slow or can be slow and there isn't much to do about it.

@IchBin cache is already used, though is not really always useful in that case, because if the IP is not present (and the likelihood it is not is far greater than the contrary) in the cache we have to resolve it to get the hostname. ;)
Title: Re: Intermittent Loading Slowdowns
Post by: IchBin on November 22, 2014, 12:25:47 pm
I understand that it might not be useful in the case where the IP is not present. But in the case when a lookup can take a significant amount of time to actually obtain the IP, this could save the time in that case for the next time it looks up that same IP that doesn't resolve quickly. But if cache is already used for this, and the issue really is not being to find the IP at all, there's not much that can be done about that. lol