Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 03-22-10, 03:51 AM   #1
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default LAMP Templating/CMS Performance

I recently started working on a new CMS system, that will hopefully grow into something robust enough that can be reusable in a production environment. One of the things I was concerned with was the performance using various methods. I wanted to make sure I could implement something that has a lot of features, but not a sacrifice in performance. I also wanted to minimize server load as much as possible.

Although the tests I ran were pretty elementary, it at least gave me a decent idea of how each method performed. I wasn't too surprised at the results, but the processing time of each method did have a larger margin than expected.

I had a discussion about this a few months ago on the devshed forums (which is a very good resource IMO).

link: http://forums.devshed.com/php-develo...ns-665199.html

Here is a breakdown of the methods discussed:

Method 1: Base template is your typical generic HTML page. The page contains specific areas where dynamic content is output.
1. The page data is loaded using fopen() or file_get_contents()
2. the dynamic content is generated coming from MySQL.
3. The content is injected using a simple find a replace type method using str_replace().
ex:
Code:
Before: {{BODYMAIN}}
After: <p>the quick brown fox jumps over the lazy dog</p> {{BODYMAIN}}
The process is reiterated until all dynamic bits are processed then injected into the template.
4. The modified document is output, page displays in users browser.


Method 2: Base template is a PHP page. The page contains a series of echo statements where the already processed dynamic content is output.
ex:
PHP Code:
//The PHP page
<?php
$pgTitle
= xContent::getTitle('main');
$myBlog = xContent::getContentBasic('homeNews', 'xnTwoArticle', 'id', 'ASC', '0, 10');
?>


// The HTML Page
<html>
<head>
<title><?php echo $pgTitle; ?></title>
<script type="text/javascript" src="./lib/js/myScript.js"></script>
<link rel="stylesheet" type="text/css" href="./tpl/myTpl/style.css" />
</head>
<body class="mytpl">
<table width="60%">
  <tr>
    <th>My Blog</th>
  </tr>
  <tr>
    <td><?php echo $myBlog; ?></td>
  </tr>
</table>
</body>
</head>
Method 3 (not tested): There is no template. All the static and dynamic content is contained in the database.

Here is a general flowchart of how each step method is done.




Luckily, I already have a codebase that is geared toward Method 1, so all I had to do is code something like Method 2.

So I did and ran some benchmarks today. Each page contained the same exact dynamic content and the templates themselves are the same, just processed differently.

System Testbed:
OS: Debian Squeeze 6.0
Kernel: 2.6.32 686
Apache: 2.2.14 with Suhosin Patch 0.9.8
PHP: 5.3.1-5 (using mysqli extension)
MySQL: 5.1.44

CPU: AMD Athlon 64 4000+ San Diego
RAM: 1GB DDR400 3-3-3-8 2T (for stability)
Chipset: nForce4 SLI x16

CPU:


Method 1:
Code:
Fastest: 0.0042ms
Average: 0.00445ms
Slowest: 0.0049ms
Method 2:
Code:
Fastest: 0.0021ms
Average: 0.0023ms
Slowest: 0.0027ms
Well, there you have it!
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...

Last edited by t3hl33td4rg0n; 04-07-10 at 05:23 AM.
t3hl33td4rg0n is offline   Reply With Quote
Old 03-22-10, 10:15 AM   #2
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Re: LAMP Templating/CMS Performance

Nice little demonstration. With PHP there's only so much you can do to speed it up though. In the end, the page still has to be compiled and displayed when it's loaded. If you want to minimize server load, then it may be best to implement something like AJAX, where database calls are only made when necessary.

A few questions:

Why do you use str_replace to fill in your fields? PHP can be initiated anywhere in the page. Wherever you need the data, put in a <?php tag and echo the data, then close it ?>. That's one reason why Method II is much better. The overhead involved in searching a document for a string and then replacing it is insane.

I always use HTML templates in my pages. Typically the header, footer, and navigation sections are always included in simple HTML templates that I include into the main page. This makes updating your site very easy, since editing 6-7 pages boils down to editing 4 short files (CSS is stored in a separate page as well).

I only use file_get_contents() when I load in HTML from a separate page to parse. If you are including a functions file, always use require, so if the page is not found an error will be thrown. Could also prevent some of your page's code from being displayed.
ViN86 is offline   Reply With Quote
Old 03-22-10, 10:31 PM   #3
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: LAMP Templating/CMS Performance

Quote:
Originally Posted by ViN86
With PHP there's only so much you can do to speed it up though.
Script compiling and code caching can speed up your applications significantly. Its not unlike databases using indexing to speed up query execution. Of course, measures like this would only be required for robust applications and where server load could become an issue.

Quote:
Originally Posted by ViN86
If you want to minimize server load, then it may be best to implement something like AJAX, where database calls are only made when necessary.
Yeah, I've considered that, and I am still considering the option. I may go both ways where the template could either grab the main dynamic data via php echo statements or retrieved using AJAX. I must still keep the mindset that I have to keep template interaction as easy as possible without sacrificing performance and feature-set.

Unfortunately, completely relying on AJAX to get data has its own issues (at least it used to). I would love to create a robust application in javascript that could allow one to freeform design a site and still harness the power of dynamic applications.

I may even integrate Dreamweaver support in some fashion by way of extensions, where a user could create custom queries using a GUI and have the usability of Dreamweavers WYSIWYG. Yes, it will rock!

Quote:
Originally Posted by ViN86
Why do you use str_replace to fill in your fields? PHP can be initiated anywhere in the page. Wherever you need the data, put in a <?php tag and echo the data, then close it ?>. That's one reason why Method II is much better. The overhead involved in searching a document for a string and then replacing it is insane.
The main reason is that anyone with HTML knowledge could create their own template/theme. just place special tags where you want the stuff to go and you're good. It was an ease-of-use feature. But that guy gave me an idea about how I could implement the other method. I never really thought about the other method (2) until i had the discussion over at devshed, thats why I wanted to have the trials and see how it stacked up.

Quote:
Originally Posted by ViN86
I always use HTML templates in my pages. Typically the header, footer, and navigation sections are always included in simple HTML templates that I include into the main page. This makes updating your site very easy, since editing 6-7 pages boils down to editing 4 short files (CSS is stored in a separate page as well).
So I am assuming you are including the dynamic data "in-between" those files? Like....

PHP Code:
$headerFile = file_get_contents('./tpl/head.html');
do
some stuff;
echo
$headerFile;
$mainTpl = file_get_contents('./tpl/main.html');
do
some stuff;
echo
$mainTpl;
Quote:
Originally Posted by ViN86
I only use file_get_contents() when I load in HTML from a separate page to parse. If you are including a functions file, always use require, so if the page is not found an error will be thrown. Could also prevent some of your page's code from being displayed.
I agree. 99.999% of the time i use require() or require_once(), I rarely use include. I go by the method, include = optional, require = required

The reason for using require() instead of file_get_contents() is because the template also has the scripts and they get executed on-the-fly. Also, its faster.

Quote:
Originally Posted by ViN86
Nice little demonstration.
Thanks!
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...

Last edited by t3hl33td4rg0n; 03-22-10 at 10:33 PM. Reason: Whoopsie
t3hl33td4rg0n is offline   Reply With Quote
Old 03-23-10, 02:08 AM   #4
ViN86
 
Join Date: Mar 2004
Posts: 15,486
Default Re: LAMP Templating/CMS Performance

Quote:
Originally Posted by t3hl33td4rg0n View Post
Yeah, I've considered that, and I am still considering the option. I may go both ways where the template could either grab the main dynamic data via php echo statements or retrieved using AJAX. I must still keep the mindset that I have to keep template interaction as easy as possible without sacrificing performance and feature-set.

Unfortunately, completely relying on AJAX to get data has its own issues (at least it used to). I would love to create a robust application in javascript that could allow one to freeform design a site and still harness the power of dynamic applications.

I may even integrate Dreamweaver support in some fashion by way of extensions, where a user could create custom queries using a GUI and have the usability of Dreamweavers WYSIWYG. Yes, it will rock!
Yea there are some limitations to AJAX. Mostly that older browsers don't really support it and different browsers handle things differently.

Quote:
The main reason is that anyone with HTML knowledge could create their own template/theme. just place special tags where you want the stuff to go and you're good. It was an ease-of-use feature. But that guy gave me an idea about how I could implement the other method. I never really thought about the other method (2) until i had the discussion over at devshed, thats why I wanted to have the trials and see how it stacked up.
Yea, I learned method 2 from a PHP book. Once I realized that you can pretty much insert PHP tags and write some code wherever you want, it only seemed natural to just insert data on the fly wherever instead of searching for strings to replace.

Quote:
So I am assuming you are including the dynamic data "in-between" those files? Like....

PHP Code:
$headerFile = file_get_contents('./tpl/head.html');
do
some stuff;
echo
$headerFile;
$mainTpl = file_get_contents('./tpl/main.html');
do
some stuff;
echo
$mainTpl;
Actually it's even simpler than that. I just write w/e PHP and HTML I need into a page called header.php or something, then I just put in include("header.php"); where I want the header to display and it just echoes the text. You have to be careful, though. I typically store my files in a template folder. But when you use include, it treats the inserted code as if it were coming from the home directory, not where it actually is. So if you're not careful you can mess up links this way.

Quote:
Thanks!
NP
ViN86 is offline   Reply With Quote
Old 03-23-10, 02:41 AM   #5
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: LAMP Templating/CMS Performance

Quote:
Originally Posted by ViN86 View Post
Yea there are some limitations to AJAX. Mostly that older browsers don't really support it and different browsers handle things differently.
As long as its supported in FF2, IE6 then you should be ok. From there it depends on the data output method you use. Doing straight preprocessed HTML shouldn't ever be a problem, however if the script is doing other stuff, you could run into issues.


Quote:
Originally Posted by ViN86
Yea, I learned method 2 from a PHP book. Once I realized that you can pretty much insert PHP tags and write some code wherever you want, it only seemed natural to just insert data on the fly wherever instead of searching for strings to replace.
Some do argue against this point in that they should always be separate. IMO, it depends on context and usage.


Quote:
Originally Posted by ViN86
Actually it's even simpler than that. I just write w/e PHP and HTML I need into a page called header.php or something, then I just put in include("header.php"); where I want the header to display and it just echoes the text. You have to be careful, though. I typically store my files in a template folder. But when you use include, it treats the inserted code as if it were coming from the home directory, not where it actually is. So if you're not careful you can mess up links this way.
OOohhhh. When you said HTML, I didnt know you meant that they were PHP files with PHP stuff in 'em :P. I usually use a constant to get around this, so that any relative paths are updated, regardless of where you put the file, as long as you update the relative path locale base script.

In method 1, I did this...
Code:
table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td width="169" align="left" valign="top" nowrap="nowrap"><img src="{{PATH}}img/head_top_01.jpg" width="169" height="61" alt="Logo"></td>
		<td align="left" valign="top" nowrap="nowrap"><img src="{{PATH}}img/head_top_02.jpg" ></td>
	</tr>
	<tr>
		<td colspan="2" class="headNav" nowrap="nowrap"><img src="{{PATH}}img/head_nav_01_home_n.jpg" width="68" height="28" alt="Home" /><img src="{{PATH}}img/head_nav_spacer.jpg" width="17" height="28"><img src="{{PATH}}img/head_nav_02_services_n.jpg" width="110" height="28" alt="Services"></td>
	</tr>
</table>
Of course this will be changed to outfit the new method I plan on using. This actually would be a good place to use AJAX to minimize the amount of PHP statements in the template. Now that I think about it, I could make this part of the object constructor.
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...
t3hl33td4rg0n is offline   Reply With Quote
Old 03-23-10, 02:49 AM   #6
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: LAMP Templating/CMS Performance

Quote:
Originally Posted by ViN86 View Post
Yea there are some limitations to AJAX. Mostly that older browsers don't really support it and different browsers handle things differently.
As long as its supported in FF2, IE6 then you should be ok. From there it depends on the data output method you use. Doing straight preprocessed HTML shouldn't ever be a problem, however if the script is doing other stuff, you could run into issues.


Quote:
Originally Posted by ViN86
Yea, I learned method 2 from a PHP book. Once I realized that you can pretty much insert PHP tags and write some code wherever you want, it only seemed natural to just insert data on the fly wherever instead of searching for strings to replace.
Some do argue against this point in that they should always be separate. IMO, it depends on context and usage.


Quote:
Originally Posted by ViN86
Actually it's even simpler than that. I just write w/e PHP and HTML I need into a page called header.php or something, then I just put in include("header.php"); where I want the header to display and it just echoes the text. You have to be careful, though. I typically store my files in a template folder. But when you use include, it treats the inserted code as if it were coming from the home directory, not where it actually is. So if you're not careful you can mess up links this way.
I usually use a constant to get around this, so that any relative paths are updated, regardless of where you put the file, as long as you update the relative path locale base script.

In method 1, I did this...
Code:
table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td width="169" align="left" valign="top" nowrap="nowrap"><img src="{{PATH}}img/head_top_01.jpg" width="169" height="61" alt="Logo"></td>
		<td align="left" valign="top" nowrap="nowrap"><img src="{{PATH}}img/head_top_02.jpg" ></td>
	</tr>
	<tr>
		<td colspan="2" class="headNav" nowrap="nowrap"><img src="{{PATH}}img/head_nav_01_home_n.jpg" width="68" height="28" alt="Home" /><img src="{{PATH}}img/head_nav_spacer.jpg" width="17" height="28"><img src="{{PATH}}img/head_nav_02_services_n.jpg" width="110" height="28" alt="Services"><img src="{{PATH}}img/head_nav_spacer.jpg" width="17" height="28"><img src="{{PATH}}img/head_nav_03_blog_n.jpg" width="63" height="28" alt="Blog"><img src="{{PATH}}img/head_nav_spacer.jpg" width="17" height="28"><img src="{{PATH}}img/head_nav_04_resume_n.jpg" width="96" height="28" alt="Resume"><img src="{{PATH}}img/head_nav_spacer.jpg" width="17" height="28"><img src="{{PATH}}img/head_nav_05_contact_n.jpg" width="106" height="28" alt="Contact"></td>
	</tr>
</table>

Of course this will be redone using the new method. Actually, this would be a good place to use AJAX, to minimize the number of php entries. It could be part of the constructor to be done automagically.

Method 1: http://ngzroot.ath.cx:9999/www/xntek.bak
Method 2: http://ngzroot.ath.cx/xntek
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...

Last edited by t3hl33td4rg0n; 04-09-10 at 02:40 AM.
t3hl33td4rg0n is offline   Reply With Quote
Old 04-07-10, 05:41 AM   #7
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: LAMP Templating/CMS Performance

Update... I got the preliminary code running and everything looks pretty good so far. If you're interested in seeing what it looks like, I made a copy of the source so you can check it out.

http://ngzroot.ath.cx/xntek/lib/class.content.phps
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...

Last edited by t3hl33td4rg0n; 04-09-10 at 02:39 AM.
t3hl33td4rg0n is offline   Reply With Quote
Old 04-07-10, 07:35 PM   #8
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: LAMP Templating/CMS Performance

Here is a performance report, with a breakdown of CPU cost in code execution.....

http://ngzroot.ath.cx/xntek/reports/...ha_run_03.html
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...
t3hl33td4rg0n is offline   Reply With Quote

Old 04-08-10, 03:30 AM   #9
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: LAMP Templating/CMS Performance

You are interested in this!
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...
t3hl33td4rg0n is offline   Reply With Quote
Old 04-08-10, 11:51 PM   #10
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default Re: LAMP Templating/CMS Performance

Quote:
Originally Posted by ViN86 View Post
Yea there are some limitations to AJAX. Mostly that older browsers don't really support it and different browsers handle things differently.
I can't really compare our CMS at work with what has been discussed here as it doesn't currently use PHP but, jQuery takes care of the cross browser issues. I might actually have to take a closer look at our CMS after reading this thread though... Good Stuff
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 04-09-10, 02:51 AM   #11
t3hl33td4rg0n
Dethklok Returns!
 
t3hl33td4rg0n's Avatar
 
Join Date: Dec 2004
Location: Kent, OH
Posts: 2,142
Send a message via AIM to t3hl33td4rg0n Send a message via MSN to t3hl33td4rg0n Send a message via Yahoo to t3hl33td4rg0n
Default Re: LAMP Templating/CMS Performance

Quote:
Originally Posted by ViN86 View Post
Yea there are some limitations to AJAX. Mostly that older browsers don't really support it and different browsers handle things differently.
One thing that has always worked for me (as far as I could tell) is using a very simple AJAX object where I send the request to the server-side script, then have the script do all the processing, then spit out the HTML. I call it include. It didnt do any JSON or XML output, just HTML.

It takes 2 parameters, the server-side script URI, and the output object. As far as I could see, it worked in all the browsers as intended, even IE6. Also wrote a handful of other functions that would take care of any extra document processing after include() was finished. They would work in a find/replace, or just edit some <divs>. Either way it worked quite well.

Quote:
Originally Posted by jcrox
jQuery takes care of the cross browser issues.
I need to look into that, I have a developer friend that swears by it and I've always heard good things. Anything that makes cross browser design/layout easier is a plus in my book!
__________________


Intel QX9650 @ 3.83GHzXFX 780ieVGA GTX 560Ti8GB GSkill DDR800Creative Sound Blaster X-Fi Titanium PCI-ESeagate 1.5TB . 4x Samsung 2TB

Quote:
Originally Posted by MowTin View Post
....it's so clear how government makes things worse not better...
t3hl33td4rg0n is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:16 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.