Forum Discussion
pwallace_110041
Nimbostratus
Jul 31, 2006Optimization 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 {...
Jul 31, 2006
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 ruleshttp://devcentral.f5.com/wiki/default.aspx/iRules/HowToWriteFastRules.htmlClick here
Cheers!
-Joe
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects