《LoadRunner 12七天速成宝典》—第2章2.6节第二个性能测试案例

2.6 第二个性能测试案例
云云:烤鱼吃得很爽。

恋恋:就是你非要吃香辣味的,害得我嘴巴都麻了。

云云:香辣味的好吃,就是鲶鱼吃得有点腻,能吃黑鱼就好了。

恋恋:下次多找几个人一起吃饭啊,这下他们生意就更好了。

云云:那么我问你一下点一样的鱼和点不同的鱼有什么区别?

恋恋:嘿,你又要考我啦,其实刚才你还没提问,我脑子里面就在想这个问题了。

云云:哦,美美狗开窍了?

恋恋:不但我是美美狗还是聪聪狗,你那点小花样我早看出来了。

云云:那么你说说,让我欣赏一下你的“光芒”。

恋恋:这个问题按照你的思路,我可以先从我公司门口的食堂说起,一般食堂都有固定的菜色,因为烧同样的东西处理能力强、成本低,而如果为每一个顾客单独炒菜,那么成本就会高很多。

云云:你这个跑题有点远啊!

恋恋:所以作为顾客,我们一般几个人都点相同的菜,这样厨子烧的快,无论从配菜,到烧菜都会并行处理。总的来说就是,如果每个人都要不同的东西,会让一个饭店很忙,处理能力降低,而如果要的东西类似,那么就会极大地提高处理效率,从而增加营业额。

云云:哎哟,貌似靠谱了。

恋恋:作为软件系统来说也是这样,如果每次对服务器发出的请求不同,那么服务器也会为每一个请求单独计算,从而会让服务器很忙,提高效率就是要让客户做的事情尽量相同,然后服务器就可以并行处理了。

云云:嗯,不错,不过有些不太专业。

恋恋:比如A和B做相同的请求,那么对于数据库来说查询的内容都相同,那么就可以只计算一次,然后内容就可以一次发给客户啦,就好像两个人都点宫保鸡丁炒饭一样,一次炒制,两盘出锅。

云云:虽然请求相同,但是可能因为业务不同而导致结果不同啊,比如A是管理员能看所有的记录,而B是普通用户只能看自己的记录!

恋恋:这个……

云云:那你觉得请求应该相同还是不相同呢?

恋恋:我觉得请求应该不同,因为你既然让我把脚本动起来,还给我说怎么做参数变量、处理业务,本质上就是要让每次输入的东西都不一样。

云云:没错,如果每次请求都一样,那么服务器会自动使用Cache机制,这也是一个使服务器提高处理能力的策略,当发现请求或者查询内容相同,系统会先从缓存(内存)中查找是否存在匹配的记录,如果有就返回,否则就执行一次,将结果存放入缓存,唯一特例就是所谓的要做及时查询,就是锁概念。

恋恋:嗯,我也听说过什么MemCache、PGA&SGA还有啥JVM内存管理,都是和缓存有关系,锁这个概念我就不太懂了。

云云:锁这个概念怎么说呢,这样吧,你知道12306买火车票难吧。

恋恋:知道啊,不知道谁做那么差个系统,查个火车票都经常刷不出来。

云云:这就叫做外行看热闹,内行看门道。其实做火车订票系统是很难的,因为查票是及时的,要锁定票。

恋恋:Go on!

云云:每当一张票被订的时候,所有的查询都要得到全新的少了一张票的情况,所有的订票都要告诉别人这个位置的这个票已经被订了。所以当成千上万的人去买票的时候,一张票被锁定会影响几万个查询,每次查询都不能用Cache,否则会得到错误的信息,你明明看到这个票有,但是订的时候却失败。

恋恋:Go on!

云云:这里面还有更复杂的业务,比如从上海到北京的高铁,如果我订了一张从南京到天津的票,就意味着会多一张上海到南京的票,还有一张从天津到北京的票!

恋恋:那么怎么优化呢?

云云:很简单,首先不要做及时查询,例如不要直接给每个客户看有多少张剩票,其次当一张票订了后,不要立即计算出可能导致生成的部分路程的票,最后将坐全程的票和坐半程的票位置分开做表分离,这样就算买了半程票影响的记录会比较少,处理起来相对简单,让专门的服务器去处理多程票!

恋恋:来吃个梨,你看又进入状态了吧,后面一个人就亢奋的说啊说啊,完全不管别人懂不懂。

云云:真是好心没有好报,看在你给我削梨的举动上就原谅你了。睡觉前是不是可以做第二个性能测试案例了啊。

恋恋:今天晚上要做啥啊?

云云:做一个脚本比较一下点击相同的帖子和点击不同的帖子的性能有何区别!

恋恋:好,开工。

**小结
理解动态访问会带来的负载点及系统处理业务的逻辑概念。
录制脚本运行
**
恋恋:打开LR启动VuGen录制一个脚本。

云云:别忘了你首先要有那么多帖子,否则你查询不到。

恋恋:对,那么先录一个生成帖子的脚本吧。

(几分钟过去后)

恋恋:脚本生成。

Action()
{
    int i;
    web_add_cookie("38We_2132_sid=NDgF6W; DOMAIN=127.0.0.1");
    web_add_cookie("38We_2132_lastvisit=1462200716; DOMAIN=127.0.0.1");
    web_add_cookie("38We_2132_lastact=1462204316%09home.php%09misc; DOMAIN=127.0.0.1");
    web_add_cookie("38We_2132_onlineusernum=1; DOMAIN=127.0.0.1");
    web_add_cookie("38We_2132_sendmail=1; DOMAIN=127.0.0.1");
    web_url("discuz",
        "URL=http://127.0.0.1/discuz/",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t2.inf",
        "Mode=HTML",
        EXTRARES,
            "Url=static/image/common/background.png", "Referer=http://127.0.0.1/discuz/forum. php", ENDITEM,
              "Url=static/image/common/search.gif", "Referer=http://127.0.0.1/discuz/forum.php", ENDITEM,
              "Url=static/image/common/chart.png", "Referer=http://127.0.0.1/discuz/forum.php", ENDITEM,
              "Url=static/image/common/px.png", "Referer=http://127.0.0.1/discuz/forum.php", ENDITEM,
              "Url=static/image/common/qmenu.png", "Referer=http://127.0.0.1/discuz/forum.php", ENDITEM,
              "Url=static/image/common/nv.png", "Referer=http://127.0.0.1/discuz/forum.php", ENDITEM,
              "Url=static/image/common/titlebg.png", "Referer=http://127.0.0.1/discuz/forum. php", ENDITEM,
        "Url=static/image/common/nv_a.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/cls.gif", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        LAST);
    web_submit_data("member.php",
        "Action=http://127.0.0.1/discuz/member.php?mod=logging&action=login&loginsubmit
              =yes&infloat=yes&inajax=1",
        "Method=POST",
        "TargetFrame=",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php",
        "Snapshot=t3.inf",
        "Mode=HTML",
        ITEMDATA,
        "Name=fastloginfield", "Value=username", ENDITEM,
        "Name=username", "Value=admin", ENDITEM,
        "Name=password", "Value=123456", ENDITEM,
        "Name=quickforward", "Value=yes", ENDITEM,
        "Name=handlekey", "Value=ls", ENDITEM,
        "Name=questionid", "Value=0", ENDITEM,
        "Name=answer", "Value=", ENDITEM,
        LAST);
    web_add_cookie("38We_2132_lastact=1462204476%09forum.php%09; DOMAIN=127.0.0.1");
    web_url("forum.php",
        "URL=http://127.0.0.1/discuz/forum.php",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php",
        "Snapshot=t4.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=uc_server/images/noavatar_small.gif", ENDITEM,
        "Url=static/image/common/user_online.gif", ENDITEM,
        "Url=static/image/common/arrwd.gif", ENDITEM,
        LAST);
    web_add_cookie("38We_2132_checkpm=1; DOMAIN=127.0.0.1");
    web_add_cookie("38We_2132_smile=1D1; DOMAIN=127.0.0.1");
    web_url("默认版块",
        "URL=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php",
        "Snapshot=t5.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=data/cache/style_1_forum_moderator.css?z69", "Referer=http://127.0.0.1/
              discuz/forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/smile.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/sad.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/cry.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/biggrin.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/shocked.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/huffy.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/shy.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/tongue.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/mad.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/titter.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/sweat.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/loveliness.gif", "Referer=http://127.0.0.1/
              discuz/forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/funk.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/lol.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/curse.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/hug.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/sleepy.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/time.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/dizzy.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/shutup.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/victory.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/kiss.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/call.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/smiley/default/handshake.gif", "Referer=http://127.0.0.1/
              discuz/forum.php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/pt_icn.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_r.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/fav.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_l.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/feed.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/pt_item.png", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/recyclebin.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/atarget.png", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/editor/editor.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/mdly.png", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/pollsmall.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=forumdisplay&fid=2", ENDITEM,
        LAST);
    web_add_cookie("38We_2132_editormode_e=1; DOMAIN=127.0.0.1");
    web_url("高级模式",
        "URL=http://127.0.0.1/discuz/forum.php?mod=post&action=newthread&fid=2",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "Snapshot=t6.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=static/image/common/card_btn.png", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=post&action=newthread&fid=2", ENDITEM,
        "Url=static/image/common/notice.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=post&action=newthread&fid=2", ENDITEM,
        "Url=static/image/common/upload.swf?site=/discuz/misc.php%3fmod=swfupload%26type
              =image%26fid=2&type=image&random=O2WM", "Referer=http://127.0.0.1/discuz/forum.
              php?mod =post&action=newthread&fid=2", ENDITEM,
        "Url=static/image/common/upload.swf?site=/discuz/misc.php%3fmod=swfupload%26fid
              =2&random=pm3E", "Referer=http://127.0.0.1/discuz/forum.php?mod=post&action=
              newthread&fid=2", ENDITEM,
        LAST);
    for(i=0;i<1000;i++)
    {
    web_submit_data("forum.php_2",
        "Action=http://127.0.0.1/discuz/forum.php?mod=post&action=newthread&fid=2&extra
              =&topicsubmit=yes",
        "Method=POST",
        "TargetFrame=",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php?mod=post&action=newthread&fid=2",
        "Snapshot=t7.inf",
        "Mode=HTML",
        ITEMDATA,
        "Name=formhash", "Value=27ad1fe9", ENDITEM,
        "Name=posttime", "Value=1462204484", ENDITEM,
        "Name=wysiwyg", "Value=1", ENDITEM,
        "Name=subject", "Value=第二天测试专用{topic}", ENDITEM,
        "Name=message", "Value=美美狗代表云层天咨发帖了{topic}", ENDITEM,
        "Name=save", "Value=", ENDITEM,
        "Name=uploadalbum", "Value=", ENDITEM,
        "Name=newalbum", "Value=", ENDITEM,
        "Name=readperm", "Value=", ENDITEM,
        "Name=price", "Value=", ENDITEM,
        "Name=usesig", "Value=1", ENDITEM,
        "Name=allownoticeauthor", "Value=1", ENDITEM,
        EXTRARES,
        "Url=uc_server/images/noavatar_middle.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=data/cache/style_1_forum_viewthread.css?z69", "Referer=http://127.0.0.1/
              discuz/forum.php?mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/js/forum_viewthread.js?z69", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=viewthread&tid=868&extra=", ENDITEM,
        LAST);
    }
    web_custom_request("home.php",
        "URL=http://127.0.0.1/discuz/home.php?mod=spacecp&ac=pm&op=checknewpm&rand
              =1462204515",
        "Method=GET",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php?mod=viewthread&tid=868&extra=",
        "Snapshot=t8.inf",
        "Mode=HTML",
        "EncType=application/x-www-form-urlencoded",
        EXTRARES,
        "Url=static/image/common/flbg.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/rec_add.gif", "Referer=http://127.0.0.1/discuz/forum.
              php? mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/oshr.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/rec_subtract.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/fastreply.gif", "Referer=http://127.0.0.1/discuz/ forum.
              php?mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/midavt_shadow.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/repquote.gif", "Referer=http://127.0.0.1/discuz/ forum.
              php?mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/edit.gif", "Referer=http://127.0.0.1/discuz/ forum.php?
              mod=viewthread&tid=868&extra=", ENDITEM,
        "Url=static/image/common/popupcredit_bg.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=868&extra=", ENDITEM,
        LAST);
    return 0;
}

恋恋:这里我用了一个循环,做了1000次。

云云:那么你发帖的那个参数是?

恋恋:{topic}啊,这是我定义的一个时间参数,这样每次帖子都不一样,如图2-23所示。

云云:哎哟,不错哦。

恋恋:好了,单击运行,我去给你削个梨。

云云:好!不过,如果你用场景,运行会快一些。

恋恋:好啊,你自己削梨去,EQ真低。

云云:我错了,还是你帮我削吧。

(几分钟后,帖子生成)如图2-24所示。

恋恋:接着我要录制一个用户随机访问帖子的脚本和一个用户访问一个固定帖子的脚本。

Action()
{
    web_url("discuz",
        "URL=http://127.0.0.1/discuz/",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t1.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=static/image/common/background.png", "Referer=http://127.0.0.1/discuz/forum. 
              php", ENDITEM,
        "Url=static/image/common/search.gif", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/nv.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/px.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/titlebg.png", "Referer=http://127.0.0.1/discuz/forum. 
              php", ENDITEM,
        "Url=static/image/common/nv_a.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/qmenu.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/chart.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        LAST);
    lr_think_time(5);
    web_url("默认版块",
        "URL=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php",
        "Snapshot=t2.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=static/image/common/pt_icn.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/pt_item.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arrwd.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/fav.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/feed.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_l.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_r.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/atarget.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/editor/editor.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        LAST);
    lr_think_time(4);
    web_url("第二天测试专用2016-05-03 00:09:10.850",
        "URL=http://127.0.0.1/discuz/forum.php?mod=viewthread&tid=1867&extra=page%3D1",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "Snapshot=t3.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=uc_server/images/noavatar_middle.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/oshr.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/rec_add.gif", "Referer=http://127.0.0.1/discuz/forum.
              php? mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/midavt_shadow.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/fastreply.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/rec_subtract.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/flbg.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/repquote.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        LAST);
    return 0;
}

恋恋:找到看帖请求的那个函数,然后将这个请求内容做一个参数化,参数名称叫作tid,并且设置从1~1000的随机数,如图2-25所示。

Action()
{
    web_url("discuz",
        "URL=http://127.0.0.1/discuz/",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t1.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=static/image/common/background.png", "Referer=http://127.0.0.1/discuz/forum.
              php", ENDITEM,
        "Url=static/image/common/search.gif", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/nv.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/px.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/titlebg.png", "Referer=http://127.0.0.1/discuz/forum.
              php", ENDITEM,
        "Url=static/image/common/nv_a.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/qmenu.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/chart.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        LAST);
    lr_think_time(5);
    web_url("默认版块",
        "URL=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php",
        "Snapshot=t2.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=static/image/common/pt_icn.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/pt_item.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arrwd.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/fav.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/feed.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_l.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_r.gif", "Referer=http://127.0.0.1/discuz/ forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/atarget.png", "Referer=http://127.0.0.1/discuz/forum.
              php? mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/editor/editor.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        LAST);
    lr_think_time(4);
    web_url("第二天测试专用2016-05-03 00:09:10.850",
        "URL=http://127.0.0.1/discuz/forum.php?mod=viewthread&tid={tid}&extra=page%3D1",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "Snapshot=t3.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=uc_server/images/noavatar_middle.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/oshr.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/rec_add.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/midavt_shadow.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/fastreply.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/rec_subtract.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/flbg.gif", "Referer=http://127.0.0.1/discuz/forum. php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/repquote.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        LAST);
    return 0;
}

恋恋:最后还要在查询前后加个事务,让脚本运行的时候能够统计响应时间,先来试着运行一下。

Action()
{
    web_url("discuz",
        "URL=http://127.0.0.1/discuz/",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t1.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=static/image/common/background.png", "Referer=http://127.0.0.1/discuz/forum.
              php", ENDITEM,
        "Url=static/image/common/search.gif", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/nv.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/px.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/titlebg.png", "Referer=http://127.0.0.1/discuz/forum.
              php", ENDITEM,
        "Url=static/image/common/nv_a.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/qmenu.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        "Url=static/image/common/chart.png", "Referer=http://127.0.0.1/discuz/forum.php",
              ENDITEM,
        LAST);
    lr_think_time(5);
    web_url("默认版块",
        "URL=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php",
        "Snapshot=t2.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=static/image/common/pt_icn.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/pt_item.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arrwd.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/fav.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/feed.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_l.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/arw_r.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/common/atarget.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        "Url=static/image/editor/editor.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=forumdisplay&fid=2", ENDITEM,
        LAST);
    lr_think_time(4);
    lr_start_transaction("viewtopic");
    web_url("第二天测试专用2016-05-03 00:09:10.850",
        "URL=http://127.0.0.1/discuz/forum.php?mod=viewthread&tid={tid}&extra=page%3D1",
        "TargetFrame=",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=http://127.0.0.1/discuz/forum.php?mod=forumdisplay&fid=2",
        "Snapshot=t3.inf",
        "Mode=HTML",
        EXTRARES,
        "Url=uc_server/images/noavatar_middle.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/oshr.png", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/rec_add.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/midavt_shadow.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/fastreply.gif", "Referer=http://127.0.0.1/discuz/ forum.
              php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/rec_subtract.gif", "Referer=http://127.0.0.1/discuz/
              forum.php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/flbg.gif", "Referer=http://127.0.0.1/discuz/forum.php?
              mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        "Url=static/image/common/repquote.gif", "Referer=http://127.0.0.1/discuz/forum.
              php?mod=viewthread&tid=1867&extra=page%3D1", ENDITEM,
        LAST);
lr_end_transaction("viewtopic", LR_AUTO);
    return 0;
}

恋恋:代码运行大功告成!

云云:值得表扬!

(分别运行未参数化和参数化过的脚本)。

恋恋:好像查询条件是随机的会慢一点。

云云:Go On。

恋恋:OK,搞定两个脚本,一个是原脚本,查询条件不变的;还有一个是查询条件是随机的内容,接着分别到场景里面去运行一下。

恋恋:场景里面要添加监控内容(Windows资源),单击开始运行。

时间: 2025-01-19 09:12:29

《LoadRunner 12七天速成宝典》—第2章2.6节第二个性能测试案例的相关文章

《LoadRunner 12七天速成宝典》目录—导读

版权 LoadRunner 12七天速成宝典 • 著 陈 霁 责任编辑 张 涛 • 人民邮电出版社出版发行 北京市丰台区成寿寺路11号 邮编 100164 电子邮件 315@ptpress.com.cn 网址 http://www.ptpress.com.cn • 读者服务热线:(010)81055410 反盗版热线:(010)81055315 内容提要 本书以生动的情境对话方式,通过本书主角云云教恋恋学习性能测试的故事,诙谐幽默地把性能测试学习中的各个难点用简单的生活案例讲解出来,是学习性能测

《LoadRunner 12七天速成宝典》—第1章 1.5节安装LoadRunner 12.5

1.5 安装LoadRunner 12.5云云:安装LoadRunner 12.5没什么特别的,下载的文件是一个EXE可以执行的压缩包,运行之后会等待解压安装文件,然后出现安装环境准备. 单击"确定"会依次安装相关组件,注意环境中包括了32位的JRE环境,如图1-28所示. 安装组件结束后,稍等片刻后就会弹出安装主界面,单击"下一步",如图1-29所示. 选择"我接受许可协议中的条款",单击"下一步",如图1-30所示. 确认

《LoadRunner 12七天速成宝典》—第1章 1.1节开始

第一天1.1 开始恋恋:教我性能测试吧!云云:怎么突然对性能测试感兴趣? 恋恋:感兴趣就感兴趣,还需要什么理由啊! 云云:一定是有啥原因,老实交代! 恋恋:昨天有猎头找我,有一家很不错的外企在招聘性能工程师,你也了解我,平常都在做功能测试,但是性能测试是短板,你是专家,当然请教你. 云云:原来如此,那专门为你设计一个七日速成法吧,应该可以及时赶上面试,不过这七天可是"非人"的待遇哦,要做好心理准备. 恋恋:行啦,我知道你有办法的,快点开始吧,中午给你做你最爱吃的红烧肉. 云云:你对性能

《LoadRunner 12七天速成宝典》—第2章2.3节理解代码

2.3 理解代码云云:接着要给你讲一下怎样理解代码录制出来的原理.LoadRunner是一个基于协议的工具,它能够使脚本录制成功的关键在于它能识别协议,当它捕获到该协议后,会尝试对其进行解析,转化为自己的C语言脚本. 恋恋:岂不是我还要学习C语言? 云云:从某些角度来说确实需要,不过现在是速成么,我尽量跳过这些麻烦的东西帮你做最常见的一些棘手的问题. 恋恋:嗯,我赞成. 云云:对于LoadRunner来说录制设置很重要,首先我来给你介绍一下怎么设置录制选项. 恋恋:来吧,时刻准备着! 云云:对于

《LoadRunner 12七天速成宝典》—第2章2.2节解决乱码

2.2 解决乱码 恋恋:那今天你说点什么难的呢? 云云:首先,我要给你说一下怎么确保录制出来的东西不会是乱码.还记得昨天我们录制出来的发帖操作内容是乱码么? 恋恋:记得,那么怎么解决呢? 云云:其实这里和编码规则有关系,你先用IE 11浏览器(不要用Windows 10的Edge浏览器)打开我们搭建的Discuz论坛,在页面上单击右键,在出现的菜单里面选择"编码"命令,如图2-1所示. 这里该页面的编码格式是UTF-8. 恋恋:那么什么是UTF-8呢? 云云:UTF-8其实是一种多语言

《LoadRunner 12七天速成宝典》—第2章2.4节让代码动起来

2.4 让代码动起来 恋恋:师傅,师傅,接着我们应该干嘛了? 云云:嗯,在搞定编码之后我可以给你说怎样让脚本动起来了. 恋恋:动起来? 云云:就是变量化操作或者直接叫做参数化吧. 恋恋:似曾相识. 云云:这样说吧,你先录制一个用户注册后发帖的脚本,然后回放一下,看看结果. 恋恋开始录制注册脚本,录制的结果如下 Action() { web_url("discuz", "URL=http://127.0.0.1/discuz/", "TargetFrame=

《LoadRunner 12七天速成宝典》—第1章 1.2节环境搭建之虚拟机系统

1.2 环境搭建之虚拟机系统 云云:这里我要给你介绍两种虚拟机,一个比较常见的VMware还有一个是Hyper-V,这两个各有优点. VMware是一个"虚拟PC"软件,它的产品可以使你在一台机器上同时运行两个或更多Windows.DOS.Linux系统.与"多启动"系统相比,VMware采用了完全不同的概念."多启动"系统在一个时刻只能运行一个系统,在系统切换时需要重新启动机器.VMware是真正"同时"运行,多个操作系统在

《LoadRunner 12七天速成宝典》—第2章2.5节参数和变量

2.5 参数和变量云云:接着来说一下关于编程实现的方式,编程实现有两条路径:一个是走纯变量,另一个是走变量转参数. 恋恋:变量和参数有什么区别来着? 云云:前面不是说过么,参数是特殊的变量,参数是LR(LoadRunner的缩写)级别的,变量是C级别的.变量是属于语言体系的,所以它需要符合C语言或者当前脚本语言体系的基本语法规范,比如: Action() { int c; char x[100]; char *y; c=10; strcpy(x,"abc"); y="aya

《LoadRunner 12七天速成宝典》—第1章 1.3节环境搭建之虚拟机配置

1.3 环境搭建之虚拟机配置云云:接着给你介绍一下怎么配置虚拟机,以后你就可以自己操作了,也不用总让我帮你重装系统了. 恋恋:你说我笨好了. 云云(无语). 恋恋:伤自尊了! 云云(继续无语). 恋恋(一气之下去洗碗去了). 云云:讲个笑话给你吧,别生气了,你知道变形金刚里面擎天柱为什么变形那么慢? 恋恋:电影呗,不就是给你这种宅男看的特效吗? 云云:不对,因为它没有用美孚一号. 恋恋(不语). 云云:打开VMware,单击"创建新的虚拟机",弹出向导对话框,如图1-12所示. 这里选

《LoadRunner 12七天速成宝典》—第2章2.1节开始

第二天2.1 开始 恋恋:好困啊,昨天一个晚上都在想性能测试,那么多概念,那么多名词,那么多菜单. 云云:刚开始入门压力当然是很大的喽,不过你那么聪明,换成别人早就被我修理一百遍了.今天要开始第二天了,不认真可会掉队的,昨天的只是开始呢. 恋恋(认真状):是老师! 云云:昨天我们学习了最简单的脚本录制.回放验证.添加监控.设置多用户负载和收集结果.这是所有性能测试在执行部分都要做到的几个关键步骤,很多人在刚接触性能测试时都觉得性能测试很神秘,但是在了解了上面的东西之后,就会觉得性能测试很简单.