Jmeter protobuf测试.无法读取Protobuf讯息 [英] Jmeter protobuf testing. Could not read Protobuf message

查看:1096
本文介绍了Jmeter protobuf测试.无法读取Protobuf讯息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过protobuf协议并使用HTTP Request Sampler测试一个项目.目标应用服务器也是用Java编写的. 响应有问题:​​

I am testing one project via protobuf protocol and using HTTP Request Sampler. The target appserver is also written on Java. There is a problem with errors in reponses:

"无法读取Protobuf消息:协议消息包含 无效标签(零).嵌套的异常是 com.google.protobuf.InvalidProtocolBufferException:协议消息 包含无效标签(零)"

"Could not read Protobuf message: Protocol message contained an invalid tag (zero).; nested exception is com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero)"

情况是,这种情况并非在100%的请求中发生.当我使用 HttpClient4 时,它大约占失败请求的30-40%.在将其更改为 HttpClient3.1 之后,错误率降低到〜10%,这也不是什么好事.

The case is that it is happening not in 100% requests. When i used HttpClient4 it was about 30-40% of failed requests. After i changed it to HttpClient3.1 error rate decreased down to ~10% which is also not a good deal.

要发送protobuf消息,我在HttpSampler的Bodydata选项卡中使用了变量$ {data}.然后在BeanShell preProcessor中执行下一个操作:

To send a protobuf message I am using variable ${data} in Bodydata tab of HttpSampler. And in BeanShell preProcessor i do the next:

(import and non-necessary stuff were ommited)
MapViewport mv = MapRequest.MapViewport.newBuilder().setMaxX(mc.getX()+15).setMaxY(mc.getY()+15).setMinX(mc.getX()-15).setMinY(mc.getY()-15).build();

byte[] data = mv.toByteArray();
vars.put("data", new String(data));

我还尝试使用其他编码方式,例如新字符串(数据,"UTF-8")等等.

Also I tryed to to use different encoding like a new String(data,"UTF-8") anso on.

如果在查看结果树"中查看请求"选项卡,我可以说所有失败的消息都包含?"符号:

If to look on Request tab if View Result Tree I can say that all failed messages contain "?" symbol:

似乎不应该发送一些奇怪的符号,但是在将字节数组保存到String之后,大约有10%的请求包含了这些符号.

Seems like some weird symbols should not be sent, but ~10% of requests after saving a byte array to String contain them.

推荐答案

我非常相信您的问题是,在从二进制流转换为String然后又返回时,您丢失了一些不可打印的字符.我正在考虑两种可能的解决方法:

I'm fairly convinced your issue is that you're losing some non-printable characters while converting from binary stream to String and then back. I'm thinking of two possible workarounds:

  1. 将二进制数据写入文件,而不是保存为字符串,然后在文件部分的正文中使用HTTP采样器中的文件名作为变量

  1. Write the binary data to a file instead of saving to string, and then use the filename as variable in the HTTP sampler, in the body from file section

使用beanshell采样器,并使用体内的二进制数据构造自己的HTTPClient对象和POST请求,并自行触发,而不是使用HTTP采样器

Use a beanshell sampler, and construct your own HTTPClient object and POST request, with the binary data in body and fire it yourself instead of using the HTTP sampler

由于所有其他文件I/O,我不喜欢第一个选项.我不喜欢第二个选项,因为现在测量响应时间将包括您正在beanshell中执行的所有请求程序集-因此您必须选择一个使您不那么麻烦的选项.

I don't like the first option because of all the additional file I/O. I do not like the second option because measuring response time will now include all the request assembly you're doing in beanshell - so you'll have to pick the one that bothers you less I guess.

请让我知道您是否希望为这两种情况编写一些代码示例.

Let me know if you want me to write up some code examples for either case.

对于使用HttpClient 4的beanshell HTTP调用:

For beanshell HTTP call using HttpClient 4:

import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;

byte[] data = null;
//...assign protobuf binary buffer to data...

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://127.0.0.1");
HttpEntity entity = new ByteArrayEntity(data);
post.setEntity(entity);
post.setHeader(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
HttpResponse response=null;
try {
    response = client.execute(post);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

ResponseCode = response.getStatusLine().getStatusCode().toString();
//if some assert is true then
Issuccess = true;
ResponseMessage="Some Response Message";

这没有经过protobuf终点的测试,请让我知道它如何为您工作.

This is untested against a protobuf end point, let me know how it works out for you.

这篇关于Jmeter protobuf测试.无法读取Protobuf讯息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆