12 Simple PHP Performance Tuning Tips

12 Simple PHP Performance Tuning Tips

Juliet Mendez Developer Tips, Tricks & Resources


In every web application, performance really matters. Users complain about slow loading pages, inaccessible pages, unresponsive links and many other factors that lead to losing those users as customers. In order to satisfy your users’ needs, performance must be continuously checked and monitored. Every software application needs tools for monitoring and measuring the system’s performance.

Here are 12 simple ways to tune your application’s performance.

PHP Performance Tuning Tips

Bottlenecks

The first step is to identify the impediments in your application’s performance in order to find the root cause of the problem. By identifying the problem, you can plan and select the best possible solution and option for your application. You can then implement the solution, and afterward you can measure the results. You can use a performance tool such as Prefix or Retrace.

Prefix is used to highlight slow queries, which validates the behavior of your code and much more. You should recognize which optimization tool might help you in measuring the performance of your application. You should also make a list of your application’s needs and order it from highest to lowest priority. This way you can identify that some items are not important to worry about.

Profiling

There are a lot of profiling tools you need to identify that would fit your needs. Every PHP profiler has its own features and advantages. I’ve been browsing for a PHP profiler in the internet and everyone has its own advantages and disadvantages in using it. Stackify has Prefix which is used as a profiling tool. It has unique characteristics that allow you to get continuous feedback on the performance of your server-side code, SQL and other methods used in your system. The best and most impressive feature in Prefix is that it visualizes the entire request pipeline when developing web applications.

Once you download and install Prefix, you can directly see information about

  • the total time it took to serve the page
  • there was a caught exception
  • the status code returned was 200

and other important details that you need to monitor and to know about your application.

Code optimization

One of the methods to ensure the quality of your code is to have the best code optimization process and techniques. Your code may be optimized so that it utilizes memory, executes more rapidly, and also performs fewer input and output operations. Even though code optimization is one of the best tuning tips, it does not mean that your code must be complex, or you need to replace any standard libraries. Sometimes optimization takes up a lot of time to trace and maintain the code. Worst case, these optimizations don’t contribute any advantage because you’re wasting a lot of time optimizing non-critical portions of your application.

Most applications typically use multiple dependencies such as PaaS services, ElasticSearch, Redis, queueing, SQL & NoSQL databases, MongoDB and a lot more. Prefix helps developers understand if their code is using all dependencies correctly. It also lets the developer know how the all of the dependencies impact the performance of their app.


PHP Retrace

Configuration optimization

In any PHP application, the first thing you do is to setup the configuration and other environments applicable in your application. You may be able to add so many features that boost your application performance and optimize your code, but a proper configuration of your PHP runtime environment would also matter. Configuration optimization also permits you to optimize the performance of your application, as well as ensure the reliability and cost-effectiveness of your applications system storage. Changing any settings of your application may cause some PHP applications to stop functioning. It’s necessary that you understand the idea of modifying certain settings, but always keep in mind that when you disable something you must follow up your changes with a test run in your environment.

Distributed computing

Distributed computing is practiced for increasing the potential for parallel execution. This technique can increase load on shared resources most likely in database systems. Most web applications have issues and problems when it comes to latency and bandwidth distribution, but distributed computing can help minimize the latency and avoid bottlenecks. This way distributed computing can be highly beneficial for your system from distributed caches. Caching tips will be the next topic below.

Caching strategy

A good caching strategy could reduce the number of database operations and code compilation. It is often wise to use Memcache for lowering the database load, the Alternative PHP Cache(APC) for opcode caching and code optimization. There are a lot of options when we talk about caching in PHP opcode. These include APC (which is free!), eAccelerator (free), XCache (free), and Zend Platform. Stackify first uses the Windows Azure Managed Cache, but a couple of complaints has been received. So Stackify decided to try Redis, which is now supported by Azure. Redis is much more recommended rather than the Windows Azure Managed Cache. My best advice is to evaluate properly each cache strategy to see which will best suite your needs and delivers the best results.

Load balancing

In a web application, load balancing has not much to do with the application, but it will impact more on the hosting and infrastructure. In PHP, load balancing is configured in the plugins using various techniques such as random, round-robin, and user’s filter. You can also do some prioritization when it comes to server’s assignment; in this way, some prioritization can be handy in heterogeneous environments. The only thing you should always keep in mind is that session objects should be avoided since it is stored in memory of your local machine. In this way, synchronization of your data may be misleading during backup or during the actual data processing. Aside from avoiding session objects, you should not also store files like images on the server. It would be better to place them in the cloud. Lastly, if you have servers that are the same configuration, always bear in mind that some servers are better than others in hardware configuration.

In most cases, the major problem with load balancing is when dealing with the database and backup recovery. In Stackify, load balancing is not much of a problem since it optimizes the cloud, Azure to be specific. The cloud offers a disaster recovery environment which is configured, ready, and scaled suitably when necessary.

Avoid client-side

In every web application, when you have so many redirections of your pages, it can decrease the page speed significantly. It is suggested that you constantly check and remove or lessen the redirection of your pages. On the other hand, redirecting users to an SSL version of your page won’t work at all.

Security (HTTP/2 over SSL)

Hypertext Transfer Protocol is an application protocol which operates on the TCP/IP layer. HTTP is responsible for establishing a connection between server and client. It also responsible for processing users request in a web page or in the browser. Secure Socket Layer (SSL), on the other hand, is the secured version of HTTP. Most of the communication between web server and client are being encrypted by SSL.

In today’s technology, most of our browser already supports HTTP/2. Although browsers do support HTTP/2, it actually has limitations on the server side.

SQL

A database is one of the most fundamental elements in a web application. In SQL database, you must always understand how the fundamental resources function properly. SQL Server has its own dynamic management views (DMVs) that are built into it. Most DVMs provide data about query stats, executing plans, and many more. The good thing about DVMs is that it is always available to provide basic rollup statistics, but it can’t visualize the queries that are being called over time.

Application performance management (APM) tools have the capability of tracking SQL queries. Retrace provide SQL tracking queries on multiple databases providers including SQL Server. Retrace also tells you how many times a query has been executed, and what transactions are calling it. The most relevant advantage of using Retrace is that it has detailed reporting across apps, per app, and query. It can show detailed transactions on how queries are being used in your app. This is the most important information when it comes to SQL performance tuning.

Content delivery network (CDN)

Content delivery network (CDN) is one of the most efficient and effective ways to speed-up the delivery of websites with high traffic and websites that have global reach. Many businesses already use CDN when handling large scale websites to increase geographic reach. This technique can reduce latency, reduce bandwidth consumption, and secure applications while it can also block spammers and other data scrappers that may attack your system.

Error handling

Error handling is one of the most important parts of any application development. PHP provides different techniques and strategies to handle errors. They permit you to establish your own error handling rules and modify the way errors can be logged. This allows you to develop and improve error reporting that best suit your needs.

Troubleshooting is a headache during and after development, and Stackify designed a solution to the problem. Retrace’s most powerful feature is code profiling, which can even track errors even if you aren’t logging. It finds hidden errors in your code, and it quickly gives you notification through email and SMS before all of your customers are affected. The monitoring of errors in Retrace is not only limited during development, but also in your production environment, and it will automatically update new errors and logs.


Stackify Loves Developers

Summary

There are many performance monitoring tools available in the market. Choose the best one to fit your needs in your application.

Stackify provides many packages that every developer, systems administrator, or even database administrator would always find useful. Using our Stackify products, you can easily monitor, log, debug data, and many more details that will find the root cause of web application issues and you will become more productive. You can try and check our free trial now.

More information

These articles provide additional information about performance tuning that might be useful for your PHP applications.

About Juliet Mendez

Juliet Mendez is a Professor in the Department of Computer and Information Sciences and a PhD student specializing in Information Technology. She has a background in CCNA, computer programmings such as PHP, JAVA, Mobile Development, and Project Management.
Improve Your Code with Retrace APM

Stackify’s APM tools are used by thousands of .NET, Java, and PHP developers all over the world.
Explore Retrace’s product features to learn more.