Resource icon

Comparing Two Timestamps In Loadrunner 2017-12-07

No permission to download
  • perf-test.com need your contributions to build up a strong repository of performance engineering resources.

Sometime you get into a situation where you need to compare time between two clicks or compare the current timestamp with a server retured value in loadrunner. In this tutorial we have illustrated how you can compare current timestamp with the time on which the tutorial was written.

Attached is a working sample loadrunner script which you can play as a example.

Loadrunner Code:

Code:
Action()
{
  // The "calendar time" structure has the following members
  struct tm {
    int tm_sec; // seconds after the minute - [0,59]
    int tm_min; // minutes after the hour - [0,59]
    int tm_hour; // hours since midnight - [0,23]
    int tm_mday; // day of the month - [1,31]
    int tm_mon; // months since January - [0,11]
    int tm_year; // years since 1900
    int tm_wday; // days since Sunday - [0,6]
    int tm_yday; // days since January 1 - [0,365]
    int tm_isdst; // daylight savings time flag
  };

  int rc; // return code
  struct tm date1;
  struct tm date2;
  long time_difference; // the number of time ticks (seconds) that separate date1 and date2.
  int days, hours, minutes, seconds;
  int mock_time_difference = 0;

  // Save example dates to a parameter. In real life, you might
  // capture these values using web_reg_save_param or similar.
  // date format: mm/dd/yyyy hh:mm:ss
 
  //Param_Date1 - Date of creating this tutorial on www.speforums.com
  lr_save_string("07/12/2017 23:01:00", "Param_Date1");
 
  //Param_Date2 - Current Timestamp
  lr_save_datetime("%d/%m/%Y %H:%M:%S", DATE_NOW, "Param_Date2"); 
 

  // Read the values from the string into the date variables
  rc = sscanf(lr_eval_string("{Param_Date1}"), "%d/%d/%d %d:%d:%d", &date1.tm_mon, &date1.tm_mday, &date1.tm_year, &date1.tm_hour, &date1.tm_min, &date1.tm_sec);
  if (rc != 6) {
      lr_error_message("Problem reading date format. Expected 6 values in string, but found %d", rc);
      lr_abort();
  }

  // As the "calendar time" structure defines tm_year as "years since 1900" and
  // tm_mon as "months since January", we must "fix" these values in the structure.
  date1.tm_mon = date1.tm_mon - 1;
  date1.tm_year = date1.tm_year - 1900;


  // Repeat the above steps for Date2
  rc = sscanf(lr_eval_string("{Param_Date2}"), "%d/%d/%d %d:%d:%d", &date2.tm_mon, &date2.tm_mday, &date2.tm_year, &date2.tm_hour, &date2.tm_min, &date2.tm_sec);
  if (rc != 6) {
      lr_error_message("Problem reading date format. Expected 6 values in string, but found %d", rc);
      lr_abort();
  }
  date2.tm_mon = date2.tm_mon - 1;
  date2.tm_year = date2.tm_year - 1900;

  // Use mktime() to convert the "calendar time" structure to time ticks.
  // Even though this function is not in the VuGen function reference, it is still available to use.
  // Note that Unix time ticks are defined between 1/Jan/1970 and
  // 19/Jan/2038, so odd things may happen with dates outside these ranges.
  time_difference = mktime(&date2) - mktime(&date1);
  lr_output_message("Total number of seconds difference: %d", time_difference);

  // Calculate time difference in days, hours, minutes and seconds.
  days = time_difference/86400;
  time_difference = time_difference - (days * 86400);
  hours = time_difference/3600;
  time_difference = time_difference - (hours * 3600);
  minutes = time_difference/60;
  time_difference = time_difference - (minutes * 60);
  seconds = time_difference;
  lr_output_message("Days: %d, Hours: %d, Minutes: %d, Seconds: %d", days, hours, minutes, seconds);

  return 0;
}

Output :

upload_2017-12-7_23-5-54.png



Other examples - https://speforums.com/resources/loadrunner-function-to-write-current-system-time.68/
Author
admin
Downloads
0
Views
147
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from admin