JMeter内置变量大揭秘:含义,用法和实例-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

JMeter内置变量大揭秘:含义,用法和实例

在JMeter中,有一些内置的变量,可以帮助我们在测试过程中存储和使用一些数据。这些内置变量有四种:vars,props,prev和sample。

vars变量

vars变量是JMeterVariables类的一个实例,它是一个Map类型的对象,可以存储String或Object类型的数据。vars变量的作用域是当前线程组,也就是说,只有同一个线程组内的线程才能访问和修改同一个vars变量。如果不同的线程组需要共享数据,就不能使用vars变量。

vars变量的使用方法很简单,我们可以在BeanShell Sampler或JSR223 Sampler中使用以下语法来获取和设置vars变量:

// JSR233  groovy 脚本
//获取vars变量
String value = vars.get("key");
Object obj = vars.getObject("key");

//设置vars变量
vars.put("key", "value");
vars.putObject("key", new Object());

直接使用${key}来引用vars变量的值。

vars变量的一个常见用途是保存上一个请求的响应数据,以便后续请求使用。例如,我们可以在 JSR233PostProcessor中使用以下代码的一些实例:

又比如从 csv 文件中读取数据,并保存到一个list对象中:

然后从其他取样器中使用这个对象:

props变量

props变量是JMeterProperties类的一个实例,它是一个Hashtable类型的对象,也可以存储String或Object类型的数据。props变量的作用域是全局的,也就是说,所有的线程组都可以访问和修改同一个props变量。如果不同的线程组需要共享数据,就可以使用props变量。

props变量的使用方法和vars变量类似,我们可以在BeanShell Sampler或JSR223 Sampler中使用以下语法来获取和设置props变量:

//获取props变量
String value = props.get("key");
Object obj = props.get("key");

//设置props变量
props.put("key", "value");
props.put("key", new Object());

我们也可以在其他元件中使用${__P(key)}来引用props变量的值。

props变量的一个常见用途是保存一些全局配置参数,例如服务器地址,端口号等。例如,我们可以在Test Plan中使用User Defined Variables元件来定义一些props变量:

然后,在其他地方,我们可以使用${__P(server)}来引用服务器地址。

prev变量

prev变量是SampleResult类的一个实例,它是一个对象,可以存储上一个请求(或者说上一个取样器)的结果信息。prev变量的作用域是当前线程组,并且只能在后置处理器(PostProcessor)或断言(Assertion)中使用。

prev变量的使用方法是在BeanShell PostProcessor或JSR223 PostProcessor中使用以下语法来获取prev变量:

SampleResult prev = ctx.getPreviousResult();

然后,我们可以调用prev对象的各种方法来获取结果信息,例如:

prev变量的一个常见用途是对上一个请求的结果进行处理或判断。例如,我们可以在BeanShell Assertion中使用以下代码来判断响应码是否为200:

SampleResult prev = ctx.getPreviousResult();
if (!"200".equals(prev.getResponseCode())) {
    AssertionResult result = new AssertionResult("检查检查状态码");
    result.setFailure(true);
    result.setFailureMessage("响应状态码鬼知道是啥,反正不是 200");
    prev.addAssertionResult(result);
    prev.setSuccessful(false);
}

当前了,你要是直接在jsr233或者beanshell中直接如下那样写,也不会出现错误,也能直接使用。

//jsr233 中的代码
def responseCode = prev.getResponseCode();

sample变量

sample变量是SampleEvent类的一个实例,它是一个对象,可以存储当前请求(或者说当前取样器)的事件信息。sample变量的作用域是当前线程组,能在监听器(Listener)中使用。

sample变量的使用方法是在BeanShell Listener或JSR223 Listener中使用以下语法来获取sample变量:

SampleEvent sample = ctx.getCurrentSampleEvent();

然后,我们可以调用sample对象的各种方法来获取事件信息,例如:

//获取取样器结果
SampleResult result = sample.getResult();
//获取线程名称
String threadName = sample.getThreadName();
//获取线程组名称
String threadGroupName = sample.getThreadGroup();
//获取主机名称
String hostName = sample.getHostname();

sample变量的一个常见用途是对当前请求的事件进行处理或记录。例如,我们可以在BeanShell Listener中使用以下代码来打印事件信息:

SampleEvent sample = ctx.getCurrentSampleEvent();
log.info("Thread name: " + sample.getThreadName());
log.info("Thread group: " + sample.getThreadGroup());
log.info("Host name: " + sample.getHostname());
log.info("Sampler name: " + sample.getResult().getSampleLabel());
log.info("Response time: " + sample.getResult().getTime());
log.info("Response code: " + sample.getResult().getResponseCode());
log.info("Response data: " + new String(sample.getResult().getResponseData()));

内置变量的区别

从上面的介绍可以看出,JMeter内置变量有以下几个区别:

  • vars和props都是Map类型的对象,可以存储和修改数据;prev和sample都是普通对象,只能读取数据。
  • vars和props都可以在任何地方引用;prev只能在后置处理器或断言中引用;sample只能在监听器中引用。
  • vars和props都需要手动设置和获取;prev和sample都由JMeter自动提供。
  • vars只能在当前线程组内共享;props可以跨线程组共享;prev和sample只能在当前线程内访问。
  • vars和props都只能存储String或Object类型;prev和sample都包含多种类型的数据。

内置变量的实际工作场景

来看一些实际工作场景:

场景一:我们需要模拟用户上传文件,并且每个用户都要上传不同的文件。这时候,我们就可以使用JSR223 PreProcessor元件来动态生成一个文件名,并将它保存到props变量中。然后,在上传文件请求中,我们就可以使用${__P(filename)}来引用文件名。

//在JSR223 PreProcessor的代码如下:
import java.util.UUID
//生成一个随机的UUID作为文件名
String filename = UUID.randomUUID().toString() + ".txt"
//将文件名保存到props变量中
props.put("filename", filename)

场景二:我们需要对每个请求的响应时间进行判断,如果超过了预期的时间,就要记录下来。这时候,我们就可以使用JSR223 Assertion元件来获取prev变量,并调用getTime()方法来获取响应时间。然后,我们就可以使用if语句来判断响应时间是否超过了预期,并使用log.info()方法来记录日志。

//在JSR223 Assertion 中的代码如下:
def prev = ctx.getPreviousResult()
//获取响应时间
def responseTime = prev.getTime()
//设置预期时间为1000毫秒
def expectedTime = 1000
//判断响应时间是否超过预期
if (responseTime > expectedTime) {
    // 这里建议将结果写入 csv 以便持久化查看
    log.info("响应时间是:${responseTime} ms, 预期时间是:${expectedTime} ms")
}

场景三:我们需要对每个请求的响应数据进行处理,如果包含了某些关键字,就要提取出来,那么则如下:

//在JSR223 PostProcessor 中的代码如下:
def prev = ctx.getPreviousResult()
def responseData = prev.getResponseDataAsString()
// 使用正则处理数据
def regex = /(.*?)<\/title>/
def matcher = regex.matcher(responseData)
if (matcher.find()) {
    def keyword = matcher.group(1)
    vars.put("keyword", keyword)
}
// 当然除了上述代码外,也可以直接使用正则匹配元件去处理。</code></pre><p>场景四:我们需要对每个请求的事件信息进行记录,例如线程名称,线程组名称,主机名称等,那么则如下:</p><pre><code>//在JSR223 Listener 中的代码
def sample = ctx.getCurrentSampleEvent()
def result = sample.getResult()
//打印事件信息到日志文件中,建议处理到csv(不过会有一点性能开销)
log.info("线程名称: " + sample.getThreadName())
log.info("线程组名: " + sample.getThreadGroup())
log.info("域名地址: " + sample.getHostname())
log.info("取样器名: " + result.getSampleLabel())
log.info("响应时间: " + result.getTime())
log.info("响应编码: " + result.getResponseCode())
log.info("响应数据: " + new String(result.getResponseData()))</code></pre>            <br>
            文章标题:JMeter内置变量大揭秘:含义,用法和实例            <br>
            网站路径:<a href="http://kswsj.cn/article/dhipeih.html">http://kswsj.cn/article/dhipeih.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/cccocpj.html">微站是什么,消防微站是什么东西</a>
                </li><li>
                    <a href="/article/cccosds.html">设置伪关机,如何能使手机假死机个把月</a>
                </li><li>
                    <a href="/article/cccosip.html">phpcms如何升级</a>
                </li><li>
                    <a href="/article/cccosgd.html">移动基站位置数据库的重要性与应用(移动基站位置数据库)</a>
                </li><li>
                    <a href="/article/cccospd.html">境外vps主机租用怎么搭建网站</a>
                </li>        </ul>
    </div>
</div>
<div class="line"></div>
<!--底部-->
<footer id="5">
    <div class="foot1 container">
        <div class="list">
            <div class="item">
                <a href="javascript:;">
                    <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span>
                    <strong>关注我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>索要报价</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>我要咨询</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>找到我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>投诉建议</strong>
                </a>
            </div>
        </div>
        <div class="tel">
            <dl>
                <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br>
                <span>也许您需要专业的服务,欢迎来电咨询</span>
            </dl>
            <dl>
                <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br>
                <span>您的需求,是我们前进的动力</span>
            </dl>
        </div>
    </div>
    <div class="friend">
        <div class="container">
            <span class="tit">友情链接:</span>
            <div class="inner">
                <a href="https://www.cdcxhl.com/link/" target="_blank">卖友情链接</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">成都手机网站建设公司</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音运营公司</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">免费收录网站</a><a href="https://www.cdcxhl.com/qiye.html" target="_blank">成都企业网站建设</a><a href="https://www.cdcxhl.com/google.html" target="_blank">Google优化</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">响应式网站建设公司</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站收录</a><a href="https://www.cdcxhl.com/google.html" target="_blank">谷歌优化排名</a><a href="https://www.cdcxhl.com/" target="_blank">创新互联建站</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">营销网站建设公司</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器托管机柜</a><a href="https://www.cdcxhl.com/cloud/" target="_blank">成都云服务器租用</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">响应式网站建设</a><a href="https://www.cdcxhl.com/app.html" target="_blank">app软件开发</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文营销</a><a href="https://www.cdcxhl.com/xiaochengx.html" target="_blank">微信小程序开发公司</a><a href="https://www.cdcxhl.com/h5.html" target="_blank">成都h5网站建设</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="https://www.cdcxhl.com/" target="_blank">成都网站建设</a><a href="http://www.cdkjz.cn/fangan/jianshe/" target="_blank">网站建设报价方案</a><a href="http://seo.cdkjz.cn/mobile/" target="_blank">手机网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="http://www.cdfuwuqi.com/" target="_blank">服务器托管机房</a><a href="https://www.cdcxhl.com/cqtuoguan.html" target="_blank">重庆服务器托管</a><a href="https://www.cdcxhl.com/idc/yaan.html" target="_blank">雅安服务器托管</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="https://www.cdcxhl.com/mobile.html" target="_blank">手机网站制作</a><a href="http://www.cqcxhl.com/" target="_blank">重庆网站制作</a><a href="http://www.cxjianzhan.com/mobile/" target="_blank">wap网站制作</a>            </div>
            <div class="footNav">
                <h3>企业服务</h3>
                <a href="https://www.cdcxhl.com/service/zzgj.html" target="_blank">备案资质管家服务</a><a href="https://www.cdcxhl.com/service/400.html" target="_blank">400电话</a><a href="https://www.cdcxhl.com/link/" target="_blank">友情链接出售</a>            </div>
            <div class="fr ecode">
                <div class="fl">
                    <img src="/Public/Home/img/ewm.jpg">
                    <p>关注企业微信</p>
                </div>
                <div class="fr slogan">
                    <p class="icon">
                        <a class="ph" href=""><i class="iconfont"></i></a>
                        <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a>
                    </p>
                    <p>
                        <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、
                        <a href="">营销型网站建设</a>、<a href="">网站建设</a>、
                        <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="bottom container">
            <p class="fl">
                版权所有:成都创新互联科技有限公司
                备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a>
                服务热线:028-86922220
            </p>
            <p class="fr">
                <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:
                <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a>
            </p>
        </div>
    </div>
</footer>
<!--在线咨询-->
<div class="fot">
    <ul>
        <li>
            <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank">
                <img src="/Public/Home/img/fot1.png" alt="">
                <p>在线咨询</p>
            </a>
        </li>
        <li>
            <a href="tel:18980820575" target="_blank">
                <img src="/Public/Home/img/fot2.png" alt="">
                <p>拨打电话</p>
            </a>
        </li>
    </ul>
</div>
</body>
</html>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>