Forum Discussion

pwallace_110041's avatar
pwallace_110041
Icon for Nimbostratus rankNimbostratus
Jul 31, 2006

Optimization review

I am looking for any way to make this little rule better, cleaner, optimized etc. iRules are my first foray into coding and I am looking for tips and tricks.

 

 

when HTTP_REQUEST {

 

if { [HTTP::uri] starts_with "/locmap/" and [HTTP::uri] contains ".asp" ||

 

[HTTP::uri] starts_with "/locmap/" and [HTTP::uri] ends_with ".css" ||

 

[HTTP::uri] starts_with "/scripts/webgate.dll/" ||

 

[HTTP::uri] starts_with "/locmap/" and [HTTP::uri] ends_with ".gif" } {

 

pool mappool

 

} else {

 

discard

 

}

 

}

 

 

 

Regards,

 

 

Pippin

1 Reply

  • You have a fairly straight-forward rule but I see a couple of things right off

    1). The order of precedence for testing your first if statement is probably not what you are going to intent as you haven't bracketed off your ands from your ifs. Odds are you want it to mean if ((a and b) or (c and d)), not (a and (b or c) and d)... Always group your logic operators to avoid extra headaches.

    2) all the separate comparisons for the same first string "/locmap" could be duplicated many times for your last condition. A call for "/locmap/foo.gif" will result in 7 string comparisons. When building an iRule you should try to minimize the duplication of calls (in this case 3 calls for "/locmap").

    3) look at your usage patterns and try to place the comparisons for the most highly requested uri's first. In your case, if gif files are requested most often, then they should be placed at the top of the check so the rest of the comparisons aren't unnecessarily made.

    Here's a quick shot that I did to try to minimize the amount of string comparisons without using regular expressions. I'm sure there are better ways than this but here you go:

    when HTTP_REQUEST {
      if { [HTTP::uri] starts_with "/locmap/" } {
        if { ([HTTP::uri] contains ".asp") ||
              ([HTTP::uri] ends_with ".css") ||
              ([HTTP::uri] ends_with ".gif") } {
          pool mappool
        } else {
          discard
        }
      } elseif { [HTTP::uri] starts_with "/scripts/webgate.dll/" } {
        pool mappool
      } else {
        discard
      }
    }

    By moving the logic into several if/else/elseif's, you limit the total number of string comparisons to 4 or less.

    One final option would have been to use a switch statement with the "-glob" option with regular expressions. it would have been much easier to read, but odds are not as optimial as this one.

    As for optimizing rules, make sure you read the tech tip on how to write fast rules

    http://devcentral.f5.com/wiki/default.aspx/iRules/HowToWriteFastRules.html

    Click here

    Cheers!

    -Joe