Forum Discussion
smp_86112
Cirrostratus
Jun 24, 2010Optimizing If statement conditions
I see this examples like this all over the place:
when HTTP_REQUEST {
if { [HTTP::host] equals "www.myhost.com" and [HTTP::uri] equals "/myuri" } {
HTTP::redirect https://[HTTP::host][HTTP::uri]
}
}
I am going though a large number of iRules in an attempt to optimize, and I had a thought occur to me while looking at one of mine like this. Shouldn't I be able to consolidate these two conditions into one, and wouldn't it be more efficient? Something like this?
when HTTP_REQUEST {
if { [[HTTP::host][HTTP::uri]] equals "www.myhost.com/myuri" } {
HTTP::redirect https://[HTTP::host][HTTP::uri]
}
}
As a practical matter, this iRule generates an error when it is triggered:TCL error: www.myhost.com - invalid command name "www.myhost.com/myuri" while executing "[HTTP::host][HTTP::uri]"
Assuming I can figure out how to properly write this condition (please help???), wouldn't this be more efficient than writing two conditions to evaluate and then compare? If I can get the syntax right, I'll test it out.
- Chris_Miller
Altostratus
Edit - I see what you're saying here...basically, combining http::host and http::uri into one command...I'll look into. Looking at the http command list below...I don't see an option here: http://devcentral.f5.com/wiki/default.aspx/iRules.HTTP - The_Bhattman
Nimbostratus
Hi SMP,when HTTP_REQUEST { set url [HTTP::host][HTTP::uri] if {$::url equals "www.something.com/blah"} { . . . do something . . . } }
- The error is coming up because you are surrounding the combined string with brackets. Try replacing the surrounding brackets with quotes (or curly braces if your embedded string contains quotes itself)
when HTTP_REQUEST { if { "[HTTP::host][HTTP::uri]" equals "www.myhost.com/myuri" } { HTTP::redirect https://[HTTP::host][HTTP::uri] } }
- The_Bhattman
Nimbostratus
I guess I learned something new. - smp_86112
Cirrostratus
Thank you very much for weighing in on this Joe, adding double-quotes did indeed consolidate the conditions as I expected. Boy, TCL sure isn't Perl is it?
Since you were kind enough to provide valueable insight, I did some analysis. I used ApacheBench Version 2.0.41-dev <$Revision: 1.141 $>. I hit the VIP 100000 times with a maximum number of concurrent connections set to 25. And I ran the test three times for each method. The results are below.
if { "[HTTP::host][HTTP::uri]" equals "www.myhost.com/admin" }
timing on when HTTP_REQUEST { if { "[HTTP::host][HTTP::uri]" equals "www.myhost.com/admin" } { HTTP::uri "/new_uri" } } ******************** ltm rule-event myrule:HTTP_REQUEST { aborts 0 avg-cycles 11501 event-type HTTP_REQUEST failures 0 max-cycles 121243 min-cycles 0 name myrule priority 500 total-executions 100022 } ltm rule-event myrule:HTTP_REQUEST { aborts 0 avg-cycles 11536 event-type HTTP_REQUEST failures 0 max-cycles 220940 min-cycles 0 name myrule priority 500 total-executions 100016 } ltm rule-event myrule:HTTP_REQUEST { aborts 0 avg-cycles 11366 event-type HTTP_REQUEST failures 0 max-cycles 105512 min-cycles 0 name myrule priority 500 total-executions 100015 }
timing on when HTTP_REQUEST { if { [HTTP::host] equals "www.myhost.com" and [HTTP::uri] equals "/admin" } { HTTP::uri "/new_uri" } } ******************** ltm rule-event myrule:HTTP_REQUEST { aborts 0 avg-cycles 11877 event-type HTTP_REQUEST failures 0 max-cycles 112904 min-cycles 0 name myrule priority 500 total-executions 100015 } ltm rule-event myrule:HTTP_REQUEST { aborts 0 avg-cycles 11751 event-type HTTP_REQUEST failures 0 max-cycles 148691 min-cycles 0 name myrule priority 500 total-executions 100018 } ltm rule-event myrule:HTTP_REQUEST { aborts 0 avg-cycles 11810 event-type HTTP_REQUEST failures 0 max-cycles 77001 min-cycles 0 name myrule priority 500 total-executions 100014 }
- I knew I liked that approach for a reason B-).
- smp_86112
Cirrostratus
I agree with you Joe,. To me, the consolidated conditions is more readable - and based on my testing, it's faster. It surprises me that I don't see this alternative used much on devcentral. In fact, I haven't found it anywhere. - JRahm
Admin
Could be due to the fact that HTTP::host is not case sensitive but HTTP::uri is (or can be in non-windows environments), so combining them might require additional tests, or a string tolower wrapper. - hoolio
Cirrostratus
Interesting testing. I would think the combined check of HTTP::host and HTTP::uri together compared with separate checks won't be as efficient if the request hostname doesn't match what's in the iRule. It would also not make sense to do a combined check if you want to check for multiple URIs. - Michael_Yates
Nimbostratus
I would be interested in seeing the test results from adding the a string tolowrapper that Jason pointed out.
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