Symfony2 Slow Initialization Time - performance

I have Symfony2 running on an Ubuntu Server 12.04 (64-bit) VM (VirtualBox). The host is a MacBook pro. For some reason I am getting really long request times in development mode (app_dev.php). I know its slower in dev mode, but I'm talking 5-7 seconds per request (sometimes even slower). On my Mac I get request times of 200ms or so in development mode.
After looking at my timeline in the Symfony2 profiler, I noticed that ~95% of the request time is "initialization time". What is this? What are some reasons it could be so slow?
This issue only applies to Symfony2 in dev mode, not any other sites I'm running on the VM, and not even to Symfony2 in production mode.
I saw this (http://stackoverflow.com/questions/11162429/whats-included-in-the-initialization-time-in-the-symfony2-web-profiler), but it doesn't seem to answer my questions.

I had 5-30 sec responses from Symfony2 by default. Now it's ~500ms in dev environment.
Then I modified the following things in php.ini:
set realpath_cache_size = 4M (or more)
disabled XDebug completely (test with phpinfo)
realpath_cache_ttl=7200
enabled and set OPcache (or APC) correctly
restarted Apache in order to have php.ini reloaded
And voilá, responses went under 2 secs in dev mode!
Before: 6779 ms
After: 1587 ms
Symfony2 reads classes from thousands of files and that's a slow process. When using a small PHP realpath cache, file paths need to be resolved one by one every time a new request is made in the dev environment if they are not in PHP's realpath cache. The realpath cache is too small by default for Symfony2. In prod this is not a problem of course.
Cache metadata:
Caching the metadata (e.g. mappings) is also very important for further performance boost:
doctrine:
orm:
entity_managers:
default:
metadata_cache_driver: apc
query_cache_driver: apc
result_cache_driver: apc
You need to enable APCu for this. It's APC without bytecode cache, as OPCache already does opcode caching. OPCache is built in since PHP 5.5.
---- After: 467 ms ----
(in prod environment the same response is ~80 ms)
Please note, this is project uses 30+ bundles and has tens of thousands of lines of code, almost hundred own services, so 0.5s is quite good on a local Windows environment using just a few simple optimizations.

I figured out the cause of the problem (and its not Symfony2). For some reason on the ubuntu VM, the modification times on certain files are incorrect (ie in the future, etc). When symfony2 checks these times using filemtime() against its registry, it determines that the cache is not longer fresh and it rebuilds the whole thing. I haven't been able to figure out why it is doing that yet.

I also needed to disable xdebug (v2.2.21) to debug apache2 max timeout loading on my macbook. It was installed using macports:
sudo port install php54-xdebug.
With xdebug enabled, every page run out max loading time, with a fatal error exceeding max timeout message dispatched. When disabled, everything just loads fine in a reasonable expected time. I came to this using MAMP, no xdebug enabled by default, and apache2 just works fast as usual. I may change for another debugger, that's a pitty, because xdebug worked fine before.
Config:
MacOSX 10.6.8
macports 2.1.3
Apache 2.2.24
php 5.4

We have the same problem.
Here we have 10 second and more for every request.
I see if I remove following lines in bootstrap.php.cache all times return in normal state (298 ms).
foreach ($meta as $resource) {
if (!$resource->isFresh($time)) {
return false;
}
}
It's possible that we have wrong modifications times, but we don't know how to fix. Somebody know a solution?

As said at https://stackoverflow.com/a/12967229/6108843 the reason of such behavior might be Ubuntu VM settings. You should to sync date and time between host and guest OS as explained at https://superuser.com/questions/463106/virtualbox-how-to-sync-host-and-guest-time.
File modification date changes to host's value when you upload file to VM via FTP. So that's why filemtime() returns wrong value.

You can move APP/var/cache в /dev/shm/YourAppName/var/cache. But it's good to have built container in local files too for IDE autocomplete and code validation. In app/AppKernel.php:
public function getCacheDir()
{
return $this->getVarOrShmDir('cache/' . $this->getEnvironment());
}
public function getLogDir()
{
return $this->getVarOrShmDir('logs');
}
private function getVarOrShmDir($dir)
{
$result = dirname(__DIR__) . '/var/' . $dir;
if (
in_array($this->environment, ['dev', 'test'], true) &&
empty($_GET['warmup']) && // to force using real directory add ?warmup=1 to URL
is_dir($result) && // first time create real directory, later use shm
file_exists('/bin/mount') && shell_exec('mount | grep vboxsf') // only for VirtualBox
) {
$result = '/dev/shm/' . 'YourAppName' . '/' . $dir . '/' . $this->getEnvironment();
}
return $result;
}

I disabled xdebug and it resulted in a decrease loading time from 17s (yea..) to 0.5s.

I had problems as well with slow page loads in development, which can extremely frustrating when you're tweaking CSS or something similar.
After a bit of digging I found that for me the problem was caused by Assetic which was recompiling all assets on every page load:
http://symfony.com/doc/current/cookbook/assetic/asset_management.html#dumping-asset-files-in-the-dev-environment
By disabling the use of the Assetic controller I was able to drastically increase my page load. However, as the link above states, this comes at a cost of regenerating your assets whenever you make a change to them (or set a watch on them).

In app_dev, all the caches and auto loading is starting from scratch and what I found to be most slow in dev is the orm. I shy away from using orm and focus mainly on dbal because of it, although i probably shouldn't. Orm is used quite a bit in sf2. My guess is orm is what's slowing you down most in dev. Look at the difference between your dev config and prod config. However, some tweaks to your dev config can make development much snappier and enjoyable.. Just try and be aware of what your doing. for example, turning off the twig controller and then modifying a lot of templates will be kind of frustrating. Your need to keep clearing your cache. But like you mentioned, its dev only and when its time to go live, symfony will speed up for you.

Related

Very long time needed to access and parse file on a web server

I'm currently having an issue and I don't understand what causes it.
I could narrow it down to a very simple php script :
$start = microtime(true);
yaml_parse_file("someYamlFile.yaml");
print(microtime(true) - $start);
It's a 63 mb file I'm trying to parse.
When I try that script with Wamp server, on my own computer, it takes only a few seconds to complete.
However, when I run the same script on the remote server hosting my website, it takes about 170 seconds to run.
If I run it a second time, it only takes a few seconds though.
After several minutes, without any visitor to the website, if I run that script again, it takes 170 seconds!
I'm no expert when it comes to servers. Any idea on what could cause this?
Thanks.
Edit :
I tried fopen() and fread() instead and the issue remained.
It took more than 2 minutes to open and read the file the first time, and only a few seconds the second time.
It turns out the problem comes from accessing the file and not from yaml_parser.
I changed the title but still have no clue about what's causing the issue.
Everything you said is reasonable on a cheap server, on which the drive may be very inefficient.
Files are normally opened using sendfile, which uses the Linux Cache.
The loads being slow again after several minutes makes sense --> the cache is invalidated.
None of these issues are related to PHP :-)
I would recommend you put that file on a Ramdisk, so that it sits in RAM instead of on the disk.
/dev/shm is the default ramdisk on modern linux distribution ==> create a folder under it and place the file there.
As for putting the data in Memcache(where it should be): requires the Memcached pecl extension
To add(run once):
$m = new Memcached;
$m->addServer('localhost',11211);
$m->set('app_config',yaml_parse_file("someYamlFile.yaml"));
To load(run in your script):
$m = new Memcached;
$m->addServer('localhost',11211);
$data = $m->get('app_config');

Magento 1.9.2 Installation stuck on Configuration

I've tried every solution on this website and others, unfortunately nothing is working. I click continue on the "Configuration" page and it processes for a minute or two and then reloads the configuration page. I would assume it was timing out but it always takes different lengths of time.
I've tried installing on XAMPP, WAMP and a Vagrant VM machine but they all have the same issue.
The only thing I can think of is that it is related to the default ports. I'm usings ports like 3305 and 4447 because 80/8080 are taken on my work computer by IIS.
Is there an issue using the port suffix when creating magento installations?
I'm using php 5.5.12, according to Mage 1.9.2 documentation PHP 5.5 is required a minimum.
Things I've tried:
Switching between "localhost" and "127.0.0.1" with the various ports on the end
Removing the validation class from the base URL box
Checking "Skip Base URL Validation Before the Next Step"
Trying various URLs in the hosts file
There's also some other really specific things I tried that I can't quite recall because I've tried so much over the past 4 hours, but pretty much if it's suggested on this site or the Magento forums then I've already done it. Does anyone have any ideas?

Symfony first time slow loading

I have been developing a web page named: directorioelectronico.com and I have specially issues now, I will be very grateful that someone can be help me.
The web page is loading very slow in the first loading (5,000ms - 20,000ms) (latest are speeded normally) I tried to install APC module but my host is shared and the administrator can not install it, so I resize realpath_cache_size to 2M and the performance is now better (4,000 - 16,000 ms) somebody knows how I can perform it much more?
In advance, Thank you very much for you help.
My issue was that my share host haven't APC cache and for symfony2 is mandatory have it for have a good load so I change my host provider and now I have a VPS where I can install APC and now it is very fast.
The first time a Symfony program is run with env=prod, it has to create a significant amount of cached code - parsing the routes, annotations, converting configurations files and preparing CSS and Javascript.
It will always be a lot slower the first time, so that the rest of the time it will be fast. If you can run it before the website goes live (eg, with app/console), then that work can happen offline.
After clear:cache the next call on the application will have to rebuild a number cached files. That can be slow - so why make a site visitor trigger it?
If you're clearing the cache in production, try using the cache:warmup command to pre-build the the cache. It will mean the next visitor won't have to wait while the heavy lifting is done.
Something like this should help:
$ php ./app/console clear:cache --env=prod
$ php ./app/console clear:warmup
More info in the Symfony documentation.
I'd also suggest to enable query and result caches for doctrine (did you install/active apc cache for your php installation?). This might further reduce the loading time. Just have a look here :-)
Also try to use a deployment script to automatically trigger the cache clear/warmup, mentioned above. This way you won't forget to call those.
Do you use assetic for css/js? Then combine those files, minify them via assetic filters
Good candidates for deployment scripts are ansible, capifony or just a simple shell script.

Laravel 4 very very slow how to check what is slowdown?

i installed fresh copy of laravel4 , than installed Laravel 4 starter kit site:
laravelcp
My site run so slow between pages(loads):
969ms , and i have 950-1.5ms this very bad.
I using localhost wamp.
What i tryed:
optimize wamp.
i changed from localhost to 127.0.0.1 at database.php
i did both php artisan optimize and php artisan optimize --force
Also when i install fresh copy of laravel i have 130-160ms.
When i install other starter kit i have 320-400ms.
Anyway i am laravel 4 newbie , how can i check what makes the load time ?
maybe its some package or something.
If you have debug=true in app/config/app.php (or a local environment's config) you will not be cacheing anything. If you aren't using the cache then it makes sense that your load times with apc vs file cacheing would be similar.
First, try setting debug to false globally, or for your local environment. Then run php artisan optimize after cacheing is disabled.
Test your speeds using a direct route (no controller, simply return "some string"; from the routes.php route for the homepage.
Try returning the same string from a controller action. Map this action to the same route for the homepage and compare. On my local setup I see about a 10ms difference.
If that doesn't speed your app up then try installing the profiler suggested (or the one I prefer: https://packagist.org/packages/sebklaus/profiler) and see what is taking the most time to run. You can enable either profiler so that they run even when debug mode is false.
Another alternative is not to worry about local speed or speed during development and get laravel sped up once your app is working as you want.
Here are some tips to help you with the post-development optimization: Optimizing for production with Laravel 4
For simple debugging, there's a cool debugbar you can install and profile your app with:
https://github.com/barryvdh/laravel-debugbar
It'll show you how long laravel takes to boot up and gives you some other debugging and profiling options.
Also, it looks like you tried running 'php artisan optimize'. Just in case you didn't try it yet, make sure to turn off debug mode before optimizing. This will turn off lots of debugging features, but it will drastically reduce the amount of files Laravel needs to include.

Laravel 3 APC session lifetime is ignored

I have a Laravel 3 project, running on a plesk 11.5 CentOS 4(dedicated). It used to be on an IIS server, but i had to migrate it to plesk, since the company i'm working for is dumping the IIS server. Everything seemed to be running smoothly, until i logged out from my application, at first i got a WSOD (white screen of death), then i enabled php error reporting, and this is the error that was displayed:
Fatal error: Cannot override final method Laravel\Database\Eloquent\Model::sync()
This is a very strange error, since i have no method called Sync in any of my classes, and needless to say that there was no such error while the project was running on IIS.
I tried several different combinations of session/cache drivers, the only one that seems to be working is the APC driver.
When i have the APC driver enabled for cache and session, the above Fatal error is not displayed and everything works correctly. The PROBLEM is that i have given the Session Lifetime a value of 60(minutes) but it is completely ignored, meaning that the user is logged out after 2 or 3 minutes.
I've been to the Laravel IRC channel with this issue, some people kindly suggested to tweak the APC memory and ttl (time to leave) settings, but with no luck unfortunately :(.
Here are some APC settings from my server configuration:
apc.gc_ttl 3600
apc.shm_size 1024M
apc.shm_strings_buffer 32M
I desperately need help if anyone has any to offer! This is for a live running project and i need to find a solution asap.
I had the exact same issue and couldn't find a solution. I was going round in circles trying to figure out what on earth was going wrong.
I finally came across this post:
Fatal error: Cannot override final method
You need to make sure that the apc.include_once_override setting is set to 0. In your apc.ini file set like so:
apc.include_once_override=0
This error seems to be caused by caching of included classes.
I solved the problem after looking around the plesk panel.
The problem was that i had "Run PHP as FastCGI application" selected.
I switched to "Run PHP as CGI application" and everything works perfectly.
I'm not sure what the exact source of the problem was, only that FastCGI triggered the error.

Resources