Forum Discussion

VictorC's avatar
VictorC
Icon for Nimbostratus rankNimbostratus
Apr 05, 2012

Performance question on 2 iRules

Hi all,

 

 

I have 2 different iRules I am using and they both do the same thing so that I can conserve IP addresses. I was wondering if there was any performance hit on a LTM running 10.2 if I used one over the other. Here are simple samples:

 

 

rule myrule1 {

 

when HTTP_REQUEST {

 

switch [string tolower [HTTP::host]] {

 

"url1.mycompany.com" {

 

pool mypool1

 

}

 

"url2.mycompany.com" {

 

pool mypool2

 

}

 

"myurl3.mycompany.com" {

 

pool mypool3

 

}

 

default {

 

discard

 

}

 

}

 

}

 

}

 

 

rule myrule2 {

 

when HTTP_REQUEST {

 

switch -glob [HTTP::host] {

 

url1* { pool mypool1 }

 

url2* { pool mypool2 }

 

url3* { pool mypoo13 }

 

default { pool mypool1 } or discard it

 

}

 

}

 

}

 

 

Note that on both these iRules I'm expecting to match up to 10 URLs/pools and the number of clients accessing them could be a thousand. I would also be implementing a few dozen of these iRules. I would like to know which iRule performs better and has the least cpu/memory utilization on the LTM.

 

 

Any advice is great.

 

 

Thanks
  • i think comparing literal string (the 1st one) may be more effective.

     

     

    just my 2 cents.
  • I thought the exact match would be more efficient also until I tried a test with tlcsh.

     

     

    % time {switch www.example.com { test.example.com { } sub.example.com { } www.example.com { } }} 100

     

    1 microseconds per iteration

     

     

    % time {switch www.example.com { test.* { } sub.* { } www.* { } }} 100

     

    0 microseconds per iteration

     

     

    You check this with your iRule using timing:

     

     

    https://devcentral.f5.com/wiki/iRules.timing.ashx

     

     

    Aaron
  • There are plenty of examples from F5 about switch being faster than if/else. Here are a couple:

     

     

    https://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/108/iRules-Optimization-101--01--if-elseif-and-switch.aspx

     

    An even more efficient manner of dealing with multiple if statements is to get rid of them all together! No, I don't mean remove the logic from your rule, I just mean use switch instead. The switch command is more efficient, in general, than using a string of if or even if/elseif statements. It's also flexible in the matching that you can do.

     

     

    https://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086424/Comparing-iRule-Control-Statements.aspx

     

    Always think: "switch", "data group", and then "if/elseif" in that order.

     

    If you think in this order, then in most cases you will be better off.

     

    Use switch for < 100 comparisons.

     

    Switches are fastest for fewer than 100 comparisons but if you are making changes to the list frequently, then a data group might be more manageable.

     

     

     

     

    In general, I don't think twice before using a switch if it's more than just an if/else.
  • Thank you all for the great tips.

     

     

    @dlg: Both of the iRules use the switch command. I was curious whether exact matching (string tolower) vs. using a wildcard* matching (with glob) would have any significant performance impact.

     

     

    With @hoolio's suggestion, I used the time call and the comparisons show no real significant differences, so I'm a lot more comfortable now. The glob iRule is a lot cleaner and would save me from typing all the DNS suffixes.

     

     

    Thanks again all.
  • Hi Victor,

     

     

    Glad you figured this out. For many scenarios you'll have more than one option on how to do it. If the performance of the options is comparable, I'd go with the one that is more intuitive or simple.

     

     

    Aaron