Simple PHP Timer

A PHP function to keep track of, and trace, execution timing in a PHP program.

I was just trying to profile an HTML page for performance bottle-necks. I’m trying to follow a top-down approach, wherein I start from the entry script, and find the block of code that takes the biggest chunk of time before digging deeper into that chunk.

At this stage, it’s not feasible to drop in a full-blown profiling tool like xdebug because of the set-up overhead and amount of data it generates. So, I wrote a simple timing function that you can call at various points in your program to provide incremental and cumulative timing info in milliseconds. Continue reading “Simple PHP Timer”

PHP vs. Python for Web Apps

These days, more and more people ask the Python vs. PHP question when they start out with a web application from scratch. I’ve developed PHP applications for 5 years but for the last couple of years I’ve been doing Python. This post is meant to note some of my observations. If you don’t want to read the whole of it, my opinion — opinion — is to stick with PHP for dishing out your *ML. Use Python in the back-end, if you must. Continue reading “PHP vs. Python for Web Apps”

MySQLdb Leaks Memory

MySQLdb 1.2.2 leaks memory on database errors. Upgrade to 1.2.3

Whenever people search for a Python library for MySQL, they get directed to MySQL for Python. However, there are some nasties hidden in it. Searching for “mysql python memory leaks” results in a few links which suggest that using Unicode causes memory leaks with the library.

Today, however, I found another cause for MySQLdb memory leaks, while debugging a leaky Python daemon at work — database errors.

Use this script:

import MySQLdb
options =   {
   "user": "user",
   "passwd": "p455",
   "db": "somedb",
   "connect_timeout": 1

while True:
    except MySQLdb.Error:

Make sure MySQL is up and it’s possible to connect to it without failures using the above credentials. Then run this script and watch its memory usage. It’ll be rock steady for as long as you care to run it.

Now stop mysqld or do anything that would cause errors during connection and rerun the script. Watch it gobble up meg after meg as the errors continue. Any MySQL error, like unreachable server, missing database or tables, malformed query, etc. would trigger the leak.

I need to check if the recently released (13 days ago) MySQLdb 1.2.3 does better in this regard. MySQLdb 1.2.3 does fix the memory leak in this case.