How CPU Time is Spent inside PHP Laravel Apps (using OpenResty XRay)
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.
Here we defined a request handler that emits a response, “Hello, world”.
Use the curl
command to access the Laravel HTTP endpoint.
The response body is “hello world” indeed.
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.
We used a client-side stress test tool to fully utilize the CPU to 100% beforehand.
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.
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.
You can choose the right machine from the list below if the current one is incorrect.
Go to the “Guided Analysis” page.
Here, you can see different types of problems that you can diagnose.
Let’s select “High CPU usage”.
Click on “Next”.
Select the PHP application.
Select the process that consumes almost 100% of the CPU resources. This is what we saw earlier in top
.
Make sure that the application type is right. Usually, the default should be correct.
OpenResty XRay can analyze multiple language levels at the same time. We’ll keep both PHP and C/C++ selected.
We can also set the maximum analyzing time. We’ll leave it as 300 seconds, which is the default value.
Let’s start analyzing.
The system will keep performing different rounds of analysis. Now, it’s executing the first round.
The first round is done, and it’s already on to the second one. That’s enough for this case.
Let’s stop analyzing now.
We can see it automatically creates an analysis report.
This is the type of problem we diagnose. It’s CPU.
This is the #1 hottest C code path for the CPU resources.
The first zend_execute
function is used to interpret and execute PHP opcodes.
When the server receives a new HTTP request, the php_cli_server_dispatch_router
function is called to read and parse the request data.
main
function shows this is the built-in web server started by the PHP CLI.
Click to see more details.
The hot code path was automatically derived from this C-land CPU flame graph.
Below are more detailed explanations and suggestions regarding the current issue.
It mentions the zend_execute
function we saw earlier.
Let’s look at the #1 hottest PHP code path details.
The bootProvider
function is a part of Laravel framework. It boots a service provider that is registered with the application.
Click “More” to see details.
Click the icon to enlarge.
Continue to zoom in.
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.
IgnitionServiceProvider::boot
method is responsible for booting all the service providers.
Let’s check out the #2 hottest PHP code path that consumes almost 14% of the CPU time.
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.
Click on “More”.
Enlarge the flame graph.
Continue to zoom in.
resolveProvider
is a method in the Laravel framework for resolving and registering Service Providers.
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.
The third hottest code path consumes about 13% of the CPU time.
Look at the details of this code path.
It implements a “hello world” response. This code path sends an HTTP response.
The first code path and the second path are not the same service, though similarly.
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.
Automatic analysis and reports
OpenResty XRay can also monitor online processes automatically and show analysis reports. Go to the “Insights” page.
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.
Though “Guided analysis” is useful for application development and demonstration.
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.