Join Justin Yost for an in-depth discussion in this video Xdebug: The basics, part of PHP: Performance Optimization.
- [Voiceover] Xdebug is the profiling and debugging tool to use with PHP. It provides a wealth of resources and we'll cover most of the primary ones through the next few videos. At this point, you'll want to install Xdebug for your machine. The process is going to be different depending on your computers operating system. If you go to Xdebug.org/docs/install, you'll find instructions for your specific operating system. For Windows users there are some pre-compiled binaries that you'll need to install.
And for the Mac users out there, you can either install it directly from PECL or of you use Homebrew, install it with Homebrew. Please do so now. At this point, you should have downloaded the exercise files for this course. In our directory for chapter two, video one, we have a sample project that we'll be working off of. This project is a simple PHP application. It's built using the KPHP framework. The library has some courses covering KPHP, if you want more details.
But we won't need any real knowledge of KPHP to work within this project. Mostly, we'll be working with standard PHP and such to solve our problems. To load this application, we need to install Composer. If you don't already have Composer installed, you'll want to do that now via the instructions for your system at getcomposer.org. Under the documentation menu option. And then in the introduction section. It includes all the help you need for getting composer installed on your system.
There are again also courses within the library that go into depth on Composer. Now that you have Composer installed and the exercise files for this course downloaded, you can load the necessary packages we need to run the files by navigating in our terminal application to where you downloaded the exercise files. Inside of the files for this chapter and video, you'll want to execute the command from your terminal application, Composer, install. Doing this should install the various dependencies for this project.
Now you should be able to run the command bin cake server. If you're on a unix based system, like Macintosh, it's going to be bin/cake server. However, if you're on Windows, the command is going to be bin\cake server. This uses a feature from PHP 5.4 to spin up a local web server for us with very little fuss. You note this command outputs a URL of local hosts, port 8765.
This is where our application is viewable from a browser. If we go there now, and go to local host port 8765, we should see the homepage for our application. We'll dive into the app later on. Right now, let's get a focus on seeing what value we can get out of Xdebug. We'll navigate to the URL localhost port 8765 /pages/xdebug. Here we're going to be able to see some of the basic Xdebug functions.
The first is Xdebug memory usage. This reports the memory used by the PHP script currently executed up to this point in time. Notice this is one of the key things we might want to measure for improving performance. Lowering this number, our memory usage, means our PHP script is using less memory. And presumably going to be faster. It's also an easy number to log against in development and set a limit of some number of bytes that all pages should be able to execute with that amount of memory.
And then work on improving the performance of any pages that are exceeding that limit. The next value is the Xdebug peak memory usage. Similar to memory usage, instead this one however, shows the peak memory usage. This is there most memory consumed by the PHP script during its execution. The reason this differs from the memory usage is as you no longer need resources by the PHP script, it frees up part of its memory because it knows it doesn't need the memory to store that particular variable any longer.
The next main value we get is Xdebug time index. This is the time the PHP script in question has been executing in seconds. Again, super critical towards working to improving the performance of our PHP script. Notice, if we reload the page a few times, we'll get slightly different values for all of these numbers. In general, however, they should be within a certain range depending on your own machine. Remember, the goal with performance is not to achieve some precise exact value, but to produce measurable improvements from what it was before.
You'll also note that sometimes the numbers change more than other times. That's because we wind up hitting different caches along the way from some of our refreshes and other times we don't. Next, we are going to explore Xdebug in further detail with stack traces.
- General optimization techniques and tools
- PHP and Xdebug
- Opcode cache
- Optimization in PHP
- Upgrading PHP
- Macro vs. Micro optimization
- HTTP caching
- HTTP compression