如何在生产中检测和诊断慢速代码
開發人員面臨的最困難的任務之一是查找和診斷生產中運行緩慢的代碼。
首先,您如何監控生產代碼而不放慢速度? 當然,您無法通過分析器運行生產代碼。 即使您具有計時代碼的機制,那么如何診斷問題呢? 如果您無法在開發環境中重現該問題,則尤其如此。 理想情況下,您希望在生產中發生問題時得到通知,并向其提供足夠的信息,以便有合理的機會修復或至少診斷問題。
這是我的同事彼得·勞瑞 ( Peter Lawrey)建議的一種機制,您可以用來精確地做到這一點。 (完整的代碼清單可在此處找到)。
您要做的是創建一個Monitor類,如下所示:
public class Monitor implements Runnable{private final Thread thread;private final AtomicLong startTime = new AtomicLong(Long.MAX_VALUE);private final int thresholdMS;public Monitor(Thread thread, int thresholdMS){this.thread = thread;this.thresholdMS = thresholdMS;}public void reset(){startTime.set(System.currentTimeMillis());}@Overridepublic void run(){while(thread.isAlive()){long timeTaken = System.currentTimeMillis()-startTime.get();if(timeTaken > thresholdMS){System.out.println(timeTaken + "-------------------------");Stream.of(thread.getStackTrace()).forEach(System.out::println);}try {Thread.sleep(thresholdMS/2);} catch (InterruptedException e) {break;}}} }如果線程無法在閾值時間內重置,則此類將轉儲正在運行的線程的堆棧跟蹤。
這是一些示例程序,演示如何調用Monitor。
Monitor monitor = new Monitor(Thread.currentThread(), 8); Thread thread = new Thread(monitor, "MonitorThread"); thread.setDaemon(true); thread.start();while(true) {monitor.reset();double x=0;for (int i = 0; i < 10_000; i++) {x += Math.sqrt(i);Logger.getLogger(getClass().getName()).fine("x=" + x);} }Monitor觀察到了這一“關鍵”代碼。 如果在8毫秒內未重置監視器,它將轉儲代碼的堆棧跟蹤。
如果您有一個Monitor監視您的關鍵代碼段,則可以確保它們在給定的約束范圍內執行。 如果代碼確實違反了約束,則可以通過檢查堆棧跟蹤來很好地了解問題所在。 您還可以使用它來查看關鍵代碼在其運行期間未執行多少次。
如果您沒有專用的備用CPU進行監控,則可以更改等待時間。 另外,您可能想更改等待策略以允許GC暫停,這會影響所有線程。 您可能想通過使用System.nanoTime()來細化計時,而不是以毫秒為單位。
翻譯自: https://www.javacodegeeks.com/2015/02/detect-diagnose-slow-code-production.html
總結
以上是生活随笔為你收集整理的如何在生产中检测和诊断慢速代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每个人都必须阅读的10篇Java文章
- 下一篇: rog超杀和三星g7哪个好(华硕超杀对比