Ahmet Emin Koçal
Ahmet Emin Koçal

Reputation: 41

How can I detect which function uses the cpu most?

How can I detect which function uses the cpu most? I get around 20 reqs/s usually, but the cpu usage is around %50 which is terrible. I have no doubts about the server's performance as it is mine.

I need to find out the most cpu consuming function in my script but I don't know how to do it. I use custom script, I've written it all.

Upvotes: 0

Views: 113

Answers (1)

IVO GELOV
IVO GELOV

Reputation: 14269

In my opinion you can get very good results with Tideways and XHGUI.

UPDATE

Install latest community edition of MongoDB

  1. Create file /etc/yum.repos.d/mongo-org-3.4.repo with the following content

    [mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

  2. Install MongoDB – sudo yum install mongodb-org

  3. Run MongoDB for the first time (it will start automatically on boot) – service mongod start

Install PEAR (to have PECL), then the PHP extension for MongoDB

  1. Run sudo yum install php-pear
  2. Run pecl install mongodb
  3. Add the extension to php.ini and restart PHP-FPM extension=mongodb.so

Install the Tideways extension for PHP profiling

  1. Create file /etc/yum.repos.d/tideways.repo with the following content

    [tideways] name = Tideways baseurl = https://s3-eu-west-1.amazonaws.com/qafoo-profiler/rpm

  2. Import the RSA key - rpm --import https://s3-eu-west-1.amazonaws.com/qafoo-profiler/packages/EEB5E8F4.gpg

  3. Install the extension – sudo yum install tideways-php
  4. Edit the config /etc/php-5.6.d/40-tideways.ini and disable autoprepending

    tideways.auto_prepend_library=0 tideways.auto_start=0 tideways.sample_rate=100

  5. Restart PHP-FPM – service php-fpm restart

Install the XHgui

  1. Download from https://github.com/perftools/xhgui/archive/master.zip
  2. Extract into /var/www and rename the folder from xhgui-master to just xhgui
  3. Update permissions – chmod 777 /var/www/xhgui/cache
  4. Make a virtual host for nginX and point it to /var/www/xhgui/webroot
  5. Create indexes in MongoDB

    $ mongo

    > use xhprof

    > db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )

    > db.results.ensureIndex( { 'profile.main().wt' : -1 } )

    > db.results.ensureIndex( { 'profile.main().mu' : -1 } )

    > db.results.ensureIndex( { 'profile.main().cpu' : -1 } )

    > db.results.ensureIndex( { 'meta.url' : 1 } )

    > db.results.ensureIndex( { 'meta.simple_url' : 1 } )

    > exit

  6. Navigate to /var/www/xhgui and run the installer – php install.php

  7. Setup an autoprepend directive in php.ini and restart PHP-FPM

    auto_prepend_file = /var/www/xhgui/external/header.php

  8. To limit the disk usage of MongoDB (e.g. to 5 days = 432000 seconds)

    $ mongo

    > use xhprof

    > db.results.ensureIndex( { "meta.request_ts" : 1 }, { expireAfterSeconds : 432000 } )

Upvotes: 1

Related Questions