Custom LoadRunner Library Code

From PeformIQ Upgrade
Jump to navigation Jump to search

lib.c

C utilities for local customization of LoadRunner scripts.

Source Code: lib.c

//
//
//-------------------------------------------------------------------------------------------------

#include "web_api.h"
#include "lrw_custom_body.h"

#include "lib.h"

//-------------------------------------------------------------------------------------------------

typedef long   time_t;

//-------------------------------------------------------------------------------------------------

static time_t  save;

//-------------------------------------------------------------------------------------------------

char           msgbuf[4096000];
char           tmpbuf[102400];

long           lfp           = NULL;

char          *logfile = "_run.log";

//-------------------------------------------------------------------------------------------------

long
refTime(int flg)
{
        long
                t;

        time(&t);

        if (flg) {
                save = t;
                t    = 0;
        } else {
                t    = t - save;
        }

        return t;
}       // refTime

//-------------------------------------------------------------------------------------------------

double
theTime(void)
{
        double
                t;

        t = refTime(0) * 1.0;

        return t;
}       // theTime

//-------------------------------------------------------------------------------------------------

void initLog(char *project, char *script, char* sgroup, int vuser_id)
{
        char           logfile[128];
    time_t         t;
        double         now;

        refTime(1);

        time(&t);

        if (vuser_id != -1) {
                sprintf(logfile, "C:\\lr\\%s\\log\\%s__%03d__%s.log", project, sgroup, vuser_id, script);
        } else {
                sprintf(logfile, "_run.log", vuser_id);
        }

        if ((lfp = fopen(logfile, "a")) == NULL ) {
                lr_error_message("Cannot open %s", logfile);
                lr_abort();
                exit(1);
        }

    sprintf(msgbuf, "Timestamp (%d) - %s", t, ctime(&t) );
        fprintf(lfp, "------  %s------  =================================================\n", msgbuf);
        fprintf(lfp, "------  Script: %s  Scenario Group: %s\n", script, sgroup);
        fflush(lfp);

    lr_output_message("=====>  %s", msgbuf);
}  // initLog

//-------------------------------------------------------------------------------------------------

void closeLog(void)
{
    time_t         t;

        time(&t);

    sprintf(msgbuf, "Timestamp (%d) - %s", t, ctime(&t) );
        fprintf(lfp, "------  Close() @ %s------ =================================================\n", msgbuf);
        fflush(lfp);
        close(lfp);

    lr_output_message("=====>  %s", msgbuf);
}  // initLog

//-------------------------------------------------------------------------------------------------

void userLog(char *msg)
{
        double
                now;

        now = theTime();

        if (lfp != 0) {
                fprintf(lfp, "%06.1f  %s\n", now, msg);
                fflush(lfp);
        }

    lr_output_message("=====>  %s", msg);
}  // userLog

//-------------------------------------------------------------------------------------------------

char *
upper(char* s)
{
        int i;

        static char buf[128];

        int l = strlen(s);

    if (l >= 127) {
                userLog("upper() overflow!");
                lr_abort();
        }

        for (i = 0; i < l; i++) {
                buf[i] = toupper(s[i]);
        }

        buf[l] = '\0';

        return buf;
}  // upper

//-------------------------------------------------------------------------------------------------

int day_of_week(char *day)
{
        char *uc_day     = upper(day);
        int   l          = strlen(day);
        int   rc         = 0;

        if (!strncmp(day, "SUNDAY", l)) {
           rc = 1;
        } else if (!strncmp(uc_day, "MONDAY", 3)) {
                rc =  2;
        } else if (!strncmp(uc_day, "TUESDAY", 3)) {
           rc =  3;
        } else if (!strncmp(uc_day, "WEDNESDAY", 3)) {
           rc =  4;
        } else if (!strncmp(uc_day, "THURSDAY", 3)) {
           rc =  5;
        } else if (!strncmp(uc_day, "FRIDAY", 3)) {
           rc =  6;
        } else if (!strncmp(uc_day, "SATURDAY", 3)) {
           rc =  7;
        }

        return rc;
}  // day_of_week

//-------------------------------------------------------------------------------------------------

int randidx(int array_count)
{
        int n;

        srand(time(NULL)); 

        n = rand() % array_count;

        return n + 1;  // matched arra
}  // randint

//-------------------------------------------------------------------------------------------------

Header: lib.h

#ifndef _LIB_H_
#define _LIB_H_

//-------------------------------------------------------------------------------------------------

#define TRUE     1
#define FALSE    0

//-------------------------------------------------------------------------------------------------

extern char           tmpbuf[];
extern char           msgbuf[];

extern long           lfp;

extern char          *logfile;

//-------------------------------------------------------------------------------------------------

void initLog(char *project, char *script, char* group, int vuser_id);
void userLog(char *msg);
void closeLog(void);

int  day_of_week(char *day);

//-------------------------------------------------------------------------------------------------

extern char *strtok(char *string, char *separators);
extern char *SEPARATORS;

//-------------------------------------------------------------------------------------------------

#endif // _LIB_H_

globals.h

#ifndef _GLOBALS_H_
#define _GLOBALS_H_

//-------------------------------------------------------------------------------------------------
// Include Files

#include "lrun.h"
#include "web_api.h"
#include "lrw_custom_body.h"

//-------------------------------------------------------------------------------------------------
// Global Variables

extern int       load_resources;
extern double    t_tx;

extern char     *project;

//-------------------------------------------------------------------------------------------------