2017-04-12 72 views
0

在jmeter bean shell采样器中执行时,我们得到如上所述的错误 提到。响应消息:org.apache.jorphan.util.JMeterException:调用bsh方法的错误:eval t null

实际上,代码在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中实现时不起作用。

+0

请不要张贴您的项目的截图,请包括自己的代码,所以它更容易阅读和复制,以防有人想重现您的问题。 – cbuchart

回答

1

有2种BeanShell的脚本故障排除方法:

  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文章。

未来不要发布代码作为图像,我不认为会有发烧友会挖掘到这一点。

相关问题