How do I rewrite the LoadRunner function?

Source: Internet
Author: User

Refer:

Http://ptfrontline.wordpress.com/2010/06/22/overriding-lr-functions-with-custom-code/

Most people do not know that LR supports overriding existing functions with custom ones in a very easy way.

I had a situation where I had a pre-made script that had fixed think-time values for the lr_think_time () CILS, and they of course were completely wrong. so my problem was that I had a lot of lr_think_time () places and I didn't want to go through them all myself.

In most cases a search-and-replace wocould do the trick, but I stumbled upon the overriding of functions and implemented it that way instead, making my changes to the script stay in one place only instead of manipulating all the actions in the script.

Remember: do not call the overridden function inside the new code !!! -You'll just end up with an endless loop, and terminate in stack-errors or similar...

Here's an empty script where I 've ve overridden the lr_think_time () function with a custom one:

01 #define lr_think_time my_think_time  // This define overrides lr_think_time() with my_think_time()
02   
03 double my_think_time(double sec)
04 // This simulates the 50% to 150% think-time randomness ...
05 {
06     double x;
07   
08     x = ((50.0+rand()%100)/100)*sec; // 50% to 150% randomness
09   
10     lr_force_think_time( x ); // force the think time (this is undocumented command)
11   
12     return x; // return how many seconds we waited
13 }
14   
15 ///////////////////////////////////////////////////////////////////////////////
16   
17 Action()
18 {
19     lr_think_time(10); // This will use the my_think_time() function !!
20   
21     return 0;
22 }

One cocould get creative and start overriding all sorts of functions with this. an example wocould be to set specific headers for specific web_url () calldepending on the Params. you cocould override the web_url () call, examine the parameters and add any needed HTTP headers before doing a web_custom_request () instead ..

Enjoy!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.