1.主要实现方法是借助JavaSamplerClient 实现接口:
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
以下main注释的方法主要用于本地调试接口,setup主要实现前置操作,构造初始数据,runTest主要实现接口调用和返回
public class ClientPredictGrpc implements JavaSamplerClient {
private static final Logger logger = LoggerFactory.getLogger(ClientPredictGrpc.class);
private int port ;
private String ip ;
private BatchInferenceRequest batchInferenceRequest;
@Override
public void setupTest(JavaSamplerContext ctx) {
String serviceId = ctx.getParameter("serviceId");
String userId = ctx.getParameter("userId");
Random random =new Random();
this.ip=ctx.getParameter("ip");
this.port =ctx.getIntParameter("port");
int columns = ctx.getIntParameter("featureCols");
int uidNumber = ctx.getIntParameter("batchSize");
this.batchInferenceRequest =new BatchInferenceRequest();
this.batchInferenceRequest.setServiceId(serviceId);
this.batchInferenceRequest.setCaseId(Long.toString(System.currentTimeMillis()));
List singleInferenceDataList = Lists.newArrayList();
BatchInferenceRequest.SingleInferenceData singleInferenceData =new BatchInferenceRequest.SingleInferenceData() ;
//todo set batch list the same batchList data each batch request
for (int j = 0; j< columns; j++){
String featureColumnName=String.format("x%d",j);
singleInferenceData.getFeatureData().put(featureColumnName, random.nextInt(2));
}
for (int i = 0; i < uidNumber; i++) {
Map uidMap=new HashMap<>();
uidMap.put("user_id", userId);
singleInferenceData.getSendToRemoteFeatureData().putAll(uidMap);
singleInferenceData.setIndex(i);
singleInferenceDataList.add(singleInferenceData);
}
this.batchInferenceRequest.setBatchDataList(singleInferenceDataList);
}
@Override
public SampleResult runTest(JavaSamplerContext ctx) {
SampleResult result= new SampleResult();
result.sampleStart();
try {
JSONObject jsonObject= ClientGrpc.call(this.ip,this.port,this.batchInferenceRequest);
if (jsonObject.get("retcode") == null || !jsonObject.getString("retcode").equals(InferenceRetCode.OK)){
if (jsonObject.get("retcode") == null){
result.setSuccessful(false);
result.setResponseData(jsonObject.toJSONString(),"utf-8");
result.setResponseCode("retcode is null ");
}else {
result.setResponseData(jsonObject.toJSONString(), "utf-8");
result.setDataType(SampleResult.TEXT);
result.setSuccessful(false);
result.setResponseMessage("failed");
result.setResponseCode(jsonObject.getString("retcode"));
}
}else{
result.setResponseData(jsonObject.toJSONString(),"utf-8");
result.setDataType(SampleResult.TEXT);
result.setSuccessful(true);
result.setResponseMessage("success");
result.setResponseCode("0");
}
}catch (Exception e){
result.setSuccessful(false);
if(e.getMessage() !=null) {
result.setResponseData(e.getMessage(), null);
result.setResponseCode("grpc exception");
}
}finally {
result.sampleEnd();
}
//logger.info("result finally get as String is : "+ result.getResponseDataAsString());
return result;
}
@Override
public void teardownTest(JavaSamplerContext ctx) {
}
@Override
public Arguments getDefaultParameters() {
Arguments arguments =new Arguments();
arguments.addArgument("ip","");
arguments.addArgument("port","");
arguments.addArgument("featureCols","");
arguments.addArgument("userId","");
arguments.addArgument("serviceId","");
arguments.addArgument("batchSize","");
return arguments;
}
// public static void main(String[] args) {
// ClientPredictGrpc demo=new ClientPredictGrpc();
// Arguments arguments = new Arguments();
// String uid="a36fda4444dbdfdb1714c81e86b13c06,a46458c10546bbe794dbca88cd0a4832";
//
// arguments.addArgument("ip","172.16.153.119");
// arguments.addArgument("port","8000");
// arguments.addArgument("header","5");
// arguments.addArgument("userId",uid);
// arguments.addArgument("serviceId","heterolr01");
// JavaSamplerContext ctx=new JavaSamplerContext(arguments);
// demo.setupTest(ctx);
// demo.runTest(ctx);
// demo.teardownTest(ctx);
// }
}
grpc client类:
public class ClientGrpc {
private static Random random = new Random();
private static GrpcConnectionPool GRPC_CONNECTION_POOL = GrpcConnectionPool.getPool();
public static ManagedChannel createManagedChannel(String ip, int port) throws Exception {
return GRPC_CONNECTION_POOL.getManagedChannel(ip, port);
}
static JSONObject call(String ip,int port,BatchInferenceRequest batchInferenceRequest) {
JSONObject predictMessage=null;
ManagedChannel managedChannel = null;
try {
managedChannel = createManagedChannel(ip, port);
InferenceServiceProto.InferenceMessage.Builder inferenceMessageBuilder = InferenceServiceProto.InferenceMessage.newBuilder();
inferenceMessageBuilder.setBody(ByteString.copyFrom(JSON.toJSONString(batchInferenceRequest), "UTF-8"));
InferenceServiceGrpc.InferenceServiceBlockingStub blockingStub = InferenceServiceGrpc.newBlockingStub(managedChannel);
InferenceServiceProto.InferenceMessage inferenceMessage = blockingStub.batchInference(inferenceMessageBuilder.build());
String result = new String(inferenceMessage.getBody().toByteArray());
predictMessage = JSONObject.parseObject(result);
} catch (Exception e) {
e.printStackTrace();
}
return predictMessage;
}
}