.net和java串列埠通訊壓力測試對比, java完勝

一级码农VIP發表於2024-03-31

最近由於工作要求,需要對一個串列埠通訊裝置進行壓力測試,要求連續持續對串列埠裝置傳送指令,無間隔,測試裝置是否會當機。
要求做到毫秒級,測試第三方的工具,基本上都無法達到毫秒級,最少的也是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,這差距就這麼大嗎?
c#用的是System.IO.Ports;官方的庫。
java用的是com.fazecast.jSerialComm.SerialPort,第三方的。

附錄

.net部分原始碼

        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()}");
            }
        }
            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);
                    }
                }
            }

關鍵程式碼已經發出來了,其他無關的不影響主沒有發。
大家可以討論一下,是我使用的方法不對嗎,請指正。

相關文章