Showing posts with label unique id. Show all posts
Showing posts with label unique id. Show all posts

Thursday, September 13, 2012

Loadrunner Unique Names

It is useful to be able to create human-readable unique names to be used for filenames, database keys, logging, etc.  One way to do this is to create a string with the following information:

  • load generator name
  • vuser id
  • iteration number
  • timestamp
This can be done easily as follows:
  • get load generator name using lr_get_host_name()
  • get vuser id using lr_whoami()
  • get iteration number using a parameter of type iteration
  • get current timestamp using lr_save_datetime()
The following script shows how to do this.

Script

Action()
{
int id
char *host; 

        // get load generator name
host = lr_get_host_name( ); 
//lr_output_message("Host: %s", host);

// get vuser id
lr_whoami(&id, NULL, NULL);
//lr_message( "Vuser id: %d",  id);

        // for iteration add a parameter named "Iteration" of type Iteration
//lr_output_message(lr_eval_string("Iteration: {Iteration}")); 

        // get timestamp, formatted as wanted
        lr_save_datetime("%m-%d-%Y-%I%M%S%p", DATE_NOW, "now"); 
//lr_output_message(lr_eval_string("Time: {now}"));

        // now generate the nice, readable unique id
lr_output_message(lr_eval_string("Host-%s-VuserId-%d-Iteration-{Iteration}-Time-{now}"),host, id);

return 0;
}

Output
Action.c(20): Host-generator02-VuserId-1-Iteration-1-Time-09-13-2012-035856PM [MsgId: MMSG-17999]
Action.c(20): Host-generator02-VuserId-2-Iteration-1-Time-09-13-2012-035856PM [MsgId: MMSG-17999]
Action.c(20): Host-generator02-VuserId-1-Iteration-2-Time-09-13-2012-035857PM [MsgId: MMSG-
Action.c(20): Host-generator02-VuserId-2-Iteration-2-Time-09-13-2012-035857PM [MsgId: MMSG-17999
Action.c(20): Host-generator02-VuserId-1-Iteration-3-Time-09-13-2012-035858PM [MsgId: MMSG-17999]