pcl2004_27 发表于 2006-10-30 14:16:08

LoadRunner:customC code

1.        Writing output file to an external file

#include "as_web.h"


Action()
{
long stream;
char kmp1;

lr_vuser_status_message("Iteration %s", lr_eval_string("{it_number}")); //iterations

if( (stream = fopen( "E:\\Current_LTProjects\\test\\order1.txt", "a" )) == NULL )

return(-1);


lr_output_message("Iteration Number:""%s",lr_eval_string("{it_number}"));
strcpy (kmp1,lr_eval_string("{it_number}"));
fprintf(stream,"Iteration number:%s",kmp1);
//fCLose(stream);
        return 0;

}

2. Randomly clicking on URL within a set of URL’s
#include "as_web.h"

int i, rand_selection;
char my_new_parameter;

Action1()
{
lr_set_debug_message (LR_MSG_CLASS_PARAMETERS, 1);

        web_url("www.google.com",
                "URL=http://www.google.com/",
                "Resource=0",
                "RecContentType=text/html",
                "SupportFrames=0",
                "Snapshot=t1.inf",
                LAST);

web_reg_save_param("search_param", "LB=<p><a href=", "RB=>", "ORD=All");

        web_submit_data("search",
                "Action=http://www.google.com/search",
                "Method=GET",
                "EncType=",
                "TargetFrame=",
                "RecContentType=text/html",
                "Referer=http://www.google.com/",
                "Snapshot=t2.inf",
                "Mode=HTML",
                ITEMDATA,
                "Name=hl", "Value=en", ENDITEM,
                "Name=ie", "Value=UTF8", ENDITEM,
                "Name=oe", "Value=UTF8", ENDITEM,
                "Name=q", "Value=Bos", ENDITEM,
                "Name=btnG", "Value=Google Search", ENDITEM,
                LAST);

lr_output_message("******************** There were: %s matches", lr_eval_string("{search_param_count}"));

for (i = 1; i <= atoi(lr_eval_string("{search_param_count}")) ; i++)
{
      sprintf (my_new_parameter, "{search_param_%d}", i);

      lr_output_message ("%s", lr_eval_string(my_new_parameter));
}

rand_selection = (rand() % atoi(lr_eval_string("{search_param_count}")) + 1);

lr_debug_message(LR_MSG_CLASS_PARAMETERS, "*************rand selection is %d", rand_selection);

sprintf(my_new_parameter, "{search_param_%d}", rand_selection);

lr_debug_message (LR_MSG_CLASS_PARAMETERS, "The random parameter selection is %s", lr_eval_string(my_new_parameter));

lr_save_string(lr_eval_string(my_new_parameter), "my_new_parameter");

web_url("TEST PARAMETER",
                "URL={my_new_parameter}",
                LAST);



        return 0;
}
3. Declare transaction name dynamically

#include "as_web.h"


Action1()
{

/*
        Purpose:This script shows how to create dynamic transaction names.The transaction will
        not show up in the Controller interface but will in the analysis results.

*/

        // declaring variables for transaction names
        char trans_name;
        char *name;

        name = "tom";

        // creating the dynamic transaction names
        sprintf(trans_name, "tom_%s", lr_eval_string("{rand_num}"));

        lr_start_transaction(trans_name);
       
        lr_think_time(1);

        lr_end_transaction(trans_name, LR_AUTO);
       
        sprintf(trans_name, "tom_%s", lr_eval_string("{rand_num}"));

        lr_start_transaction(trans_name);
       
        lr_think_time(1);

        lr_end_transaction(trans_name, LR_AUTO);

        lr_output_message("==>%s", name);

        return 0;
}




4. String Usage


// Example 1:

// declaring a prototype
char *strtok(char *, char *);

Actions()
{
    /* this forces the intepreter to save input_line in read/write memory.
        If a character array is used, value would be save in read only memory
        and would cause a memory violation when strtok is used. */

        char input_line = "Hi, my name is tom.";

        // variable to hold a word from the above phrase.
        char *word;

        // gets the first word in the phrase.
        word = strtok(input_line, " ");

        // prints out the whole phrase.One word at a time.
        while (word != NULL) {
                lr_output_message("===>%s<===", word);
                word = strtok(NULL, " ");
        } // End of While

        return 0;
}

//==============================================================


//        EXAMPLE 2:
// declare a prototype
char *strstr(char *, char *);

Actions()
{
        // declare C variables
        char str2 = "name";
        char str1 = "Hi, my name is tom";

        char *temp_buf;

        // Example of how to print out the values
        lr_output_message("===>%s<===", str1);
        lr_output_message("===>%s<===", str2);

        // example of how to use strstr
        lr_output_message("===>%s<===", strstr(str1, str2));
          temp_buf = strstr(str1, str2);

        lr_output_message("===>%s<===", temp_buf);


        return 0;
}

//==============================================================

//        EXAMPLE 3:
char *strtok(char *, char *);
char *strdup(char *);


Actions()
{
    /* this forces the intepreter to save input_line in read/write memory.
        If a character array is used, value would be save in read only memory
        and would cause a memory violation when strtok is used. */

        char *input_line = strdup("Hi, my name is tom.");
        char *word;

        // variable to hold a word from the above phrase.
        word = strtok(input_line, " ");

        // prints out the whole phrase.One word at a time.
        while (word != NULL) {
                lr_output_message("===>%s<===", word);
                word = strtok(NULL, " ");
        }        // End of While

        return 0;
}









5. For IP Spoofing – To find out name of host machine
#include "as_web.h"


Action()
{

char *my_ip;
char *my_host;



my_ip = lr_get_vuser_ip();

my_host = lr_get_host_name( );


web_url("yahoo.com",
"URL=http://yahoo.com/",
"Resource=0",
"RecContentType=text/html",
"Referer=",
"Snapshot=t1.inf",
"Mode=HTML",
LAST);

lr_output_message("The IP address is %s", my_ip);

lr_output_message("Host IP address is %s", my_host);
        return 0;
}

6. For viewing vuser status on controller or VUGen(No of iterations)


lr_vuser_status_message("Iteration %s", lr_eval_string("{it_number}")); //iterations






7. Create parameter values programmatically in the script.
Char Group;
Int VuserNumber, iteration=0;
Char parameterValue;

Int LR_FUNC Actions(LR_PARAM p)

{
lr_whoami(&VuserNumber, &Group, NULL);
SPRINTF(parameter_Value,%d%s%d”,VuserNumber, Group, iteration);
Lr_save_string(parameterValue,” parameter_name”);
Iteration++;

In this case parameter_name will have some value which will be unique for all vusers per iteration

8. Using lr_advance_param function

#include "as_web.h"

// Requirement:
// Total Vusers: 5
// Total iterations: 3
// Total data in parameter file = 15
// Behavior:
//   Vuser 1 picks up data 1, 6, 11
//   Vuser 2 picks up data 2, 7, 12
//   Vuser 3 picks up data 3, 8, 13
//   Vuser 4 picks up data 4, 9, 14
//   Vuser 5 picks up data 5, 10,15

Action1()
{
        int x,y,v,i;

        //get the VuserID and store it in v
        v=atoi(lr_eval_string("{VuserID}"));

        //get the Iteration number and store it in i
        i=atoi(lr_eval_string("{Iteration}"));

        //For the first iteration, x = VuserID
        if(i==1) x=v;

    //For the second iterations onward, x = number of Vusers + 1
        //You need to modify this number according to the number of Vusers you have in the scenario
        else x=6;       

        for(y=1;y<x;y++)
                lr_advance_param("NewParam");

        lr_output_message("This is vuser %d , iteration %d, with data %s",v,i ,lr_eval_string("{NewParam}"));

        return 0;


9. Parameterize lr_think_time
When the user tries to run a script in which lr_think_time is parameterized, VuGen issues the following errors:
run.c (x): illegal expression
run.c (x): syntax error; found `Param1' expecting `)'
run.c (x): syntax error; found `Param1' expecting `;'
run.c (x): undeclared identifier `Param1'
run.c (x): illegal expression
Solution: Save the string to a int variable
int kmp= atoi(lr_eval_string("{param1}"));
        lr_think_time(kmp);
lr_output_message("The parameter value is %s", lr_eval_string("{param1}"));
..
10. Convert Integer in to String
Action()
{
int i = 1001;
int ii = 1002;
char sVar1;
char sVar2;

// itoa: 1st pass an integer, next is string to contain integer as string, next is 10 for decimal or 16 for hex
itoa(i, sVar1, 10);
lr_save_string(sVar1, "sVar1");
lr_output_message(sVar1);
itoa(ii, sVar2, 10);
lr_save_string(sVar2, "sVar2");
lr_output_message(sVar2);

        return 0;
}

11.Generate Unique data thro script

#include "as_web.h"


Action()
{

        /* data record for the database */
        char sJobTitle;
        char *sHolder_JobTitle = "DBA";

        int iRand;
        char sRand;
        char xPrefix;

    srand(time(NULL)); // seed it always
   /* Generate some random number */
        iRand = (rand() % 1000);
        lr_output_message ("RandomNumber %d\n", iRand);

    /* convert number to string */
    if (!itoa(iRand, xPrefix, 10))
                lr_output_message("Cannot convert iRand to ascii char");
        else {
         lr_output_message("Converted value %s", xPrefix);
   }
       
        // create unique field //

        //strcpy(sJobTitle, xPrefix);
        strcpy(sJobTitle, xPrefix);
        strcat(sJobTitle, sHolder_JobTitle);
        lr_output_message ("Unique JobTitle --> %s\n", sJobTitle);
               
        return 0;
页: [1]
查看完整版本: LoadRunner:customC code