|
|
#1 | |
|
Dethklok Returns!
|
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}}
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:
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 Code:
Fastest: 0.0021ms Average: 0.0023ms Slowest: 0.0027ms Last edited by t3hl33td4rg0n; 04-07-10 at 05:23 AM. |
|
|
|
|
|
|
#2 | |
|
Registered User
Join Date: Mar 2004
Posts: 15,486
|
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. |
|
|
|
|
|
|
#3 | ||||||
|
Dethklok Returns!
|
Quote:
Quote:
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:
Quote:
PHP Code:
Quote:
![]() 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:
Last edited by t3hl33td4rg0n; 03-22-10 at 10:33 PM. Reason: Whoopsie |
||||||
|
|
|
|
|
#4 | ||||
|
Registered User
Join Date: Mar 2004
Posts: 15,486
|
Quote:
Quote:
![]() Quote:
Quote:
![]() |
||||
|
|
|
|
|
#5 | |||
|
Dethklok Returns!
|
Quote:
Quote:
Quote:
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>
|
|||
|
|
|
|
|
#6 | |||
|
Dethklok Returns!
|
Quote:
Quote:
Quote:
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 Last edited by t3hl33td4rg0n; 04-09-10 at 02:40 AM. |
|||
|
|
|
|
|
#7 |
|
Dethklok Returns!
|
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 Last edited by t3hl33td4rg0n; 04-09-10 at 02:39 AM. |
|
|
|
|
|
#8 |
|
Dethklok Returns!
|
Here is a performance report, with a breakdown of CPU cost in code execution.....
http://ngzroot.ath.cx/xntek/reports/...ha_run_03.html |
|
|
|
|
|
#9 |
|
Dethklok Returns!
|
You are interested in this!
|
|
|
|
|
|
#10 | |
|
Mahna Mahna
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,123
|
Quote:
![]()
__________________
|
|
|
|
|
|
|
#11 | ||
|
Dethklok Returns!
|
Quote:
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:
|
||
|
|
|
![]() |
| Thread Tools | |
|
|