1. 程式人生 > >在LoadRunner指令碼中實現隨機ThinkTime

在LoadRunner指令碼中實現隨機ThinkTime

一般情況下,我們都是通過Run-Time Settings來設定Think Time(思考時間),可以設定回放指令碼時忽略思考時間,或者是設定回放隨機的一段思考時間。

By default, when you run a Vuser script, the Vuser uses the think time values that were recorded into the script during the recording session. VuGen allows you to use the recorded think time, ignore it, or use a value related to the recorded time:

Ignore think time. Ignore the recorded think time—replay the script ignoring all lr_think_time functions. Replay the think time. The second set of think times options let you use the recorded think time: As recorded. During replay, use the argument that appears in the lr_think_time function. For example, lr_think_time(10)
waits ten seconds. Multiply recorded think time by. During replay, use a multiple of the recorded think time. This can increase or decrease the think time applied during playback. For example, if a think time of four seconds was recorded, you can instruct your Vuser to multiply that value by two, for a total of eight seconds. To reduce the think time to two seconds, multiply the recorded time by 0.5. Use random percentage of the recorded think time.
Use a random percentage of the recorded think time. You set a range for the think time value by specifying a range for the think time. For example, if the think time argument is 4, and you specify a minimum of 50% and a maximum of 150%, the lowest think time can be two (50%) and the highest value six (150%). Limit think time to. Limit the think time's maximum value.

除此之外,我們還可以在VU指令碼中編寫隨機函式設定隨機思考時間:

Action()
{

// … your code

 lr_think_time(fRandInteger(3, 12));

// … more of your code

 return 0;
}

/*---- BEGIN Function fRandInteger --------------------------------------------------------*/
/* Send 2 arguments - minimum and maximum.
   Output to caller is a random integer anywhere in the specified range, inclusive.*/
int fRandInteger(minInt, maxInt)
{
 int rndInt, offset;
 if (maxInt == 0)
 return maxInt; // Caller must handle a 0 in order to prevent error.
 offset = minInt;
 rndInt =   ((minInt) + rand() % (maxInt - offset + 1));

 /* Examples:
 1. lr_think_time(fRandInteger(3, 12));
 2. rndVal = fRandInteger(1, 9);
 3. rndVal = fRandInteger(1, CALLED_UFC_FIGHTER_A_WUSS_IN_PERSON); */

 return rndInt;

}
/*---- END Function fRandInteger ------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/

fRandInteger函式用於取某個區間中的一個隨機整數。

參考: