Using profiling systems, you can collect information about which functions in PHP code consume more CPU time and RAM, that is, to identify the slowest and most memory-demanding places in a PHP program.

xhprof

XHProf - PHP profiler developed by Facebook.

Installation:

Aptitude install php-pear pecl install xhprof-0.9.4 echo "extension=xhprof.so" > /etc/php5/mods-available/xhprof.ini ln -s /etc/php5/mods-available/xhprof.ini /etc /php5/conf.d/xhprof.ini apachectl restart

The files necessary for work are located in the directory /usr/share/php. However, not everything, but only with PHP code. For normal display of reports, jquery and css are required. They can be obtained from the github repository:

Git clone https://github.com/facebook/xhprof.git

After this, add the line to the PHP script code in the place where data collection should begin:

Xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

Parameters for data collection are indicated in parentheses. In this case, data will be collected on processor load and RAM usage. One more option is possible XHPROF_FLAGS_NO_BUILTINS when used, data on built-in functions is not collected.

$xhprof_data = xhprof_disable(); include_once "xhprof_lib/utils/xhprof_lib.php"; include_once "xhprof_lib/utils/xhprof_runs.php"; $xhprof_runs = new XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test"); echo "Report: http://domain.tld/xhprof_html/index.php?run=$run_id&source=xhprof_test"; echo "\n";

In line $run_id The quotation marks indicate the name of the profile, which can be set arbitrarily.

The processed result looks like this:

If you specify the parameter XHPROF_FLAGS_NO_BUILTINS, it is clear that the number of function calls is significantly reduced:

The table provides the following information:

Calls- number of function calls,
Wall Time- the total operating time of the function, including the time spent waiting for a response from external resources,
CPU- how much time was spent processing functions,
MemUse- how much RAM was used,
PeakMemUse- peak memory consumption.

The modifiers are:

Incl- inclusive - taking into account calls to other functions from this function,
Excl.- exclusive - excluding function calls.

In addition, above the table there is information about the total processing time, memory used and the number of function calls.

Also XHProf allows you to build difference reports between two runs, which are indicated by red and green colors. With these reports, you can get a clear picture of improvements after each code change.

To obtain such a report, you need to use a link like this:

http://domain.tld/xhprof_html/index.php?run1=run_id1&run2=run_id2&source=xhprof_test

Where run_id1 And run_id2- launch identifiers.

If you install Graphviz:

Aptitude install graphviz

There are also third-party web interfaces for php profiler xhprof that use databases:

xDebug

xDebug- PHP code debugger with profiling capability, written by Derick Rethans.

Installation:

Yum install php5-xdebug

Then we edit the config:

Nano /etc/php5/mods-available/xdebug.ini

adding the lines to it:

Xdebug.profiler_enable = 1 xdebug.profiler_aggregate = On xdebug.profiler_output_dir = /tmp

Here we enable the PHP profiler and specify the directory in which to store the profiles. Profiles are created with names like cachegrind.out.*

There is a webgrind web client: https://github.com/jokkedk/webgrind. It doesn't work too fast, but it allows you to quickly view small profiles. In fact, this is the PHP code that needs to be cloned from github:

Git clone https://github.com/jokkedk/webgrind.git

a directory will be created webgrind, which you need to copy to the directory of any site and access it from the browser. Next, to make plotting in the configuration file work in Debian config.php need to fix the path to executable file graphviz. It should look like this:

Static $dotExecutable = "/usr/bin/dot";

In addition, you can adjust the time zone:

Static $defaultTimezone = "Europe/Moscow";

In the header, you can select a profile and check the box to take into account built-in functions. The table itself shows the functions, the number of calls, the operating time of the function itself and the time including waiting. To go deeper into the functions, just click on the triangular arrow. In my case, with fairly large profiles (from several megabytes), the wait for the result was unnecessarily high. It is probably better to use for fairly large profiles local programs prosmora.

The graph might look like this:

note that webgrind should not be used on production servers, since no authorization is provided, but there is access to the php file code. If necessary, use at least basic Apache authorization.

There are also programs for analyzing profiles for Linux:

About profiling

Profile data can help you improve your application, that is, achieve certain goals, for example, reduce memory consumption, reduce page generation time, and so on.

The information in the profile is the starting point for optimization: it tells you how long it takes to generate a result, how much memory is used, and how many function calls are made. With more detailed data, you can improve these metrics.

For example, if you are using a framework, then using some of the framework's functions may lead to calls to several core functions. If you are reading some data multiple times, it may be worth storing the result in a variable.

The profiler can also help you understand where to use PHP code caching, for example, using APCu or memcached.

First of all, it is worth optimizing the functions that require the most execution time. Once everything is optimized and it seems that there is nothing left to improve, it is worth sorting functions by the number of calls and working on reducing it. Even if PHP is fast, it's worth considering whether functions need to be called that often?

If you encounter the following situations, you should consider caching:

  • Immutable functions are called inside a loop,
  • Some content is generated twice,
  • Content that does not change is generated every time,
  • Content is generated even if not used.

You shouldn't cache everything, since memory is also a valuable resource. Cache the data you access constantly. Also, caching makes little sense if caching wastes more resources than it saves.

In addition to caching in the code, do not forget about caching using the web server (), as well as on the client side. If you use the right headers, many requests can be resolved before they even reach the server.

Signaling that a person has “failed.”

Many people don't notice this at all.

The “concrete slab” on their head seems so familiar and familiar that they perceive the transition to a creative state - when they are full of strength and feel omnipotent and one with everything that exists - as something unusual... for them this is a powerful breakthrough.

But this is the NATURAL state of a person?!

So when you:

Step towards YOURSELF. Challenge every day

Don't know how to learn to love yourself?

Get 14 exercises that will help you accept yourself and your life in its entirety!

By clicking the “Instant Access” button, you consent to the processing of your personal data and agree to

  • You fall into self-pity
  • You often feel in a bad mood
  • You feel small and worthless
  • You are driven by emotions
  • Acting “automatically”
  • I don't want to get up in the morning
  • You face constant obstacles
  • Feeling powerless

it's safe to say you FAILED.

And the very first step on the path of “healing” is SEE and realize, that you have fallen... fallen out of your usual creative state.

The sooner you do this, the sooner you will take action to get back TO YOURSELF!

If you “failed” - How to get out of a 3D hole

As soon as you realize that it is not the world around you that has suddenly become gray and dull, but that you yourself have begun to perceive it as such, your main task is to SWITCH.

You can do this in different ways:

1. Play music that inspires you

Irritation, fatigue, and bad mood create a cocoon of low-vibration energy around you.

Gather your collection of tracks and compositions that will allow you to tune into a higher vibration.

What could be easier than turning on your favorite songs in the player and doing your usual things!

Usually in such a state one is drawn to something melancholic and even decadent, to match the mood... Switch urgently!

At such moments, I myself turn on Hallelujah performed by Anastasia Dobrovolskaya)))

When you fail, you feel isolated and no one appreciates or understands you. Bitterness and self-pity grow inside.

At this moment, your heart is covered with heavy armor designed to protect you.

It is impossible to enter a creative state with a closed heart!

So your task is restore your own sensitivity. This can be done through meditative practices (see point 6).

Or combine the benefits and pleasure of watching a good movie.

Comedies won't do. You need a movie that touches your soul and makes you empathize!

For example, these:

3. Immerse yourself in vivid impressions

Your soul is nourished by bright colors. Give her this. Even through “I don’t want”!

Get out of the house. go shopping, galleries, museums. In real life or virtually (the Internet allows this).

Personally, I am very inspired by the lights of the city at night.

And last night we found ourselves in a fairy tale: there was fog outside, and we decided to go to the sea... The rustling of waves, the milky fog around, and the mesmerizing light of lanterns... completely surreal. It's a pity that technology cannot convey this beauty.

4. Do what you love

When you immerse yourself in what you love to do, intrusive thoughts, doubts and questions fade into the background.

You activate your creativity and CREATE.

You may not immediately be able to catch the drive of what you are doing... the main thing is not to stop halfway. This is your resource state, and you know it.

So don’t put it off until later, take the time to plunge into what you love with passion.

5. Get away to nature

Nature has no bad weather, no bad mood. Nature allows you to balance naturally.

Even if you go for a walk with the whole family, ask you not to touch it for 10 minutes. Step aside, listen to the sound of the wind, the creaking of the trees, touch the rough bark. Look into the sky. Absorb the balance of nature into every cell.

Once you have shifted your attention, it is time for the next step. Now you are more or less balanced and can perform simple practices that will support you in returning to your usual state.

Any methods of harmonization and balancing, practices for opening the heart and building a vertical channel of the Spirit, as well as energy practices to increase your energy level are suitable.

6. Open your Heart

Your grievances, claims, anger at yourself and self-criticism are blocking the energy channels of your heart drop by drop.

7. Ask for blessings and support from your Higher Self

At the moment of failure, you feel like a small person, so ordinary and spontaneous that you most likely forget that you are not only your body, your thoughts and feelings.

You forget that there is a wiser and more advanced part of you - your Higher Self, which does not interfere in your life until you DO NOT ask about this.

This means, “Ask and it will be given to you”!

If you have never met/communicated with this part of you, meditation will help you do so.

8. Forgive yourself for failure.

Whatever the cause of your condition, know that you subconsciously consider yourself responsible, and in severe cases, guilty, for what happened.

The Ho'oponopono cleansing technique consists of 4 steps: I'm so sorry. I'm sorry. Thank you. Love you.

Many years ago, when my world seemed to be collapsing before my eyes, the following phrases appeared in my head:

I forgive you. I thank you. I release you with love.

For several hours in a row I said them out loud like a mantra, and then I realized that I had forgotten an important element, and added: I forgive myself!

Forgive YOURSELF for this experience. Thank YOURSELF for it. And let go with love.

9. Sing the Song of Gratitude

Many people have mystical experiences associated with this song. For this to work for you, it is important to fulfill 3 conditions: 1) retire for 15 minutes so that no one will distract you and you feel relaxed, 2) turn on the speakers to maximum, 3) sing along with Deva Premal the female part of “Hallelujah” in full force.

Hallelujah is a song of gratitude. For everything that was and is in your life.

Catch the mood of this composition and don’t be shy to sing the way you know how. Your voice will tune itself to the desired vibration.

And since the throat center is responsible for your self-expression in this world, you pour out not only gratitude, but also express who you are at your core, without masks, pretensions and failures.

Try it! You won't regret it!

P.S. I suggest posting in the comments the names of the films that touched your soul (we won’t refuse music tracks either).

P.P.S. I would be grateful if you could share links to your favorite videos on Youtube, my stock is already running low ((

Application profiling is the collection of data on the execution speed of various program sections (files and functions). There are many tools PHP profiling, but not all tools are suitable for performing analysis directly in production.

XHProf— a mega simple profiler that collects statistics directly while the application is running, with almost no overhead.

Why profile?

If an application starts to run slowly, profiling can help you find out which part is slow. The result of profiling is usually a list of executed functions and their execution time.

Profiling should be done before any application optimization. Otherwise, you will be guided by guesswork. Most likely incorrect.

Xdebug problem

Xdebug is a powerful solution for PHP. But the Xdebug platform itself is so heavy that it cannot be used on live sites. XDebug creates a significant load on server resources and slows down the application.

On the other hand, problems on a live site may be completely different from those in a development environment. Profiling only on developer computers will show only a portion of the problems.

That is why the solution was developed XHprof. It is intended for use in running applications. The main idea of ​​this profiler is to create a minimum load on the application while collecting all the necessary data on the speed of operation. The solution was developed by the guys from Facebook and is supported by new versions of PHP.

XHProf

Installation

On Debian XHprof is in sid packages, so: apt-get install xhprof

You can also build XHprof yourself.

Enable profiling

Let's say we have a script with the following code:

execute();

Let's carry out profiling using XHprof. To do this on this page you need:

  1. Enable the profiler at the very beginning.
  2. At the very end of the program, stop the profiler and save the received data.

It will look like this:

# Initialize the profilerxhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); # Execute the program after turning on the profiler execute(); # Stop the profiler after executing the program$xhprof_data = xhprof_disable();

# Save the profiling result to the $xhprof_data variable

  • Function xhprof_enable() takes flags as arguments. XHPROF_FLAGS_CPU for recording processor statistics, XHPROF_FLAGS_MEMORY for memory, XHPROF_FLAGS_NO_BUILTINS for ignoring built-in functions.
  • xhprof_disable() will turn off the profiler and return the collected statistics.

Reports

Generation

The collected data can be analyzed in the XHprof interface to generate reports. To do this, you need to download the XHprof sources: cd /var/www; wget http://pecl.php.net/get/xhprof-0.9.4.tgz gzip -d xhprof-0.9.4.tgz tar -xvf xhprof-0.9.4.tar

After this, you need to make changes to the script:

include_once "/var/www/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php"; include_once "/var/www/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php"; $xhprof_runs = new XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, "test");

# New code saves the report for use in the GUI

Interface for reports

To see the report, you need to configure the virtual host to the /var/www/xhprof-0.9.4/xhprof_html folder. For example, in Nginx:

Server ( server_name xh..9.4/xhprof_html; index index.php; location ~* \.(php)$ ( fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ) ) nginx -s reload

After this, a list of reports will appear:

The table contains a list of functions that were performed within one page with additional information:

  • Calls — number and percentage of function calls.
  • Incl. Wall Time — execution time of a function with nested functions.
  • Excl. Wall Time is the execution time of a function without nested functions.
  • Incl. CPU - processor time with nested functions.
  • Excl. CPU - processor time without nested functions.
  • Incl. MemUse - memory consumption with nested functions.
  • Excl. MemUse - memory consumption without nested functions.
  • Incl. PeakMemUse - maximum memory consumption with nested functions.
  • Excl. PeakMemUse - maximum memory consumption without nested functions.

Graphic reports

To build a graphical report, make sure you have graphviz installed: apt-get install graphviz

Resource-intensive sections of the code are highlighted in yellow (medium) and red (heaviest). These are those sections of code that use a lot of resources relative to the rest of the program. This could be one slow function or many calls to a fast function. In our example, the function str_replace() marked red due to 262 calls.

Aggregate reports

The XHprof interface also allows you to view aggregate information from several reports at once. To do this, run_id is passed separated by commas: http://xh..php?run= 53a894f6d5d9b,53a894fcf126e&source=test

TL;DR

Use XHprof to profile PHP directly in production.

The profile data loop is the most complex out of all the custom BuddyPress loops. It’s actually two loops in one, the first is to loop through profile field groups, and the second to loop through profile fields in that profile field group.

Standard Loop

This user does not have a profile.

Accepted Parameters

The bp_group_has_profile() function will accept a number of parameters that will manipulate the data being returned.

  • profile_group_id optional

    By default all groups and all fields will be displayed. If you provide the ID of a profile field group, then only the fields in this group will be displayed.

    • Default value: false
  • user_id optional

    The ID of the user you want to fetch the profile data for. This is required if you are outside a member profile URL (/members/andy/…), otherwise it is the ID of the displayed user.

    • Default value: bp_displayed_user_id()
  • member_type optional

    Limit fields by those restricted to a given member type, or array of member types. If $user_id is provided, the value of $member_type will be overridden by the member types of the provided user. The special value of ‘any’ will return only those fields that are unrestricted by member type – i.e., those applicable to any type.

    • Default value: false
  • hide_empty_groups optional

    By default empty groups will not be displayed. If you provide the 0 value, then all the groups will be displayed.

    • Default value: true
  • hide_empty_fields optional

    By default, only show empty fields if we’re on the Dashboard, or we’re on a user’s profile edit page, or this is a registration page. If you provide the 0 value, then all the fields will be displayed on your page.

    • Default value: !is_network_admin() && !is_admin() && !bp_is_user_profile_edit() && !bp_is_register_page()
  • fetch_fields optional

    Whether to fetch each group’s fields.

    • Default value: false
  • fetch_fields_data optional

    Whether to fetch data for each field. Requires a $user_id .

    • Default value: false
  • exclude_groups optional

    Comma-separated list or array of group IDs to exclude.

    • Default value: array()
  • exclude_fields optional

    Comma-separated list or array of field IDs to exclude.

    • Default value: array()
  • update_meta_cache optional

    Whether to pre-fetch xprofilemeta for all retrieved groups, fields, and data.

    • Default value: true

Advanced Usage

Fetch all the profile data for the user with ID 10 .

Fetch the profile data for fields in the profile group ID 2 for the user with ID 10 .

Fetch the profile data for fields in the profile group ID 2 .

Fetch all the profile data, even empty fieds and groups, for current user.

Fetch all the profile data for the user with ID 10 , excluding fields with ID 5, 6, 7 .


Close