如何在JSR223的Groovy类中使用JMeter的内部方法(vars.get()等)

时间:2019-06-28 04:23:22

标签: groovy jmeter jsr223

我在JSR223预处理器中这样编写Groovy脚本

import groovy.json.JsonSlurper;

class jmeter {

   
    def parsingJSON(String fileName){
        def payload = new File(fileName).getText();
        def jsonSlurper = new JsonSlurper()
        def object = jsonSlurper.parseText(payload)

        vars.put("myVar",  object.event_id)

        return object.event_id
    }
}

		def running = new jmeter()
        running.parsingJSON( "C:/Users/payload.json")

这将返回异常

ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor Dummy javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: vars for class: jmeter

但是当我删除类时,它看起来像这样

import groovy.json.JsonSlurper;

    def parsingJSON(String fileName){
        def payload = new File(fileName).getText();
        def jsonSlurper = new JsonSlurper()
        def object = jsonSlurper.parseText(payload)

        vars.put("myVar",  object.event_id)

        return object.event_id
    }
    
        parsingJSON( "C:/Users/payload.json")

它将运行没有任何问题。

我的问题:我了解vars是JMeter内部API,但是我们不能在Groovy脚本中使用JSR223 Sampler / PreProcessor中的vars.put()在Groovy脚本中使用vars.get()class{} JMeter?

[获取输入信息后,在19年7月1日更新]:

我用Intellij写代码,看起来像这样。它运行得很好。

import groovy.json.JsonSlurper
import org.apache.jmeter.threads.JMeterVariables


class jmeter {

    JMeterVariables vars;
    jmeter(JMeterVariables vars) {
        this.vars = vars;
    }

    def parsingJSON(String fileName){
        def payload = new File(fileName).getText();
        def jsonSlurper = new JsonSlurper()
        def object = jsonSlurper.parseText(payload)

        vars.put("myVar",  object.event_id)
        println("My Var " + vars.get("myVar"))

        return object.event_id
    }
}


class runTest {
    static void main(String[] args) {
        JMeterVariables vars = new JMeterVariables();
        def running = new jmeter(vars)
        running.parsingJSON( "C:/Users/payload")
    }
}

代码基本上将值放入参数myVar,因此我可以在HTTP Request Sampler中使用它。 我将其复制粘贴到JMeter JSR223 PreProcessor,然后创建HTTP Request采样器,并将$ {myVar}放入主体数据

enter image description here

我希望“ myVar”的值会反映在“身体数据”中。但是运行它后,在结果树侦听器中看到参数$ {myVar}而不是值。

enter image description here

[从Dmitry获得输入后更新,2019年7月3日]

我使用IntelliJ并编写如下代码

import groovy.json.JsonSlurper
import org.apache.jmeter.threads.JMeterVariables

class Global {
    static vars = [:]
}

Global.vars.jmeterVars = vars



class jmeter {

        def parsingJSON(String fileName){
        def payload = new File(fileName).getText();
        def jsonSlurper = new JsonSlurper()
        def object = jsonSlurper.parseText(payload)

        Global.vars.jmeterVars.put("myVar",  object.event_id)  //This will return java.lang.NullPointerException: Cannot invoke method put() on null object
        println("My Var " + vars.get("myVar"))

        return object.event_id
    }
}


class runTest {
    static void main(String[] args) {

        def running = new jmeter()
        running.parsingJSON( "C:/Users/payload")

    }
}

这一次甚至IntelliJ都向我返回错误

Exception in thread "main" java.lang.NullPointerException: Cannot invoke method put() on null object at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91) at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:43) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:34) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135) at jmeter.parsingJSON(jmeter.groovy:21) at jmeter$parsingJSON.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127) at runTest.main(jmeter.groovy:38) 基本上是由Global.vars.jmeterVars.put("myVar", object.event_id)

引起的

[更新]:2019年7月17日,来自user7294900的输入

我在IntelliJ中编写了这样的代码

import groovy.json.JsonSlurper
import org.apache.jmeter.threads.JMeterVariables


class jmeter2 {

    JMeterVariables vars;

    jmeter2(JMeterVariables vars) {
        this.vars = vars;
    }

    public addvar(String VarName, String value) {
        vars.put(VarName, value);
    }


    def parsingJSON(String fileName) {
        def payload = new File(fileName).getText();
        def jsonSlurper = new JsonSlurper()
        def object = jsonSlurper.parseText(payload)

        addvar("myVar", object.event_id)
        println("My Var " + vars.get("myVar"))

        return object.event_id
    }
}


class runTest {
    static void main(String[] args) {
        
        JMeterVariables vars = new JMeterVariables();
        def running = new jmeter2(vars)
        running.parsingJSON( "C:/Users/payload.json")

    }
}

它在Intellij中工作得很好。我只需要向Intellij添加一些jar依赖项(slf4j-simple-1.7.2apachejmeter_corelogkit-1.2.2)。 IntelliJ将返回结果。到目前为止,一切都很好。

我通过创建JSR223 samplerdebug samplerResults Tree listener将代码复制粘贴到JMeter。

在侦听器中看不到JSR223采样器创建的任何myVar

enter image description here

enter image description here

JMeter日志也没有显示任何问题

2019-07-17 12:33:15,806 INFO o.a.j.s.SampleEvent: List of sample_variables: [] 2019-07-17 12:33:15,806 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*) 2019-07-17 12:33:15,812 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group 2019-07-17 12:33:15,812 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group. 2019-07-17 12:33:15,812 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error 2019-07-17 12:33:15,812 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false 2019-07-17 12:33:15,813 INFO o.a.j.t.ThreadGroup: Started thread group number 1 2019-07-17 12:33:15,813 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started 2019-07-17 12:33:15,813 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1 2019-07-17 12:33:15,825 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1 2019-07-17 12:33:15,825 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1 2019-07-17 12:33:15,825 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2019-07-17 12:33:15,825 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

文件 payload.json 仅包含一个非常简单的json

{
"event_id": "01DE95CRARFQ4X9WEKXACQYHVX",
"event_type": "form_response"
}

我不确定JMeter为什么不喜欢该脚本。 有什么想法吗?谢谢。

2 个答案:

答案 0 :(得分:0)

如果要保持代码结构的最少更改,请考虑引入一个单独的类,该类包含静态变量,可在Groovy脚本中的任何位置访问。

类似的东西:

import groovy.json.JsonSlurper

class Global {
    static vars = [:]
}

Global.vars.jmeterVars = vars

class jmeter {


    def parsingJSON(String fileName) {

        def payload = new File(fileName).getText();
        def jsonSlurper = new JsonSlurper()
        def object = jsonSlurper.parseText(payload)

        Global.vars.jmeterVars.put("myVar", object.event_id)

        return object.event_id
    }
}

def running = new jmeter()
running.parsingJSON("C:/Users/payload.json")

更多信息:

答案 1 :(得分:0)

如果要添加变量,则需要将当前的JMeterVariables对象传递给jmeter对象,

以下代码将变量aaa添加到现有的JMeter变量

import org.apache.jmeter.threads.JMeterVariables;
class jmeter {
      JMeterVariables vars;
      jmeter(JMeterVariables vars) {
         this.vars = vars;
      }
public addvar() {
    vars.put("aaa","bbb");
}
}

def running = new jmeter(vars);
running.addvar();

因此Intellij识别出JMeterVariables,您将不得不从JMeter的lib \ ext文件夹中添加ApacheJMeter_core.jar