Forum Discussion

smp_86112's avatar
smp_86112
Icon for Cirrostratus rankCirrostratus
Apr 19, 2011

Which is more efficient - switch or class statement?

We were evaluating a v10 rule today which sets a pool based on the client IP address:



when HTTP_REQUEST {
  switch [IP::client_addr] {
    "10.15.102.126"  -
    "10.15.101.14"    -
    "10.11.12.205"    -
    "10.108.2.117"    -
    "10.49.168.11"    -
    "10.116.15.44"    -
    "10.220.55.86"    -
    "10.101.11.88"    -
    "10.106.17.223"  -
    "10.44.49.221"    -
    "10.110.119.41"  { pool test_pool }
  }
}

My coworker is much more skilled at programming than I am, but does not have as much F5 admin experience as I do. I thought I remembered reading something somewhere that claimed the use of classes is not as efficient as a switch statement in an iRule. But from a unix perspective, my coworker believes using a class statement should be more efficient. So I wrote another irule that performed the same check using a class/data group versus a switch statement, and did some timing tests. I tested four scenarios by blasting the VS with 100K connections from ApacheBench, keying on the IP of my test server. I tested the rule where the IP of my test system matched the class (1) and switch (2), and when the IP of my test system did not match the class (3) or switch (4).

Here's the rewritten rule using the class statement. The class was type "address":


when HTTP_REQUEST {
  if { [class match [IP::client_addr] equals "test_class"] } {
    pool test_pool
  }
}

And here are my results. The switch statement appeared to be ~20% more efficient than the same iRule written to use a class statement:

Operation IP Match Avg CPU Cycles Max CPU Cycles

class yes 11094 87035

switch yes 9090 194695

class no 8966 74882

switch no 7031 85863

Is it true generally speaking that switch statements are more efficient than class statements, given a relatively small number of arguments like this? Or do I have a flaw somewhere in my testing methodology?

3 Replies

  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus
    It depends on the number of items you're checking against. The rule of thumb is to use "switch" up to about a 100 items and a class for more than a 100 items.

     

     

    However, this is from a performance perspective. One problem I've encountered with using switches is that iRules tend to apply only to new connections. In other words, updates to iRules do not affect existing clients. In my experience classes do not have this drawback.

     

  • In addition to Arie's comments, I also expect a datagroup and class lookup would be more efficient if you're handling subnets instead of single hosts.

     

     

    Aaron
  • Hamish's avatar
    Hamish
    Icon for Cirrocumulus rankCirrocumulus
    And a lot easier... A rule of thumb I try to apply is no data in iRules... That includes IP addresses, networks etc..

     

     

    The tip about iRule updates and data group updates is especially pertinent. And means a lot more when you're considering a long lived connection rather than simple HTTP.

     

     

    H