Difference in performance among config files for P

2019-08-01 03:30发布

问题:

I'm building a site whose configuration data is held by a php file, with no more than 15 lines of variables.

Everything has worked well so far, but now I'm facing the need of defining more variables, and researching a little I found an article about Application Configuration in PHP, whose conclusions state that php files are the wost option when analyzing how they scale (Databases being the better choice).

What is the most recommended choice when deciding which format of configuration file to use?

回答1:

I'm sorry, what?!

php files are the wost option when analyzing how they scale (Databases being the better choice).

This is just weird, because you can scale php very well - just set up another server and that's it, you've scaled your php twice.

Also keep in mind that there are opcode optimizers that will keep the parsed file contents in the memory.

Whereas scaling databases is a rocket science. Scaling databases is a tricky process, that involves good database internals and your OS knowledge.

So I wouldn't refer to that article ever.

PS: looked at the article:

When the script is run, the results are in. Averaging around 0.052640 mark, making this by far the slowest method so far.

I cannot realize how just instantiating variables can be slower than reading from database (which includes connection overhead, reading from tables overhead, transmitting the data overhead and the same instantiating variables process in the end). X just cannot be more than X + Y by definition.



回答2:

In my opinion, if you can store the configuration in the database and cache the results in memory, performance should be very very good.

However, if the configuration is stored in the database and it takes a lot of joins to get it, with no caching, performance may be worst than using PHP files.

On the other hand, if you store your configuration in a PHP file that returns an array:

//config.php
return array(
   'configitem1' => 'xyz',
)

//then use
$config = require_once('config.php);

the performance should not be too bad. Obviously there would be disk IO for each request, so you might see some performance issues there, if you are requiring lots of files. You can always cache the results into memcached or APC to get rid of Disk IO performance issues if requring loads of files.

Finally, you can use XML or YAML files. These would be the least performant as you will need to load and read the file, and then parse it.

Personally, my opinion would be to store configuration in the database where the user of the application may need to modify it. For example:

  • Custom URLs for pages.
  • Application settings such as whether users can create new accounts
  • Flags such as whether the app is in maintanence mode or not.

There will always be configuration you need to store in files:

  • Database connection parameters
  • Cache settings (i.e. memcached settings).

However, the configuration stored in files can be cached in memcached or APC, so your application should still be performant.

So my 2 cents:

  • Use database to store the configuration if the user NEEDS to be able to modify that.
  • Store other configuration in a PHP file as an array and use caching.
  • Use YAML and XML as your last resort.


回答3:

Here is why the article is wrong:

Type    First Run   Second Run
Ini File    0.000421    0.021381
XML File    0.000717    0.012310
PHP File    0.000450    0.052640
Database    0.005533    0.014184

Saying "first run" and "second run" clearly indicates that these aren't averaged results. The author just ran it twice, without an actual profiler, and published a meaningless number as result.