【问题标题】:Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval null响应消息:org.apache.jorphan.util.JMeterException:调用 bsh 方法时出错:eval null
【发布时间】:2017-04-12 07:06:10
【问题描述】:

在 jmeter bean shell 采样器中执行时,我们得到了如上的错误 提到了。

实际上代码在 Eclipse 中运行良好,但在 Jmeter 中实现时却无法运行。 谁能帮我们解决这个问题?

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.*;
import java.util.Map.*;
import java.util.Set;
public class JsonComparator {

public static void main(String[] args) throws Exception {
    JsonParser parser = new JsonParser();

    try{
        Gson g = new Gson();
        JsonElement jsonElement1 = parser
                .parse(new FileReader("E:\\InCites_UI\\trunk\\Tests\\Filters_ByPerson_People_PerfC.json"));
        JsonElement jsonElement2 = parser
                .parse(new FileReader("E:\\InCites_UI\\trunk\\Tests\\Filters_ByPerson_People_PerfA.json"));

        System.out.println("Is the two JSON File Same: "+compareJson(jsonElement1, jsonElement2));
        if(!compareJson(jsonElement1, jsonElement2)){
            Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
            Map<String,Object> firstMap = g.fromJson(jsonElement1, mapType);
            Map<String, Object> secondMap = g.fromJson(jsonElement2, mapType);
            System.out.println(JsonComparator.mapDifference(firstMap, secondMap));
        }
        else{
            System.out.println("The Two JSON Are SAME!!!!!!!!!!!!!!!");
        }

    }catch(Exception e1){
        e1.printStackTrace();
    }

}

public static <K, V> Map<K, V> mapDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
    Map<K, V> difference =  new HashMap<K, V>();
    difference.putAll(left);
    difference.putAll(right);
    difference.entrySet().removeAll(right.entrySet());
    return difference;
}

public static boolean compareJson(JsonElement json1, JsonElement json2) {
    boolean isEqual = true;
    ArrayList<Object> ls1 = new ArrayList<Object>();
     ArrayList<Object> ls2 = new ArrayList<Object>();
    // Check whether both jsonElement are not null
    if (json1 != null && json2 != null) {

        // Check whether both jsonElement are objects
        if (json1.isJsonObject() && json2.isJsonObject()) {
            Set<Entry<String, JsonElement>> ens1 = ((JsonObject) json1).entrySet();
            Set<Entry<String, JsonElement>> ens2 = ((JsonObject) json2).entrySet();
            JsonObject json2obj = (JsonObject) json2;
            if (ens1 != null && ens2 != null && (ens2.size() == ens1.size())) {
                // Iterate JSON Elements with Key values
                for (Entry<String, JsonElement> en : ens1) {
                    isEqual = isEqual && compareJson(en.getValue(),json2obj.get(en.getKey()));
                }
            } else {
                return false;
            }
        }
        // Check whether both jsonElement are arrays
        else if (json1.isJsonArray() && json2.isJsonArray()) {
            JsonArray jarr1 = json1.getAsJsonArray();
            JsonArray jarr2 = json2.getAsJsonArray();
            if (jarr1.size() != jarr2.size()) {
                return false;
            } else {
                int i = 0;
                // Iterate JSON Array to JSON Elements
                for (JsonElement je : jarr1) {
                    isEqual = isEqual && compareJson(je, jarr2.get(i));
                    i++;
                }
                if (isEqual) {
                    Object[] o1 = ls1.toArray();
                    Object[] o2 = ls2.toArray();
                    isEqual = Arrays.deepEquals(o1, o2);
                }
            }

        }

        // Check whether both jsonElement are null
        else if (json1.isJsonNull() && json2.isJsonNull()) {
            return true;
        }

        // Check whether both jsonElement are primitives
        else if (json1.isJsonPrimitive() && json2.isJsonPrimitive()) {
            ls1.add(json1);
            ls2.add(json2);
        }
    } else if (json1 == null && json2 == null) {
        return true;
    } else {
        return false;
    }
    return isEqual;
}

}

我们正在寻求这方面的帮助,是否有人可以使用此代码并尝试在 Jmeter 上工作?我们无法在 Jmeter 中实现。

实际上代码在 Eclipse 中运行良好,但在 Jmeter 中实现时却无法运行。

【问题讨论】:

  • 请不要张贴你的项目截图,请附上它自己的代码,这样更容易阅读和复制,以防有人想重现你的问题。

标签: java jmeter performance-testing beanshell


【解决方案1】:

Beanshell脚本故障排除技术有2种:

  1. debug(); 命令添加到脚本的开头 - 这样您将在 stdout 中获得有关测试流程的额外信息
  2. 将您的代码放入try block 中,例如:

    try {
        //your code here
    }
    catch (Throwable ex) {
        log.error("Beanshell failure", ex);
        throw ex;
    }
    

    采用这种方法,您将在 jmeter.log 文件中获得更多信息的堆栈跟踪

一般来说,我建议切换到JSR223 SamplerGroovy 语言。 Groovy has built-in JSON support 与 Beanshell 相比性能要好得多,请参阅 Groovy Is the New Black 文章了解详细信息。

以后不要将代码发布为图像,我认为不会有爱好者会深入研究这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多