Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel High CPU usage - PHP-FPM

I'm using Laravel 4.2, when I navigate through the pages the PHP-FPM spikes to 10-20%.

I checked it on 2 environments:
A:
Nginx Balancer - 2 CPU, 2GB.
PHP-FPM server - 1 CPU, 1GB.
MySQL server - 2 CPU, 2GB.

B:
Simple VPS on linode of 1 CPU, 1GB.
*SSD on all servers.

Now, I started investigating it more deeply using JMeter:
JMeter settings:

Number of Threads(users) - 100.
Ramp-Up Period(in seconds) -  1.
Loop Count - 5.
  1. Laravel 4.2, sent the requests to:

    Route::get('/test', function() { echo "Test"; });

Results: about 40 seconds of CPU on 100%.

  1. Laravel 5.2(clean installation), sent the requests to the welcome page.
    Results: CPU usage on 100% for 20 seconds.

  2. Phalcon(clean installation): sent the requests to Hello world page.
    Results: CPU usage on 50% for 2 seconds.

  3. Codeiginiter(rest api server, same config): sent the requests to a page that loads data from the DB(returning JSON).
    Results: CPU usage on 55%-60% for 3 seconds.

    • Same results on both environments.
    • Disabled debug mode, database connection, changed the configs and so on.
      PHP-fpm config:
      pm.max_children = 9
      pm.start_servers = 3
      pm.min_spare_servers = 2
      pm.max_spare_servers = 4

I understand that Laravel should be slower but I doubt the difference should be that big? I googled for hours, I encountered a few posts that have the same issue as mine but without any solution:

http://laravel.io/forum/09-25-2014-what-steps-should-i-take-to-reduce-laravels-high-cpu-load
http://laravel.io/forum/08-12-2014-cpu-load-general-performance-question

What am I missing here?
Thanks in advance.

like image 900
Jack Avatar asked Mar 27 '16 00:03

Jack


1 Answers

That might sound obvious and off topic, but make sure your PHP installation includes OPCache extension php-opcache. That helped me a lot.

OPCache gradually increases PHP performance by storing precompiled script bytecode in shared memory, sot does not repeatedly load/parse scripts for each request.

You can get more information from PHP OPCache Documentation

like image 96
Yerke Avatar answered Oct 19 '22 11:10

Yerke