最近由于工作要求,需要对一个串口通讯设备进行压力测试,要求连续持续对串口设备发送指令,无间隔,测试设备是否会死机。
要求做到毫秒级,测试第三方的工具,基本上都无法达到毫秒级,最少的也是10毫秒。
于是就自己写代码测试。通过.net写,发现最少能达到1毫秒,但是有遗漏,看日志如下
2024-03-31 22:11:02 164 00 00
2024-03-31 22:11:02 165 00 00
2024-03-31 22:11:02 166 00 00
2024-03-31 22:11:02 167 00 00
2024-03-31 22:11:02 169 00 00
2024-03-31 22:11:02 170 00 00
2024-03-31 22:11:02 171 00 00
2024-03-31 22:11:02 172 00 00
2024-03-31 22:11:02 173 00 00
2024-03-31 22:11:02 175 00 00
2024-03-31 22:11:02 176 00 00
2024-03-31 22:11:02 178 00 00
2024-03-31 22:11:02 179 00 00
2024-03-31 22:11:02 181 00 00
2024-03-31 22:11:02 183 00 00
2024-03-31 22:11:02 184 00 00
2024-03-31 22:11:02 186 00 00
2024-03-31 22:11:02 188 00 00
2024-03-31 22:11:02 190 00 00
2024-03-31 22:11:02 191 00 00
2024-03-31 22:11:02 192 00 00
2024-03-31 22:11:02 194 00 00
2024-03-31 22:11:02 196 00 00
2024-03-31 22:11:02 198 00 00
2024-03-31 22:11:02 199 00 00
2024-03-31 22:11:02 201 00 00
时间后的三位数字是毫秒,发现偶尔会少一个毫秒。
用java写了一段测试,效果如下
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401190]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401191]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401192]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401193]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401194]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401195]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401196]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401197]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401198]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401199]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401200]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401201]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401202]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401203]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401204]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401205]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401206]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401207]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401208]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401209]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401210]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401211]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401212]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.225 [main] DEBUG com.bonn.Test2 - [401213]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401214]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401215]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401216]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401217]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401218]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401219]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401220]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401221]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401222]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401223]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401224]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401225]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401226]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401227]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401228]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401229]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401230]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401231]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401232]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401233]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401234]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401235]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401236]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401237]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401238]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401239]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401240]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401241]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401242]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401243]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401244]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401245]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
2024-03-31 22:05:20.226 [main] DEBUG com.bonn.Test2 - [401246]---> 发送:FC FC FC 0A 00 02 20 00 28 2E
会发现同一毫秒发了好多条。从上面的日志来看,java完胜c#啊
我用了十多年的.net的,今年才开始用java,这差距就这么大吗?
.net部分源码
c#
static void Main(string[] args)
{
SerialPortHelper sp = new SerialPortHelper();
sp.Open();
for (int i = 0; i < 1000; i++)
{
byte[] bs = new byte[2];
sp.Send(bs);
Trace.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss fff} {bs.ByteToHexString()}");
}
}
java
while (true) {
i++;
if (i > totalTimes) {
break;
}
synchronized (lock) {
try {
long curCommMillis = System.currentTimeMillis();
String[] strList = new String[2];
strList[1] = "FC FC FC 0A 00 02 20 00 28 2E";
byte[] sendBuff = ByteHelper.hex2byte(strList[1]);
// Send(sendBuff);
sp.writeBytes(sendBuff, sendBuff.length);
logger.debug(String.format("[%s]---> 发送:%s ", i, byteToString(sendBuff)));
} catch (Exception ex) {
logger.error("main exception 01:", ex);
}
}
}
关键代码已经发出来了,其他无关的不影响主没有发。
大家可以讨论一下,是我使用的方法不对吗,请指正。