JMeter Groovy语言抛出“无法解析类”,但是beanshell或java都可以

时间:2018-08-07 15:57:51

标签: groovy jmeter

我正在使用JMeter 4.0。以下是我在JSR223采样器中拥有的一些代码。它会在目录中查找并找到特定文件。

//This looks in the test results folder and locates
//the most recent file with the testReport prefix we are looking for

import org.apache.commons.io.FileUtils; 
import org.apache.commons.io.filefilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.io.comparator.LastModifiedFileComparator;

//show some logging in the jmeter log and in the jenkins console
log.info("=== get Test Results File to Upload ===");
OUT.println("=== get Test Results File to Upload ===");

//put the test_results_path property in a string to make it easier to work with
String dir_path = props.get("test_results_path");
log.info("=== test_results_path === "+dir_path);
OUT.println("=== test_results_path === "+dir_path);
String rpt_pref = props.get("testReport_prefix");
//log.info("=== testReport_prefix === "+rpt_pref);
//OUT.println("=== testReport_prefix === "+rpt_pref);
String rpt_suff = props.get("testReport_suffix");
//log.info("=== testReport_suffix === "+rpt_suff);
//OUT.println("=== testReport_suffix === "+rpt_suff);

//define an empty file
File theNewestFile = null;
try {
   File dir = new File(dir_path);
   //log.info("=== file directory === "+dir);   
   //OUT.println("=== file directory === "+dir);   
   FileFilter fileFilter = new WildcardFileFilter(""+rpt_pref+"*."+rpt_suff+"");    
   //log.info("=== fileName === "+fileFilter);
   //OUT.println("=== fileName === "+fileFilter);   

   File[] files = dir.listFiles(fileFilter);
   if (files.length > 0) {
        /** The newest file comes first **/
        Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
        theNewestFile = files[0];
        String fileName = files[0].getName().toString();
        log.info("=== File to Upload === "+fileName);
        OUT.println("=== File to Upload === "+fileName);
        props.put("varResultsReportFile",fileName);
   } else if(files.length <= 0) {
            /** no files exist **/
            //theNewestFile = ;
            String fileName = "FILE_MISSING";
            log.error("*FAILED to find file for "+vars.get("testApp")+" ===  "+fileName);
        OUT.println("*FAILED to find file for "+vars.get("testApp")+" ===  "+fileName);
        //SampleResult.setSuccessful(false);
        SampleResult.setStopTestNow(true);
        //SampleResult.setResponseData("FILE_MISSING");
        props.put("varResultsReportFile",fileName);
   }

    return theNewestFile;
}
catch (Throwable ex) {
   log.error("*FAILED - Something bad happened", ex);
   throw ex;
}

当我使用Language = BeanShell或Java运行此程序时,它运行良好。当我尝试使用Language = groovy时,它给了我这个错误:

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script16.groovy: 5: unable to resolve class org.apache.commons.io.filefilter
 @ line 5, column 1.
   import org.apache.commons.io.filefilter;
   ^

1 error

    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile(GroovyScriptEngineImpl.java:183) ~[groovy-all-2.4.13.jar:2.4.13]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:215) ~[ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_181]
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script16.groovy: 5: unable to resolve class org.apache.commons.io.filefilter
 @ line 5, column 1.
   import org.apache.commons.io.filefilter;

我已经检查过了,我似乎在lib文件夹中有jmeter 4.0附带的commons-io.jar。我将Java jdk升级到8_181。我还可以做些什么?现在,我将使用beanshell运行,但由于它是jmeter优于beanshell的首选语言,因此我想转换为groovy

2 个答案:

答案 0 :(得分:1)

我不知道您从何处获得此代码,但它做的事情很奇怪。即使您解决了所有编译问题和其他问题,它也将无法正常运行,因为整个测试将在此行停止:

SampleResult.setStopTestNow(true);

,并且不会进一步。

基于前缀和后缀查找最新文件的简便方法是:

File theNewestFile = new File(props.get("test_results_path")).listFiles().findAll {
    it.getName().startsWith(props.get("testReport_prefix")) && it.getName().endsWith(props.get("testReport_suffix"))
}?.sort { -it.lastModified() }?.head()

参考文献:

答案 1 :(得分:0)

删除未使用的导入:

  import org.apache.commons.io.filefilter;

filefilter是一个Java包(不是类),不需要导入。