Speed issues with a central xml file storing site settings

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Nunyah
Forum Newbie
Posts: 7
Joined: Sat Jul 01, 2006 7:08 pm

Speed issues with a central xml file storing site settings

Post by Nunyah »

Hey all

I'm currently working on a CMS project and I've begun considering to have a config.xml file to store various settings for every page as well as global settings. A very simplified example:

Code: Select all

<webconfig>
    <global>
        <database>
            <name>MyDatabaseName</name>
            <user>root</user>
            <password>Password1</password>
        </database>
...
    </global>
    <newspage>
        <db_table>news</db_table>
        <newsperpage>20</newsperpage>
    </newspage>
...
</webconfig>
To me, this seems like a lot of flexibility. If I wanted to change something, it would need to be edit one place and voila.. I'm curious if this would make a significant performance hit compared to having the settings stored either in a) the scriptfile or b) with the rest of my functions in the function library. The reason I'd like to have all the settings stored in an xml file is because I can seperate all my different settings from the rest of my codefunctions which is stored in my library folder. Then linking them up in a controlpanel at the backend of the site for ease of use and changes.

I'm worried that if every page needed to consult the xml file for variable settings, for every user loading a function or database query, it would cause a lot of waiting on the serverside, slowing things down.. Or is this just noticable at 100(0) user peak +?
The site will at least start off relativly small and I guess caching would solve much of this if it would become much of a problem..

What do you guys think?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Speed issues with a central xml file storing site settin

Post by timvw »

Nunyah wrote: To me, this seems like a lot of flexibility. If I wanted to change something, it would need to be edit one place and voila..
With this statement i can agree.
Nunyah wrote: I'm curious if this would make a significant performance hit compared to having the settings stored either in a) the scriptfile or b) with the rest of my functions in the function library. The reason I'd like to have all the settings stored in an xml file is because I can seperate all my different settings from the rest of my codefunctions which is stored in my library folder. Then linking them up in a controlpanel at the backend of the site for ease of use and changes.
Any sort of configuration file format can do this... The case against XML is that the parsing of the file can lead to a performance hit... (Anyway, i would only take this into consideration if it really becomes an issue)
Nunyah wrote: I'm worried that if every page needed to consult the xml file for variable settings, for every user loading a function or database query, it would cause a lot of waiting on the serverside, slowing things down.. Or is this just noticable at 100(0) user peak +?
Any decent operating system (and it's filesystem) will store files that are often requested in it's cache... So i'm expecting that the only bottleneck would be the parsing of the file...
Nunyah
Forum Newbie
Posts: 7
Joined: Sat Jul 01, 2006 7:08 pm

Post by Nunyah »

Thank you for clear answers, timvw. My concerns for performance hits using xml at my level are now void. :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Nunyah wrote:Thank you for clear answers, timvw. My concerns for performance hits using xml at my level are now void. :)
No, they really aren't. Parsing an XML file takes a lot of resources and a lot of time. Storing config info in a PHP file as constants is a lot faster.

If you want it to be easy to edit, store as XML.

If you want it to be fast, store as PHP.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You should take a look at the Zend_Config proposal in the Zend Framework. It accepts XML files or INI files or just arrays and I am sure that more will be added.
(#10850)
Nunyah
Forum Newbie
Posts: 7
Joined: Sat Jul 01, 2006 7:08 pm

Post by Nunyah »

onion2k wrote:No, they really aren't. Parsing an XML file takes a lot of resources and a lot of time. Storing config info in a PHP file as constants is a lot faster.

If you want it to be easy to edit, store as XML.

If you want it to be fast, store as PHP.
A lot of time and resources in what measure? The page would be relativly small, 70% of the xml lookup would be when using admin functions. I really doub there would be more than 5 people online at the same time.. Would you notice any performance hits with such a site?

I'm building a CMS system which I might use as groundwork for future client projects (besides from learning and developing my own skills), thus having a flexible solution is more welcome than being able to withstand large quantity of peak traffic.

arborint:
Looks interesting, I'll look into it!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Nunyah wrote:A lot of time and resources in what measure? The page would be relativly small, 70% of the xml lookup would be when using admin functions. I really doub there would be more than 5 people online at the same time.. Would you notice any performance hits with such a site?

I'm building a CMS system which I might use as groundwork for future client projects (besides from learning and developing my own skills), thus having a flexible solution is more welcome than being able to withstand large quantity of peak traffic.

arborint:
Looks interesting, I'll look into it!
CPU time and memory resources .. a PHP based config file would take practically no time to parse .. 0.001 seconds maybe. An XML file would take maybe 0.02 seconds. 20 times slower. On a site with 5 concurrent users that's nothing to worry about .. but on a site with 5000 concurrent users it's a much bigger concern.

If you're starting from scratch, why not write something that loads an XML file, caches it as a serialized PHP array, and loads the cached version for future pages instead. Config info rarely changes after all.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

If you're starting from scratch, why not write something that loads an XML file, caches it as a serialized PHP array
Btw, what is faster: unserialize(file_get_contents($file)); or include($file)?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

In my testing:

Code: Select all

<?php
function microtime_float() {
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
$start = microtime_float();
for($i = 0; $i<10000; $i++) {
    $conf = unserialize(file_get_contents('con.ser'));
}
$end = microtime_float();
echo "Unserialize: " . ($end - $start) . " seconds\n";

$start = microtime_float();
for($i = 0; $i<10000; $i++) {
    include('con.php');
}
$end = microtime_float();
echo "Include: " . ($end - $start) . " seconds\n";

?>
unserialize seems to perform better:

Code: Select all

:!php con_test.php
Unserialize: 0.78778219223022 seconds
Include: 1.0780591964722 seconds
:!php -v
PHP 4.4.0 (cli) (built: Aug 18 2005 16:04:27)
Nunyah
Forum Newbie
Posts: 7
Joined: Sat Jul 01, 2006 7:08 pm

Post by Nunyah »

onion2k wrote: CPU time and memory resources .. a PHP based config file would take practically no time to parse .. 0.001 seconds maybe. An XML file would take maybe 0.02 seconds. 20 times slower. On a site with 5 concurrent users that's nothing to worry about .. but on a site with 5000 concurrent users it's a much bigger concern.

If you're starting from scratch, why not write something that loads an XML file, caches it as a serialized PHP array, and loads the cached version for future pages instead. Config info rarely changes after all.
Absoultly, that makes sense. If I understand you correctly, I should write some cache function that x times a day updates it's cache data with the xml file so the regular php scripts can look for it in an array instead of opening the xml file, wasting time and resources parsing it..
Now don't beat me with a stick for saying this, but how would I go about in order to store the data in the array? AFAIK, memory in php is released when scripts run out of scope. Wouldn't that clean up the array as well?

Edit:
I guess I could run the "update cache" function each time there is made a change in the configurationfile.. so ignore the x times a day statement
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Nunyah wrote:Now don't beat me with a stick for saying this, but how would I go about in order to store the data in the array? AFAIK, memory in php is released when scripts run out of scope. Wouldn't that clean up the array as well?
Your "update cache" function would load the XML file, parse it into a data structure that can be serialised (eg an array), serialise it, and save it to a cache file. Each time your script runs it does "unserialise(file_get_contents("config.file"))" (as Weirdan demonstrated) reinvoking the array. serialise()/unserialise() is basically a way of saving PHP variables.
Nunyah
Forum Newbie
Posts: 7
Joined: Sat Jul 01, 2006 7:08 pm

Post by Nunyah »

Ah, I get it now.. That's a pretty nice feature. Thanks for explaining this to me. I doub I'll see any noteworthy effect using this as of now but I think it's a really nice habit to have - One day I really might gonna need it or expand upon it and it really dosn't add much more of a hassle or complexity to my code.
Post Reply