参考文章:JMeter5.1开发TCP协议接口脚本
说明:我的项目是 单片机上位机 与 Netty 进行 socket 通信,软硬件约定的数据交换格式为:字节数组,所以这里测试中用了:BinaryTCPClientImpl
1、修改 jmeter 配置文件
jmeter.properties
修改TCP Sampler,如下图就可以了
2、Jmeter 脚本参数设置
3、Text to Send 来源说明【C语言】
消息发送方代码由 C 语言编写,发送消息封装成结构体,在C程序中打印出结构体的字节数组,然后复制粘贴到“Text to Send”即可。
C程序打印出结构体字节数组代码如下:参考: https://blog.youkuaiyun.com/NRlovestudy/article/details/94392718
int print_all_byte(void *addr, int size)
{
unsigned char *ptr = addr;
int print_bytes = 0;
if (NULL == ptr) {
return -1;
}
while (print_bytes < size) {
printf("%02x", *ptr);
ptr++;
print_bytes++;
}
printf("\n");
return print_bytes;
}
int main(void)
{
struct message
{
int length;
char content[1020];
};
struct message msg;
memset(&msg, 0, sizeof(msg));
sprintf(msg.content, "kjsakjcbjkb271671289cskbcsbs76e87");
msg.length = strlen(msg.content);
msglength = 4 + strlen(msg.content);
if (msglength == print_all_byte((void *)&msg, msglength)) {
printf("printf message success!\n");
}
return 0;
}
4、超时 500 错误的处理
由于实际项目中通信双方并没有根据 特定符号 来判断消息结束,TCP测试时无法填写 End of line(EOL) byte value,这会导致接收超时错误,即使TCP连接和发送成功也会被Jmeter 判定为测试失败,这与实际需求不符,需要修改源码。
(1)源码导入IDEA:https://blog.youkuaiyun.com/NRlovestudy/article/details/94392777
(2)修改源码:参考 https://blog.youkuaiyun.com/woshilishu/article/details/86589558
第三个参数 设置为 null