The Developer Day | Staying Curious

Archive for August 2007

Aug/07

21

PHP load testing tool

I needed to test load some of our applications. I’m currently using a windows machine as my workstation. For some weird reason I’ve decided to develop such a load testing tool myself. It was both interesting and fun.

I used php curl extension multi_ functions that are only available since PHP5. The script is very simple. It accepts an integer parameter with max clients and a string parameter where a URL list file resides. You can run the script either from the command line or a browser. The script outputs average client response time, average responses per minute, total amount of responses and time elapsed during test loading. After a client responds the tool requests another URL. You can only stop the tool by killing the process.

You can try my php test loading tool yourself. If you are looking for something more serious try apache ab under Linux or Cygwin.

, , , Hide

Aug/07

20

Profiling PHP applications

A while ago I was in need to profile one of our PHP applications. I needed to be able to see execution times, memory usage and some kind of a graphical interface to view it all. I remembered a PHP profiler debugger tool named Xdebug. Xdebug works as a PHP Zend extension. If you are a windows user installing it on your machine is very straightforward:

Download the appropriate windows module from xdebug.org. Edit your php.ini file by adding the following line to the extensions section:

zend_extension_ts=”path/xdebug.dll”

Restart your web server and look for a module named Xdebug in your phpinfo() output. If you’re lucky enough you’ll find it there.

After that you need to configure xdebug extension.

;enables the profiler
xdebug.profiler_enable=1;
;specifies profiler output files directory
xdebug.profiler_output_dir=”c:/php/xdebug/”
;specifies profiler output file name pattern.
xdebug.profiler_output_name=”cachegrind.%u.out”
;enables auto tracing on every script startup
xdebug.auto_trace=1
;specifies trace files output direcctory
xdebug.trace_output_dir=”c:/php/xdebug/”
;specifies trace output file name pattern.
xdebug.trace_output_name=”trace.%u”
;enable memory usage tracking
xdebug.show_mem_delta=1

Restart your web server again. Run any of your php applications and you should find files in your specified directories:

trace.1187359095_993350.xt
cachegrind.1187366421_815700.out

Now you can view cachegrind files with WinCacheGrind. The application I tested was spawning few processes of it’s own and the profile information was in one file by default. But apparently something didn’t want to work so I separated each process to a separate file by adding %u to the output file name pattern.

The most interesting part for me was memory usage which you can find in the .xt files. It shows the total memory used after every line of code executed and a delta’s between executions. A tool for viewing/analyzing these trace files would be nice. Did not look for one though. All in all Xdebug is useful to find those nasty bottlenecks. It also has other nice features that help you to daily develop your apps. Nice thing I didn’t try yet is the “Code coverage” test. It might help me find some unused code in our applications.

, , , Hide

Aug/07

17

PHP SoapClient certificate problems

I’ve been working with SoapClient and certificates for some time. I recently switched my old workstation to a new one and while working with a piece of code that seemed to work fine I got an error like this:

Unable to set local cert chain file `filename.pem’; Check that your cafile/capath settings include details of your certificate and its issuer in somescript.php on line ##

I tried to google what the problem might be and found a comment that you can get this error if you have curl extension disabled. But in my case it was enabled and working fine. I tried to remember what extensions I had enabled on my previous box and after a few thoughts I decided to enable mcrypt extension which seems related to crypted certificates and after restarting my webserver everything started working fine.

The php soapclient documentation does not mention anything that soapclient is dependency related to curl or mcrypt.

, , , , , Hide

Aug/07

6

JavaScript Associative Arrays & Iterations

Today I was working with a little piece of JavaScript code and writing something like this:

var tmp = new Array();
tmp['id']= '123';
tmp['name'] = 'bla bla';

And then i tried to iterate over the array like this:

for (var i in tmp) { alert(i); };

If you haven’t done a lot of JavaScript you would probably say that the above iteration should output ‘id’, ‘name’. Surprisingly you may be wrong. If you are using any code that extends the Array prototype with attributes or functions you will see them in the output. This may happen if you are using the prototype framework or The open source code of a JSON parser and JSON stringifier. All you’re doing is setting properties on an object. After reading a few blog posts and comments on this I found yet another issue that ideally I shouldn’t be using new Array() and using new Object(). That sounds kind of true because JavaScript does not support associative arrays and you could do the same on any object. Date, RegExp, Boolean or any other. Actually using Object solved the prototype framework problem, because prototype framework does not extend the Object prototype since 1.4 but the JSON stringifier does.

After googling a while I found out a little tricksy that may mostly work with this problem. You could do something like this:

for (var i in obj) {
if (!obj.propertyIsEnumerable(i)) continue;
foo(obj[i]);
}

This only works if properties are enumerable. That may not always save you especially if you are using any 3rd party software that breaks this, but it certainly helps you while solving the prototype framework or JSON stringifier puzzle.

, , , , Hide

Find it!

Theme Design by devolux.org