SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)

    IfModule checks in htaccess - performance impact?

    Do IfModule checks in your htaccess file actually result in a performance hit?

    It doesn't seem to make a difference on the servers I maintain (granted, they don't get a very high amount of traffic), and I haven't run any heavy analysis other than browser load times.

    I've seen folks who say that it does result in a significant performance hit, and also folks that say there's no hit, and still others that say it's so miniscule that it doesn't really matter. However, I haven't seen any solid data to back this up to show which school of though is actually correct.

    Has anyone seen any solid data/testing on the subject?
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,278
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    There are a couple ways we can measure. The first is with a simple PHP loop.

    Code PHP:
    $url = 'http://localhost/some/path';
    $nRequests = 100;
     
    $start = microtime(true);
    for ($i = 0; $i < $nRequests; $i++) {
        file_get_contents($url);
    }
    $stop = microtime(true);
     
    $timePerRequestInSeconds = ($stop - $start) / $nRequests;
    $timePerRequestInMilliseconds = $timePerRequestInSeconds * 1000;

    And the second is with an HTTP stress tester, such as siege or httperf.

    I tested response times with the following two htaccess files. One with an <IfModule> test:

    Code:
    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # Rewrite non-file requests to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php
    </IfModule>
    And one without:

    Code:
    RewriteEngine On
    
    # Rewrite non-file requests to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php
    Here are the results I got on my machine:

    PHP loop:
    With <IfModule>: 9.7802 ms/req +/- 0.58
    Without <IfModule>: 9.74254 ms/req +/- 0.64

    siege (siege -b -t10s http://localhost/some/path):
    With: 1,454.244 reqs/sec +/- 8.165
    Without: 1,460.032 reqs/sec +/- 8.135


    The difference between the two is within each other's margin or error, and even then, the difference is measured in microseconds. No doubt an "if" check executes some number of additional instructions, but the performance difference is so miniscule that it doesn't really matter.
    "First make it work. Then make it better."

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    FF,

    I've seen a couple members who've had a several <IfModule> wrapped blocks in their .htaccess. Depending upon the number of matches (and redirections) made by the .htaccess mod_rewrite code, each test will be run multiple times for every file request. Why? What does that accomplish?

    Okay, I'm sure that each one takes only a few dozen machine cycles but that time is simply wasted - as is the time to load that useless code each time the .htaccess file is read. Cumulatively, the number of wasted cycles must add-up into significant delays. If you're on a dedicated server, you're only delaying your own visitors; if you're on a shared server, the number of useless tests is multiplied by the number of accounts on that server. Very quickly, you're talking about real time ... and abuse of the server.

    Any professional webmaster should KNOW what modules are enabled on his/her server so it is ridiculous (and an abusive of the server) to run the same test over and over and over and ... As I have reminded members in my Standard Rant #4, the definition of an idiot is someone who makes the same test over and over and expects different answers each time. I've always reminded members to test ONCE then remove the <IfModule> blocks. I consider it a matter of professionalism and have been shocked at the number of newbies who don't understand this ... until pointed out. To have anyone argue to continue testing, well, Standard Rant #4 identifies the problem.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,278
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    It's worth noting that the reason CMSs, frameworks, etc, write the <IfModule> checks in the first place is because they want the htaccess file to be portable. It's meant to add features to your site if a module is available, but not crash your site if a module isn't available. Though, once the htaccess file has a permanent home on a server with a known configuration, then I agree it's safe to remove those "if" checks.

    However, if the only motivation to do so is performance, then there are much more important things we could focus on. Reduce the number of database queries, or make a sprite. Those are things that will actually matter. There's nothing wrong with removing the "if" checks when it's safe, but we shouldn't exaggerate its performance impact (which is virtually nil).
    "First make it work. Then make it better."

  5. #5
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    The difference between the two is within each other's margin or error, and even then, the difference is measured in microseconds. No doubt an "if" check executes some number of additional instructions, but the performance difference is so miniscule that it doesn't really matter.
    Thanks for performing the test. Those were the results that I was expecting would probably turn up.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Force Flow View Post
    Thanks for performing the test. Those were the results that I was expecting would probably turn up.
    Unfortunately, JM's test failed to account for multiple <IfModule> wrappers and multiple redirections ... on a shared server. I would have expected nothing else (from a few machine cycles). I'll bet, if he ran that test multiple times, he'd come up with the same answer!

    That leaves me at the same point: Running the same test repeatedly accomplishes nothing but a waste of resources. However, I've changed my Standard Rant #4 as the definition I was using was for insanity, not an idiot, therefore,

    [standard rant #4]
    The definition of insanity is to repeatedly do the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world. DON'T ACT INSANE! If you don't know whether a module is enabled, run the test ONCE (without the wrapper) then delete it permanently as it is EXTREMELY wasteful of Apache's resources (and should NEVER be allowed on a shared server).
    [/standard rant #4]

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,278
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    ...as it is EXTREMELY wasteful of Apache's resources...
    And just like that, your standard rant becomes your standard misinformation. The impact of an <IfModule> wrapper is so small that it's barely even measurable. To call it "extremely wasteful" is just flat wrong.
    "First make it work. Then make it better."

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Would you prefer "just plain stupid" because it accomplishes nothing but wastes machine cycles?

  9. #9
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,278
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Would you prefer "just plain stupid" because it accomplishes nothing but wastes machine cycles?
    There's nothing wrong with removing the "if" checks when it's safe, but we shouldn't exaggerate its performance impact (which is virtually nil).
    "First make it work. Then make it better."

  10. #10
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,551
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    By apache's own recommendation, put a directive in httpd.conf rather than in a .htacess file if you want a (very small) increase in performance. For the vast majority of people the convenience of using .htaccess is a lot more important though.

  11. #11
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dklynn View Post
    Would you prefer "just plain stupid" because it accomplishes nothing but wastes machine cycles?
    There's a few things to take into account:
    Performance (how much of an impact will it have? In this case, very little)
    Convenience (It's convenient just to leave the "If" checks in, rather than to make all the necessary edits)
    Portability (If I move the site to a new server or reuse the htaccess file, by leaving in the "If" checks, I don't have to worry about hard failures if the module doesn't exist).

    I agree that the "best practice" is to remove the "If" checks on a server where you know that the modules are installed. For portability, this is not a best practice (And htaccess code and files tend to be ported all over the place).

    However, the point was was to see how detrimental the "if" checks were to performance if they were left in (as they typically are because of portability). For a site like slashdot, milliseconds will add up. For smaller sites...not so much.

    Bottom line--it doesn't matter nearly as much as your "rants" would lead folks to believe.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  12. #12
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,635
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I'll add, as a guy who has done some serious web scalability stunts here and there, that micro optimizations such as this IfModule thing really don't move the needle -- we are talking nanoseconds at most, none of which will be blocking your actual precious resources which are disk and network I/O. If you are looking at real scale you start looking at farms of machines with intelligent, aggressive caching. In fact your typical requests when not under load will often get slower as you are building to scale out fast and serve everyone under some bar, not micro optimize your personal blog about banna slugs.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •