METAR cacheing / performance / code methods
Brought to you by:
iridium
From: Phil P. <phi...@sw...> - 2004-01-06 02:13:15
|
While I was trolling through the code, I often saw what looked like many and various chunks of code which essentially re-processed the RAW METAR data from scratch. (eg interpreting the timestamp). [ Maybe I'm not being clear here... it looks to me like different bits of PHPWeather that need (for example) the timestamp re-interpret it from the RAW data themselves, rather than a chunk of code munging that into an internal datastructure (ie once) which all the code chunks read as they need. Let me also just say that I've only done a little trolling around PHPWeather sources, and I'm barely able to 'crawl' when it comes to writing PHP. I'm not trying to criticize the code, merely wanting to better understand how it works (perhaps I might try my hand at contributing some tweaks/features in the not-too-distant future) ] As this makes much and heavy use of regexes, I was wondering if this could have a material impact on performance. So, I did some quick hacking around with my PHP scripts and came up with the following stats: - full site page, including process (ensure this has been cached) METAR for TEXT as well as evaluate for ICONS) - 0.089 seconds or thereabouts - same page, remove the code chunk/module for the METAR processing and output - 0.018 seconds ugh! even when it's cached, processing the METAR and generating the html-ified output makes the page generation take 3 times longer than previously. (of course, I'm using MySQL as the datastore for METARs, so this delay could be mostly due to MySQL query queueing and processing delays) So I hacked around, and wrote some additions to my scripts to cache my formatted HTML output into a file. Now (as long as the cached results are within the timeout) showing the processed weather data takes negible additional time over normal page generation. That's nice as a workaround. But obviously it only helps me and nobody else (well, not much). Here's the code-fragment I used to 'cache the output HTML' if anyone else wants to use it. /* LockFile must exist, cacheFile must be writeable by the webserver */ if((time() - filectime($cacheFile)) < $metarCacheTimeout) { readfile($cacheFile); } else { /* here we regenerate the formatted METAR results */ $output = {munged html-ified output you want on your site}; /* grab a lock on LOCKFILE, rewrite cachefile, release lock */ $fp = fopen($cacheFile . '-LOCK', 'r'); if( flock($fp, LOCK_EX + LOCK_NB) ) { $fp2 = fopen($cacheFile, 'w'); if($fp2) { fwrite($fp2, $output); fflush($fp2); fclose($fp2); } flock ($fp, LOCK_UN); } echo $output; } At some point I'll have a closer look and see if I can find where the bulk of the time is going, but for now I just thought I'd throw my findings out there and listen to any comments/feedback others might have. Thanks, Phil P |