Optimizing Performance of Legacy PHP, MySQL Application

When your webpage load time increases randomly & drastically, there is always a need to benchmark the performance for high scalable websites. The challenge i faced was, of server page generation time, or your can say time to first byte. My application was using servers like Memcache, Sphinx, Mysql and Apache. Now i went to dig down the issues and thanks to Syed Alam & Muhammad Wasif, i discovered a really useful server app, NewRelic. This application have wonderful features but definitely it has some cost too, but i think it worth if you can manage it.

So, for those who want to run their free manual solutions, immediate solution would be to check MYSQL query timings. If you are using some database wrapper function or class, it would be very easy to just track time before and after mysql_query() function (using timer library). It will tell the insights where you are actually losing.

But if are working on tuning legacy code base, in which there us no mysql_query wrapper used, it a big cost to wrap it or apply benchmarks all over the place. This was the challenge i faced recently while working on a project. I searched web for some options and found that there are 2 libraries available, that can override core PHP functions with your own implementations.

Runkit’s runkit_function_redefine()
APD’s overide_function()

My experience with both of them was not really very good, as although they override mysql_query() function with my custom function, but gave segmentation fault and blank page randomly. Definitely not suitable for production. But if you can diagnose issues on local environment, you can take this risk for dev machine.

Solution:

Finally, thanks to Zeeshan Shah, i found another Clone of Runkit, that actually fixed those segmentation fault issues, and much stable than the PHP.net PECL version.

It really gave insights of how many queries are on my page and not performing well on local environment. I won’t recommend to use it on live as it may also have some glitches but one can try on dev machine. This can be integrated with PHP Quick Profiler for more presentable results.

Following is the sample code to play with …

<?php

// Custom implementation of mysql_query
function custom_mysql_query($query, $link = null)
{
// put your timer benchmarking custom code

core_mysql_query($query, $link);
}

// Make a copy of actual mysql_query function
runkit_function_copy(‘mysql_query’,’core_mysql_query’);

// Now redefine base function with custom_mysql_query()

runkit_function_redefine(‘mysql_query’,’$query, $link_identifier = null’,’return custom_mysql_query($query, $link_identifier);’);

Share your thoughts & feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: