In this tutorial, we will demonstrate how the CPU time is spent quantitatively inside PHP Laravel applications with OpenResty XRay. These hot code paths are obtained by OpenResty XRay automatically analyzing and interpreting the PHP language-level CPU flame graphs.

Problem: high CPU usage

We prepared a hello world web application using PHP’s Laravel framework.

Screenshot

Here we defined a request handler that emits a response, “Hello, world”.

Screenshot

Use the curl command to access the Laravel HTTP endpoint.

Screenshot

The response body is “hello world” indeed.

Screenshot

Let’s run the top command to check the CPU usage of the target PHP process.

This is the PHP hello world service process we demonstrated earlier.

Screenshot

We used a client-side stress test tool to fully utilize the CPU to 100% beforehand.

Screenshot

Let’s run the ps command for more details about this process.

Here, we can see that it is using the standard php binary executable that comes with the Linux distribution.

Screenshot

Use the Guidede Analysis Feature of OpenResty XRay to Analyze CPU Distribution in Laravel Application

Let’s use OpenResty XRay to check how CPU time is spent inside the PHP process. Open the OpenResty XRay web console in the web browser. Make sure it is the right machine you are watching.

Screenshot

You can choose the right machine from the list below if the current one is incorrect.

Screenshot

Go to the “Guided Analysis” page.

Screenshot

Here, you can see different types of problems that you can diagnose.

Screenshot

Let’s select “High CPU usage”.

Screenshot

Click on “Next”.

Screenshot

Select the PHP application.

Screenshot

Select the process that consumes almost 100% of the CPU resources. This is what we saw earlier in top.

Screenshot

Make sure that the application type is right. Usually, the default should be correct.

Screenshot

OpenResty XRay can analyze multiple language levels at the same time. We’ll keep both PHP and C/C++ selected.

Screenshot

We can also set the maximum analyzing time. We’ll leave it as 300 seconds, which is the default value.

Screenshot

Let’s start analyzing.

Screenshot

The system will keep performing different rounds of analysis. Now, it’s executing the first round.

Screenshot

The first round is done, and it’s already on to the second one. That’s enough for this case.

Screenshot

Let’s stop analyzing now.

Screenshot

We can see it automatically creates an analysis report.

Screenshot

This is the type of problem we diagnose. It’s CPU.

Screenshot

This is the #1 hottest C code path for the CPU resources.

Screenshot

The first zend_execute function is used to interpret and execute PHP opcodes.

Screenshot

When the server receives a new HTTP request, the php_cli_server_dispatch_router function is called to read and parse the request data.

Screenshot

main function shows this is the built-in web server started by the PHP CLI.

Screenshot

Click to see more details.

Screenshot

The hot code path was automatically derived from this C-land CPU flame graph.

Screenshot

Below are more detailed explanations and suggestions regarding the current issue.

It mentions the zend_execute function we saw earlier.

Screenshot

Let’s look at the #1 hottest PHP code path details.

Screenshot

The bootProvider function is a part of Laravel framework. It boots a service provider that is registered with the application.

Screenshot

Click “More” to see details.

Screenshot

Click the icon to enlarge.

Screenshot

Continue to zoom in.

Screenshot

The Laravel service provider uses the ServiceProvider::boot method to register macros and configurations for the Carbon date library. The corresponding boot method is used to initialize settings such as timezone.

Screenshot

IgnitionServiceProvider::boot method is responsible for booting all the service providers.

Screenshot

Let’s check out the #2 hottest PHP code path that consumes almost 14% of the CPU time.

Screenshot

This register function is called during the application bootstrapping process. It is part of the registration process for the service provider registration process. In Laravel application, a new application instance is started on each request.

Screenshot

Click on “More”.

Screenshot

Enlarge the flame graph.

Screenshot

Continue to zoom in.

Screenshot

resolveProvider is a method in the Laravel framework for resolving and registering Service Providers.

Screenshot

DatabaseServiceProvider::register is a method used in the Laravel framework. It is part of the service container and is responsible for registering the database services.

Screenshot

The third hottest code path consumes about 13% of the CPU time.

Screenshot

Look at the details of this code path.

Screenshot

It implements a “hello world” response. This code path sends an HTTP response.

Screenshot

The first code path and the second path are not the same service, though similarly.

Screenshot

Here’s a performance comparison between Laravel and OpenResty. As you can see, Laravel’s performance is two orders of magnitude slower than OpenResty. The OpenResty framework is more efficient and advanced in its design and implementation.

Screenshot

Automatic analysis and reports

OpenResty XRay can also monitor online processes automatically and show analysis reports. Go to the “Insights” page.

Screenshot

You can find the reports on the Insights page for daily and weekly periods. For this reason, you don’t have to use the “Guided Analysis” feature.

Screenshot

Though “Guided analysis” is useful for application development and demonstration.

Screenshot

What is OpenResty XRay

OpenResty XRay is a dynamic-tracing product that automatically analyzes your running applications to troubleshoot performance problems, behavioral issues, and security vulnerabilities with actionable suggestions. Under the hood, OpenResty XRay is powered by our Y language targeting various runtimes like Stap+, eBPF+, GDB, and ODB, depending on the contexts.

If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!

About The Author

Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.

OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.

As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.