【问题标题】:JMeter with gRPC Java Client and BeanShell Sampler带有 gRPC Java 客户端和 BeanShell 采样器的 JMeter
【发布时间】:2020-12-27 07:23:42
【问题描述】:

为什么无法从 JMeter 的 BeanShell 采样器中运行本机 Java gRPC 客户端:

package at.fhj.swd.grpc.client;

import at.fhj.swd.grpc.CalcRequest;
import at.fhj.swd.grpc.CalcResponse;
import at.fhj.swd.grpc.CalcServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class GrpcClient {

public static void calc() {
    ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9090)
            .usePlaintext()
            .build();

    CalcServiceGrpc.CalcServiceBlockingStub stub
            = CalcServiceGrpc.newBlockingStub(channel);
    CalcResponse calcResponse = stub.calc(CalcRequest.newBuilder()
            .setNumber(7)
            .build());

    channel.shutdown();

    System.out.println(calcResponse.getResultList());
    }
}

BeanShell 采样器脚本无法创建 GrpcCient 的实例。 (引用方法调用)

import at.fhj.swd.grpc.client.GrpcClient;

GrpcClient grpcClient = new GrpcClient();

//grpcClient.calc();

错误:

Response code:500
Response message:org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   io/grpc/Channel

似乎导入有问题,但为什么呢?如果在没有 JMeter 的情况下执行,则客户端运行。

【问题讨论】:

  • ManagedChannel 扩展了 Channel,但为什么会出现问题?

标签: jmeter grpc


【解决方案1】:
  1. Beanshell 不是 100% 与 Java 兼容,所以如果有重载的函数/构造函数,你需要通过 reflection 明确指定你想要的
  2. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy 因此只需从语言下拉列表中选择 groovy 就足够了,您的代码将按预期开始工作。此外,与 Beanshell 相比,Groovy 的性能要好得多,请参阅Apache Groovy - Why and How You Should Use It 文章了解更多详情

【讨论】:

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