IT Process Automation

 View Only
  • 1.  Reference Switches outside of loop

    Posted Apr 05, 2016 02:26 PM

    I'm trying to figure out how I can declare a switch outside a loop and reference it once each iteration of the loop without the overhead of having to re-instantiate it every iteration. Is there some way to declare it like a 'Process._ _ _ _' variable(global variable) so I can create it once and reference it for the life of the process without having to recreate it every time?

    I have two switches both of which are quite large and consume a lot of CPU to create and process every iteration of the loop.

     

    Is there something else that I could use instead of switches that wouldn't consume as many system resources?

     

    Thanks for any ideas or help!



  • 2.  Re: Reference Switches outside of loop

    Broadcom Employee
    Posted Apr 06, 2016 09:06 AM

    I'm not positive what you mean by "switches" but a Process.variable is defined at the Process Dataset level and is persistent through the process unless updated by an operator.  Is this a loop operator that you are using?  You should be able to reference the Process.variable within a loop operator without having to do anything within the loop other than referencing it.

     

    Let us know more about what exactly your process looks like if the above does not help.



  • 3.  Re: Reference Switches outside of loop

    Posted Apr 06, 2016 09:50 AM

    I'm just using regular old switches:

     

     

    switch(expression) {

        case n:

            code block

            break;

        case n:

            code block

            break;

        default:

            default code block

    }

     

     

    The reason they are problematic is that they have so many cases they use an excessive amount of system resources to create and execute every iteration on the loop. I'd like to declare it once outside the loop(rather than inside the loop once every iteration of the loop like I currently do) to eliminate the CPU load associated with creating it repeatedly.

     

    About the Process:

    It queries SCCM and creates/updates CIs in the CMDB.

    The pre-execution code of the loop is what will reference the switches(or alternatives) once with every iteration of the loop.



  • 4.  Re: Reference Switches outside of loop

    Posted Apr 06, 2016 02:24 PM

    I think pam runs a new thread for each 'script' portion of an operator so nothing lives in the js runtime past the point execution is done so trying to access that from the context of another operator is a non-starter. You are very correct in that resetting pam loops is a very expensive operation. The best thing I have found is mostly obvious, make them as small as possible. When possible avoid the loop operator all together and use the operators own loop options. There's a few different design patterns i've found much more performant (is that a real word? My spell check doesn't seem to think so) that lumping them into a loop operator.

     

    might also evaluate how you're doing your queries. trying to pull all the data at once, then reconciling the missing (need to be created) handles would probably be faster than incurring the start up / tear down cost of operator when doing it one by one.



  • 5.  Re: Reference Switches outside of loop

    Posted Apr 09, 2016 11:12 AM

    An option would be using the js function eval() to evaluate and execute a piece of code as a string that you can store on a global Process.{variable}. But I don't think it's the best option, and the maintenance of the switch code would be very confusing.

    I don't know how complex is the group of operators inside the Loop Operator, maybe you could create specialized sub-process, for each alternative, call it detached (using events to synchronize each subprocess at its end). A Valuemap can be created, at Process level, to hold the sub-process path of each case so you can pick, during the runtime, to use on every iteration of the loop (in this case you can get rid of the Loop Operator, and use the loop parameter of the Sub-Process Operator)



  • 6.  Re: Reference Switches outside of loop
    Best Answer

    Posted Apr 15, 2016 01:41 PM

    Hey Elwynn,

     

    Here's a couple of hints on how to optimize comparison with JavaScript. (Following advices may apply to several languages)

     

    Use IF statements when :

    • You have 2 or less possibilities to test
    • Your datas can be separated into ranges
      • Ex of a wrong IF usage
    if (value == 0){
    
      return result0;
    
    } else if (value == 1){
    
      return result1;
    
    } else if (value == 2){
    
      return result2;
    
    } else if (value == 3){
    
      return result3;
    
    } else if (value == 4){
    
      return result4;
    
    } else if (value == 5){
    
      return result5;
    
    } else if (value == 6){
    
      return result6;
    
    } else if (value == 7){
    
      return result7;
    
    } else if (value == 8){
    
      return result8;
    
    } else if (value == 9){
    
      return result9;
    
    } else {
    
      return result10;
    
    }
    
    
    
      • To make this better, you can order your if based on the frequency of each value

      • To make this even better, you can organize these statements in branches. Something like :
    if (value < 6){
      if (value < 3){
      if (value == 0){
      return result0;
      } else if (value == 1){
      return result1;
      } else {
      return result2;
      }
      } else {
      if (value == 3){
      return result3;
      } else if (value == 4){
      return result4;
      } else {
      return result5;
      }
      }
    } else {
      if (value < 8){
      if (value == 6){
      return result6;
      } else {
      return result7;
      }
      } else {
      if (value == 8){
      return result8;
      } else if (value == 9){
      return result9;
      } else {
      return result10;
      }
      }
    }
    

     

    Another way to do comparison is to use switch statement as you do.

    To make switch statement better, you may consider ordering your cases based on the frequency of each value.

    You may nest switch statement inside of if statement. Something like :

    if (value < 6){
      switch(value){
        case 0:
            return result0;
        case 1:
            return result1;
        case 2:
            return result2;
        case 3:
            return result3;
        case 4:
            return result4;
        case 5:
            return result5;  
    }
    } else {
    switch(value){
        case 6:
            return result6;
        case 7:
            return result7;
        case 8:
            return result8;
        case 9:
            return result9;
        default:
            return result10;
    }
    }
    

     

     

    When you have a huge amount of possibilities, forget switch and if. The weapon of choice is : Array Lookup!

     

    Imagine you want to get the extension table name for a CI family.

     

    You can store those informations in an array and access it directly.

     

    var family_extName = [['Hardware','ext_hardware'],['software','ext_software']];
    var extName = items[value][1];
    

     

    If you go with Array Lookup, let me suggest to store the array in a dataset outside of your process.

     

     

    Have fun.