如何在生产中检测和诊断慢速代码
開發(fā)人員面臨的最困難的任務(wù)之一是查找和診斷生產(chǎn)中運(yùn)行緩慢的代碼。
首先,您如何監(jiān)控生產(chǎn)代碼而不放慢速度? 當(dāng)然,您無法通過分析器運(yùn)行生產(chǎn)代碼。 即使您具有計(jì)時(shí)代碼的機(jī)制,那么如何診斷問題呢? 如果您無法在開發(fā)環(huán)境中重現(xiàn)該問題,則尤其如此。 理想情況下,您希望在生產(chǎn)中發(fā)生問題時(shí)得到通知,并向其提供足夠的信息,以便有合理的機(jī)會(huì)修復(fù)或至少診斷問題。
這是我的同事彼得·勞瑞 ( Peter Lawrey)建議的一種機(jī)制,您可以用來精確地做到這一點(diǎn)。 (完整的代碼清單可在此處找到)。
您要做的是創(chuàng)建一個(gè)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;}}} }如果線程無法在閾值時(shí)間內(nèi)重置,則此類將轉(zhuǎn)儲(chǔ)正在運(yùn)行的線程的堆棧跟蹤。
這是一些示例程序,演示如何調(diào)用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觀察到了這一“關(guān)鍵”代碼。 如果在8毫秒內(nèi)未重置監(jiān)視器,它將轉(zhuǎn)儲(chǔ)代碼的堆棧跟蹤。
如果您有一個(gè)Monitor監(jiān)視您的關(guān)鍵代碼段,則可以確保它們?cè)诮o定的約束范圍內(nèi)執(zhí)行。 如果代碼確實(shí)違反了約束,則可以通過檢查堆棧跟蹤來很好地了解問題所在。 您還可以使用它來查看關(guān)鍵代碼在其運(yùn)行期間未執(zhí)行多少次。
如果您沒有專用的備用CPU進(jìn)行監(jiān)控,則可以更改等待時(shí)間。 另外,您可能想更改等待策略以允許GC暫停,這會(huì)影響所有線程。 您可能想通過使用System.nanoTime()來細(xì)化計(jì)時(shí),而不是以毫秒為單位。
翻譯自: https://www.javacodegeeks.com/2015/02/detect-diagnose-slow-code-production.html
總結(jié)
以上是生活随笔為你收集整理的如何在生产中检测和诊断慢速代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每个人都必须阅读的10篇Java文章
- 下一篇: 流式传输大数据:Storm,Spark和