groovy - JMeter groovy 语言抛出 'unable to resolve class' 但 beanshell 或 java 都可以

标签 groovy jmeter

我正在使用 JMeter 4.0。下面是我在 JSR223 Sampler 中的一些代码。它在目录中查找并找到特定文件。

//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 运行,但想转换为 groovy,因为这是 jmeter 相对 beanshell 的首选语言

最佳答案

我不知道你从哪里得到这段代码,但它做了一些非常奇怪的事情。即使您解决了所有编译和其他问题,它也不会起作用,因为您的整个测试将停止在这一行:

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()

引用文献:

关于groovy - JMeter groovy 语言抛出 'unable to resolve class' 但 beanshell 或 java 都可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51731005/

相关文章:

grails - 如何通过Grails代码获取 session 超时值

java - 使用 Spring Boot 用 groovy 解析日期

groovy - 将 Groovy TemplateEngine 与任何变量名称绑定(bind)

调用 SOAP Web 服务的 Java/Groovy 方法(版本 1.2)

java - Eclipse java 编辑器将生成的 (Groovy) 方法标记为未定义

websocket - JMeter 中的 Signalr 负载测试

java - 使用 Maven Eclipse 运行 jmeter 脚本时出现 com.thoughtworks.xstream.converters.ConversionException

web-applications - 当我不知道客户端应用程序行为时,哪个是最好的性能测试工具。因为我将拥有许多基于 Web 的客户端

XPath JMeter 断言 : Error "prefix must resolve to a namespace"

onclick - 是否可以使用 JMeter 生成 onClick 事件?