How I Am Choosing Sections Of A Website To Optimize

I have an e-commerce system that is a complete PHP spaghetti mess.

I am running way Way WAY to many database queries. I want to share how I am tackling this mess.

I am using the mysql general log to store the queries that are being run to both a table and a file. At first I was using this to see what was run most often, and started caching those in memcache.

Today I decided, why don’t I find out what pages are causing the most queries, and optimize those, instead of individual queries. For example, my first test showed my product page took 110 queries.

1) I am executing this on every page when I initialize the DB.
2) I turn on the general log for a few minutes. set global general_log=1; wait…. set global general_log=0;
3) I am using this query to find out what threads are running the most queries.
SELECT general_log., count(*) as c from general_log where general_log.event_time like ‘2012-03-02%’ and argument like ‘SELECT%’ group by q order by c DESC limit 10
4) I can then use the thread_id to find out what it was doing. The @page from the gist above will be one of the first queries, so I know where to start analyzing.

I hope this helps someone else.


Rackspace Cloud Servers to the Rescue

Rackspace’s cloud server architecture has a fantastic API. I recently found a need for an automated script to have access to a server for less than an hour a day.

Within this script, I am easily able to see if there is a server ready for it already, if not, it creates a server out of an image that I have already created for this purpose, waits for the server to become active, and then it does its work.

I have a separate script that check periodically for servers running past their expired time and deletes them so that we don’t have to keep paying for them.

This will take a server from about $10/month to less than $0.50 / month.


Pre-Commit Fails When Removing a File From Git

I just blogged about automatically running PHP Lint when committing a file into the repo. Well I also had the joy of it failing because I removed a file.

If you get “Could not open input file: abc.php”, then you may have the same problem I did.

There’s an easy solution. The pre-commit file looks for all changed files. Technically, a removed file is changed, but it also doesn’t exist when you go to run php -l against it.

Open your pre-commit file, and change the line that looks like this:

exec(“git diff-index –cached –name-only {$against}”, $output);


exec(“git diff-index –diff-filter=ACMRTUXB –cached –name-only {$against}”, $output);


Run PHP Lint before committing to GIT

This is going to sound stupid, but if you don’t have rock solid unit tests in place, one of the things you should be doing is running PHP Lint.

What is PHP Lint?
Simply stated, it checks your php file for syntax errors. How many times have you forgotten a semi-colon, or had unmatched curly braces?

How do I run PHP Lint?
It is very easy to run from the command line
php -l your_php_file.php

Using GIT? You should setup a pre-commit hook to always do this so you don’t push something bad into the repo. I am guilty of doing this in the past. 🙁

Here is a great post by Travis Swicegood about setting this up.

One thing to remember is that you also have to make the script executable.
chmod 744 .git/hooks/pre-commit


phpUnderControl javascript fix for Internet Explorer

There are two errors in the phpUnderControl javascript that causes IE to work like the other browsers.

Basically, you may not be aware that the “home screen” auto updates in other browsers. I kept refreshing like a madman until I opened it in another browser and saw it happen automatically. Then I dug in and fixed it.

Here is the patch:
@@ -16,7 +16,7 @@
if(d != null) {
el.setStyle( {
‘height’: d.getAttribute(‘height’) + ‘px’,
– ‘width’: d.getAttribute(‘width’) + ‘px’,
+ ‘width’: d.getAttribute(‘width’) + ‘px’
} );
delete d;
@@ -26,7 +26,7 @@
var d = el.contentWindow.document.body.scrollHeight;
if(d != null && d > 20) {
el.setStyle( {
– ‘height’: (d + 50) + ‘px’,
+ ‘height’: (d + 50) + ‘px’
} );
else {


jQuery Mobile Select Box Not Showing Up

jQuery Mobile is a great framework with lots of great out-of-the-box magic.

I am in the middle of converting a site over from a full website to a mobile version. I was really frustrated when I tried to click on a select box and nothing would happen. If my header/footer were fixed, they would just toggle on and off. I assumed it was the fixed positioning, and I was wrong.

Through some FireBug debugging, I found the problem. It probably won’t affect many people, but it’s worth pointing out. I had a set a width through CSS on the select item for the desktop version. When I ported into the mobile version, I left the CSS width in there. So if I happened to click on the very far left of the button, it worked.

I removed the width and all is right in the world again. 🙂


Running Apache Benchmark (ab) and receiving apr_socket_recv: Connection reset by peer (54)

I was having this issue and was getting quite frustrated. All of my researched turned up nothing. Finally I decided to run this from another machine instead of my Mac, and it worked just fine. So this had nothing to do with my server, it was a client side related issue.

I still don’t know what caused it. I was just able to identify the issue and thus find a work around.


Google +1 Button Shows Zero (0) Count When Not Logged Into Google Account

I had a situation that my boss pointed out yesterday that was very confusing.

He was on a competitor’s website and their +1 button was showing a count. However, the same product on our website was showing 0. I knew that I had +1’d it.

So I go to my machine and I see (3) +1’s. I logout of Google and sure enough the 3 went away. ?!?!?!?

I was so confused that I went as far as copying the +1 code from the competitor’s site and theirs showed a number and ours still did not until I logged in. My mind was boggled.

Next step: ask another person in the office to create a Google Profile and +1 our product. Bringing our +1’s up to 4. There you have it, it showed whether I was logged in or out of Google.

I hope this save someone else time from searching hours for an answer. It was ridiculously confusing. Why is 4 the magic number?


I am coining a new term “Arsonistic Firefighter”

I find myself putting out fires in my code base all the time. The problem is, I set the fires to begin with. 🙁


Lazy Loading Images with JQuery

I am trying to speed up a website every way that I can. I decided to look into lazy loading which I have read about numerous times.

Lazy loading is basically not wasting time loading images that are off the screen, or out of the viewport.

This got me thinking, why do the browsers not do this for us? They would be better equipped to handle this than a JS developer. Especially since the browser is doing the rendering, it knows what it needs in order to display the screen.