Our legacy site: www.webwob.com   

VUgen scripting methods (page 3)

This page will cover more LoadRunner scripting methods, following on from the legacy site pages (PAGE 1).

     LoadRunner CI/CD      *_LR_CI_*      LR_CI_+      Email_out      Email_in      Cookies 1      TUTORIAL      sprintf      debug      VUgen      Kill -9      disk space      AWS S3      dig (DNS++)      substitute      curl with timings      LR_Jenkins_graphs     
    

Functions and methods for VUGen coding (page 2)

Areas covered on this page

1. get_globals_file_lr_variables()

2. log_AWS_ip()

3. get_LR_ip.sh

4. Another IP BASH fucntion I use in other scripts

5. Get current time, hours, minutes etc.




get_globals_file_lr_variables()


VuGen code snippet:

A vugen function used to load parameters out of the globals.h file.
I used BASH to add the lines into the globals.h file, from Jenkins, so i could automate jobs more fully.

get_globals_file_lr_variables()
{
    //USE THIS FUNCTION TO GET THE FILE VARIABLES IN PROJECT SCRIPTS
    
    int total = 0;
    char line[1024], ch;
    long file_stream;
    int lcount,s,n,r,lLen;
    char name[1024],value[2048];
    char *equals, *space;
    int nameLen, valueLen;

    if ((file_stream = fopen("globals.h", "r")) != NULL )
    {
        lcount=0;
        // Get the first line from the file
        while(fgets(line, 1024, file_stream) != NULL)
        {
            lcount++;
            //lr_output_message( "line %d is \"%s\"", lcount,line);
            
            equals = (char*)strchr(line,'=');
            if(strstr(line,"//lr_variable") == line && equals)
            {
                //find the first space - format of lines must be:
                //lr_variable environmnet=perf
                //lr_variable VtsServer=79.xxx.121.13
                
                space = (char*)strchr(line,' ');
                
                nameLen = (long)equals-(long)space-1;
                //lr_output_message("nameLen %d",nameLen);
                
                strncpy(name,space+1,nameLen);
                name[nameLen] = '\0';
                //lr_output_message("name = %s",name);
                
                lLen=strlen(line);
                //lr_output_message("Len = %d",lLen);
                n = strchr(line,'\n');
                r = strchr(line,'\r');
                
                if(n) n = n-(int)line;
                if(r) r = r-(int)line;
                if(n && n < lLen) lLen = n;
                //lr_output_message("Len = %d",lLen);
                if(r && r < lLen) lLen = r;
                //lr_output_message("Len = %d",lLen);
                
                valueLen = (long)line+lLen-(long)equals-1;
                //lr_output_message("valueLen %d",valueLen);
                
                strncpy(value,equals+1,valueLen);
                value[valueLen] = '\0';
                //lr_output_message("value = %s",value);
                
                lr_save_string(value,name);
            }
        }
        
        if (fclose(file_stream))
            lr_error_message("Error closing variable file");
    }
    else
        lr_output_message("lr_variables file not found");
    
    return 0;
}




    
    

log_AWS_ip()


VuGen code snippet:

A vugen function used to get injector IP in use during a load test
VUGEN SCRIPT:
  
  log_AWS_ip()
  {
  #define PATH_MAX 1024
      int status;
      char path[PATH_MAX];
      
      long fp; // file/stream pointer
      int count; // number of characters that have been read from the stream.
      char buffer[BUFFER_SIZE]; // allocate memory for the output of the command.
      char * token;
      char param_buf[10]; // buffer to hold the parameter name.
      int i,k,j,len,gotfirstdot,gotfirstnum;
      char strtemp[256];
      char strtemp2[256];
      int rc;
      
      //USE THIS CODE TO TEST ANY CALLS in vugen:
      //result = system("cd z:\\temp & z:\\github\\PerformanceTesting\\jenkins\\get_LR_ip.bat");
      //lr_output_message("result is %d",result);
      
      //fp = popen("ls *", "r");
      fp = popen("cd z:\\temp & z:\\github\\PerformanceTesting\\jenkins\\get_LR_ip.bat", "r");
      if (fp == NULL)
          /* Handle error */;
      
      
      //lr_log_message("OUTPUT IP FUNCTION RESULTS");
      while (fgets(path, PATH_MAX, fp) != NULL)
      {
          printf("%s", path);
          //lr_log_message("path is: %s",path);
          
          len=strlen(path);
          //lr_log_message("len is: %d",len);
          if(strstr(path,"got our real ip address as: "))
          {
              //lr_log_message("--- found our ip address ---");
              strncpy(strtemp,path+28,len-28);
              strtemp[len-29]='\0';
              lr_log_message("--- Our ip address is: %s ---",strtemp);
              
              sprintf(strtemp2,"Using injector %s",strtemp);
              lr_set_transaction(strtemp2,0.01,LR_PASS); /* my own bit! */
              
          }
      }
      
      //lr_log_message("END OF - OUTPUT IP FUNCTION RESULTS");
      
      status = pclose(fp);
      if (status == -1) {
          /* Error reported by pclose() */
          lr_log_message("Error reported by pclose()");
      } else {
          /* Use macros described under wait() to inspect `status' in order
           to determine success/failure of command executed by popen() */
          //lr_log_message("status not -1");
      }
  
      return 0;
  }
  

get_LR_ip.bat
c:\cygwin\bin\bash -l -c /cygdrive/z/github/PerformanceTesting/jenkins/get_LR_ip.sh


get_LR_ip.sh
echo
echo "get LR ip.sh"
echo

my_ip=$(ipconfig | grep IPv4 | awk 'BEGIN {FS=": "} {print $2}' )
echo ${my_ip}
real_ip=$(aws ec2 describe-instances | grep "${my_ip}" | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | tail -1)
echo "got our real ip address as: ${real_ip}"
real_ip_=$(echo "${real_ip}" | sed 's#\.#_#g')
echo can use this in any file names: "${real_ip_}"
echo
mydate=$( date +%Y%m%d_%H%M%S )
echo ${mydate}
echo


Another IP BASH fucntion I use in other scripts
  
  my_ip=$(ipconfig | grep IPv4 | awk 'BEGIN {FS=": "} {print $2}' | head -1)
  echo "my_ip is ${my_ip}"
  real_ip=$(aws ec2 describe-instances | grep "${my_ip}" | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" | tail -1)
  echo "got our real ip address as: ${real_ip}"
  real_ip_=$(echo "${real_ip}" | sed 's#\.#_#g')
  
  if [ "${real_ip}" == "" ]
  then
      echo "AWS ip not obtained. Going to use local ip instead"
   real_ip_=$(echo "${my_ip}" | sed 's#\.#_#g')
  else
      echo going to use this in the tar file name: "${real_ip_}"
  fi
  
  mydate=$( date +%Y%m%d_%H%M%S )
  tar_file_name="jenkins_backup_${real_ip_}__${mydate}.tar.gz"
  tar -czvf ${tar_file_name} jenkins_backup1 >/dev/null 2>&1
  
  echo "backup zipped up into: ${tar_file_name}"
  echo "backup tar file is $(du -k ${tar_file_name} | cut -f1) KB"
  
  
  


    
    

Get current time, hours, minutes etc.


VuGen example code for using time functions from c:

ACTION.C
  
  #include <time.h>
  
  Action()
  {
      char offsetDate_c[56];
      
      int iOffset;
      time_t t;
      struct tm before;
      
      iOffset = 0;
      //time offset using c - NOTE: use 32_s AND allocate structure above
      t = time32(0) - 60*60*24*iOffset;
      localtime32_s(&before,&t);
      lr_log_message("time now: %ld, %s",t,ctime64(&t));
      sprintf(offsetDate_c,"/%4.4d/%2.2d/%2.2d",before.tm_year + 1900,before.tm_mon+1,before.tm_mday);
      lr_log_message("offset date _c is: %s",offsetDate_c);    
      
      sprintf(offsetDate_c,"%2.2d/%2.2d/%4.4d,%2.2d:%2.2d:%2.2d",before.tm_mday,before.tm_mon+1,before.tm_year + 1900,before.tm_hour,before.tm_min,before.tm_sec);
      lr_log_message("offset date _c is: %s",offsetDate_c);
      
      lr_log_message("hour _c is: %2.2d",before.tm_hour);
      lr_log_message("minute _c is: %2.2d",before.tm_min);
      
      return 0;
  }
  
  
  OUTPUT:
  Starting action Action.
  time now: 1498729114, Thu Jun 29 10:38:34 2017
  offset date _c is: /2017/06/29
  offset date _c is: 29/06/2017,10:38:34
  hour _c is: 10
  minute _c is: 38
  Ending action Action.