JMeter - 无法在 JSR223 脚本中使用 Java 流

JMeter - Can't use Java stream in JSR223 script

我无法在 JMeter 的 JSR223 脚本中使用 Java 流,

我试过 Java stream

List<String> myList =
    Arrays.asList("a1", "a2", "b1", "c2", "c1");

myList
    .stream()
    .filter(s -> s.startsWith("c"))
    .map(String::toUpperCase)
    .sorted()
    .forEach(System.out::println);

有例外

2019-04-22 13:44:49,808 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script9.groovy: 6: unexpected token: -> @ line 6, column 15.
       .filter(s -> s.startsWith("c"))
                 ^

1 error

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script9.groovy: 6: unexpected token: -> @ line 6, column 15.
       .filter(s -> s.startsWith("c"))
                 ^

1 error

    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile(GroovyScriptEngineImpl.java:187) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:217) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) [ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) [ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]

我试过 groovy stream

def sample = ['Groovy', 'Gradle', 'Grails', 'Spock'] as String[]

def result = sample.stream()  // Use stream() on array objects
               .filter { s -> s.startsWith('Gr') }
               .map { s -> s.toUpperCase() }
               .toList()  // toList() added to Stream by Groovy

有例外:

019-04-22 13:42:40,345 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.String;.stream() is applicable for argument types: () values: []
Possible solutions: grep(), size(), sort(), sum(), grep(), head()
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.String;.stream() is applicable for argument types: () values: []
Possible solutions: grep(), size(), sort(), sum(), grep(), head()
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
    at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_191]

Lambda expressions still not supported in beanshell

Groovy 不支持 lambda 但是您可以用 Closure 替换 lambda,如下所示:

​List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");  
myList.stream()
       .filter{s -> s.startsWith("c")}
       .map{s-> s.toUpperCase()}
       .sorted()
       .forEach{s->println s};​

输出:

C1
C2

你的第二种情况给出了错误,因为样本是一个数组,stream() 在数组上不可用。您可以通过 Arrays.stream(array) 获取流。

import java.util.stream.Collectors;

def sample = ['Groovy', 'Gradle', 'Grails', 'Spock'] as String[]  
def result = Arrays.stream(sample )
                        .filter { s -> s.startsWith('Gr') }                
                        .map { s -> s.toUpperCase() }                
                        .collect(Collectors.toList())​