LoadRunner Utility Code: Lance’s Log

Several years back, I found this posting and thought it was interesting. I am just now getting around to sharing it. Try this code if you want. I use this LoadRunner utility code to assist in analyzing test data, or to write desired dynamic data from scripts to .csv files outside of LoadRunner scenario results folders.

This code requires some script parameters to be defined, and you have to pre-allocate your folder where you want to write your data. The code also checks to see if you are running from Vugen or the Controller. If running out of Vugen, it writes the logs to the script’s results folder using a nice name that will sort to the top of the results folder. If you run out of the controller, it writes to the pre-allocated folder, getting the path via the parameter file I call MyLog.dat:

  1. Make sure you have created the folder path
  2. Make sure you can write to the folder from the generator via mapped drive letter.
  3. Create the following script parameters:
  • parmname=”time” format: “%m%d%y,%H%M$S”  This is a custom date/time format with a comma embedded in it. This is used when writing timestamped rows of data to your log. be sure to set “Update each Occurrence”. I assume you already know how to create a custom Date/Time format.
  • parmname=”fname” format: “%m%d%y_%H%M$S” used to create your log file name in the folder you pre-created. Be sure to set “Update Once”
  • parmname=”iter” type iteration parm format “%02d”

Create parameter file (ie .dat) – “MyLog.dat” (or something like it). The value of the parameter in file will be the full path to the folder create in step 1. Use a parameter name like “LogLoc”.

For example:

LogLoc
Q:\Lance\Results\MyLogs\030406\firstRun\

NOTE: The Q drive is a mapped network drive on the generator.

Create the script parameter parmname=”LogLoc” pointing to MyLog.dat to pick up the path

See the whole example below. You can stick the WriteToLog function in your scripts.  I use an include because I feel it is a cleaner method.

#include "web_api.h"
 int WriteToLanceLog(void)
 {
 
extern char* strtok(char *token, const char *delimiter);
extern char *strcat ( char *to, const char *from );
extern void lr_whoami (int *vuser_id, char **sgroup, int *scid);
 
char *scriptpath, *last, *scriptname ;
 
long logHndl;
int id;
char *vuser_group;
char id_string[3];
static char filepath[1024];
 
// first time stuff - create a new log file
 if ( atoi(lr_eval_string("{iter}")) == 1) {
 
// build file name
 lr_whoami(&id, &vuser_group, NULL); // only good in controller mode
 lr_log_message( "*****vuser_group: %s, vuser id: %d, ",
 vuser_group, id );
 
 // if a VUgen run, leave the log in VUgen directory
 if ( strcmp(vuser_group,"None") == 0) {
 scriptpath = lr_get_attrib_string("usr");
 lr_output_message("%s", scriptpath);
 last = (char*)strrchr(scriptpath, '\\');
 lr_output_message("%s", last);
 scriptname = (char *)strtok(last+1, ".");
 lr_output_message("%s", scriptname);
 strcat(filepath, "@");
 strcat(filepath, scriptname);
 lr_log_message("filepath=%s", filepath);
 
strcat (filepath, lr_eval_string("_{fname}") );
 
strcat (filepath, ".csv" ); // make it a csv file
lr_log_message("******filepath=%s", filepath);
 
lr_save_var( filepath, strlen(filepath), 0, "MyLog");
lr_log_message("%s", lr_eval_string("{MyLog}") );
 
} // if ( strcmp(vuser_group,"None") == 0)
 
else { // its running out of controller
 
// strcat (filepath, "@");
// get MH log folder location via parameter
strcat (filepath, lr_eval_string("{LogLoc}") );
lr_log_message("filepath=%s", filepath);
strcat (filepath, vuser_group);
itoa(id, id_string, 10);
strcat (filepath, "_");
lr_output_message("%s", id_string);
strcat (filepath, id_string);
strcat (filepath, lr_eval_string("_{fname}") );
strcat (filepath, ".csv" ); // make it a csv file
lr_output_message("filepath=%s", filepath);
} 
 
// else
// end build file name
 
logHndl = fopen(filepath, "a"); // write header record in log
 
lr_message("logHndl=%d", logHndl);
if (logHndl == NULL) {
lr_error_message("Iteration: %s Unable to create/access %s",
     lr_eval_string("{iter}"), filepath);
return -1;
} //if (logHndl == NULL)
 
fprintf(logHndl, "Date,Time,Iter,MyData\n");
fclose(logHndl);
} // if ( atoi(lr_eval_string("{iter}")) == 1)
 
// This is the normal write routine, after the initial setup above
logHndl = fopen(filepath, "a"); // open log
 
//lr_message("logHndl=%d", logHndl);
 
if (logHndl == NULL) {
 lr_error_message("Iteration: %s Unable to create/access %s",
     lr_eval_string("{iter}"), filepath);
 return -1;
 } //if (logHndl == NULL)
 
// now write the data
fprintf(logHndl, "%s,%s,%s\n",
lr_eval_string("{time}"),
lr_eval_string("{iter}"),
"WhateverDataNeedsToBeWrittenGoesHere" );
 
fclose(logHndl);
 
return 0;
 
}
// end WriteToLog

Now here is what the Action section would look like:

Action()
{
WriteToLanceLog();
return 0;
}

 

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Scott Moore

About Scott Moore (153 articles)

With over 20 years of IT experience with various platforms and technologies, Scott has tested some of the largest applications and infrastructures in the world. He is a Certified Instructor and Certified Product Consultant in HP’s LoadRunner and Performance Center products. He currently holds HP certifications for ASE, ASC, and CI. A thought leader in the APM space, he speaks regularly at IT conferences and events



  • Kevin

    Hi Scott,
    I need some help with vugen code. I am working on a asynchronous document depository application. I first have to go to a mapped network drive to either create a text file or open up a PDF or word files(with different size..5MB, 10MB, 20MB) then save them. Once it is saved there is a process that runs in the background and the web application will pickup this newly created/ modified file. I need to track the latency.
    My thoughts is to create a web script in vugen using the C function fopen something like that to write a file to source location. Then use the use vugen web function to search for the file in the web application.
    I’m sure about the fopen syntax/ is there something else I should be using, can you some examples please.

  • Greg

    Have you had any experience mapping/unmapping a network drive within a script?