Archive for March, 2006

Using PHP to serve files

Wednesday, March 15th, 2006

Sometimes you need PHP to passthru files instead of using a webserver like you should. Sometimes you need to check permissions; or do other stuff which requires you to make PHP behave like a webserver. So, what is the fastest way to passthru a simple file using no logic? I have been working of several ways of benchmarking this. First; I used Apache Bench to run a PHP file a few hundred times; but the results were too unstable. Sometimes I got 2 requests per second, sometimes I got 30 requests per second using the same settings. So I started looking for a different way of benchmarking. I found a way which was pretty stable. However; the results weren’t really interesting either. I ran all commands 100 times for different file sizes; the only noticeable change was readfile becoming slower as the file size increased (from 2.3 to 3.2 for a 6 Mb file). All other functions (fgets, file_get_contents, fpassthru, fread, readfile) remained constantly at 2.3 seconds. Conclusion is it doesn’t really matter what function you use; the speed difference is negligible.

When exportfs -arv takes forever

Saturday, March 11th, 2006

I had the strangest problem with my server for the last months. Starting nfsd on my Archlinux machine took forever and I couldn’t figure out why. Stracing `exportfs -arv` showed a lot of DNS queries were done; which is strange since my nfs server is only available to machines on the internal network (and those are all listed in /etc/hosts as well). I quickly found out that removing the nameservers from /etc/resolv.conf would somehow fix this problem; nfsd would start instantly. A lot of strange IP addresses showed up while tracing, but I couldn’t quickly find out where they came from. When I was ready to modify my nfsd startup script to empty the resolv.conf before starting; I started a last serie of Google queries. And guess what. I found it.

Searching for ‘exporting “to kernel” exportfs’ (the message exportfs gave while running, and which contained the strange IP addresses) pointed me to [nSLUG] Re: Help with NFS, plz?. This thread mentioned a file `/var/lib/nfs/rmtab`; which, when I viewed it, actually contained those strange IP addresses. Somehow nfsd had stored all the hosts that made nfs-requests; including many old and dead ones. It seemed a reverse DNS lookup was done for each IP address in there; resulting in a lot of lookups that were never satisfied. The problem was solved by emptying the rmtab file.

Using JSON to create your DOM elements

Saturday, March 4th, 2006

While working on a webapplication using AJAX technology I found myself reading XML and writing that to the DOM; but doing that is really frustrating. Check out this hideous piece of code I needed to create a simple element:

var dom_item_date, dom_item_title_text, dom_item_title_link;
var dom_item_content_text, dom_item_title, dom_feed_item;
dom_item_date = document.createElement('dt');
dom_item_date.appendChild(document.createTextNode(item.getFirstValue('pubDate')));

dom_item_title_text = document.createTextNode(item.getFirstValue('title'));

dom_item_title_link = document.createElement('a');
dom_item_title_link.href = item.getFirstValue('link');
dom_item_title_link.appendChild(dom_item_title_text);

dom_item_content_text = document.createTextNode(item.getFirstValue('description'));

dom_item_content = document.createElement('p');
dom_item_content.appendChild(dom_item_content_text);

dom_item_title = document.createElement('dd');
dom_item_title.appendChild(dom_item_title_link);

dom_feed_item.appendChild(dom_item_date);
dom_feed_item.appendChild(dom_item_title);
dom_feed_item.appendChild(dom_item_content);

So I wanted a different setup. Reading about JSON I assumed there was an easy way to put JSON object in the DOM. The only one I found is this script. However; the example still seems a bit too long, with redundunt stuff in it. So I decided to create an alternative. Check this out:

var feed_item = {
	dt : {
		_text : item.getFirstValue('pubDate')
	},
	dd : {
		a : {
			href : item.getFirstValue('link'),
			_text : item.getFirstValue('title')
		}
	},
	p : {
		_text : item.getFirstValue('description')
	}
}

var dom_feed_item = Object2DOM(feed_item);

Can you believe that this easy-readable script does the exact same thing that the first script did? This is really going to add joy to my Javascript-DOM coding! You can download the Object2DOM script here. Everything should be quite intuive; the only thing special is that you need to name your attribute ‘_text’ if you want a textnode to be created. Also; since the Object specification requires you to have a top-level element to hold all the sub-items; I am currently converting this top-level element to a <span>, but this can be changed easily inside the script.