
SLF4J - 效能分析
SLF4J 分發版提供slf4j-ext.jar,其中包含用於效能分析、擴充套件日誌記錄、事件日誌記錄以及使用 Java 代理進行日誌記錄等功能的 API。
效能分析
有時程式設計師希望衡量一些屬性,例如程式的記憶體使用情況、時間複雜度或特定指令的使用情況,以衡量程式的真實能力。這種對程式的測量稱為效能分析。效能分析使用動態程式分析來進行這種測量。
SLF4J 提供了一個名為Profiler的類,位於org.slf4j.profiler包中,用於效能分析目的。這被稱為“窮人版”的效能分析器。使用它,程式設計師可以找出執行長時間任務所需的時間。
使用 Profiler 類進行效能分析
效能分析器包含秒錶和子秒錶,我們可以使用效能分析器類提供的的方法來啟動和停止它們。
要使用效能分析器類繼續進行效能分析,請按照以下步驟操作。
步驟 1 - 例項化效能分析器類
透過傳遞一個表示效能分析器名稱的字串值來例項化 Profiler 類。當我們例項化 Profiler 類時,將啟動一個全域性秒錶。
//Creating a profiler Profiler profiler = new Profiler("Sample");
步驟 2 - 啟動子秒錶
當我們呼叫start()方法時,它將啟動一個新的命名子秒錶,並停止之前的子秒錶(或時間儀器)。
透過傳遞一個表示要建立的子秒錶名稱的字串值,呼叫Profiler類的start()方法。
//Starting a child stopwatch and stopping the previous one. profiler.start("Task 1"); obj.demoMethod1();
建立這些秒錶後,您可以執行您的任務,或者呼叫執行這些任務的方法。
步驟 3:啟動另一個子秒錶(如果您希望)
如果需要,可以使用start()方法建立另一個秒錶,並執行所需的任務。如果這樣做,它將啟動一個新的秒錶並停止上一個秒錶(即任務 1)。
//Starting another child stopwatch and stopping the previous one. profiler.start("Task 2"); obj.demoMethod2();
步驟 4:停止秒錶
當我們呼叫stop()方法時,它將停止最近的子秒錶和全域性秒錶,並返回當前的時間儀器。
// Stopping the current child stopwatch and the global stopwatch. TimeInstrument tm = profiler.stop();
步驟 5:列印時間儀器的內容。
使用print()方法列印當前時間儀器的內容。
//printing the contents of the time instrument tm.print();
示例
以下示例演示了使用 SLF4J 的 Profiler 類進行效能分析。這裡我們採用了兩個示例任務,列印從 1 到 10000 的數字的平方和,列印從 1 到 10000 的數字的和。我們試圖獲取這兩個任務所需的時間。
import org.slf4j.profiler.Profiler; import org.slf4j.profiler.TimeInstrument; public class ProfilerExample { public void demoMethod1(){ double sum = 0; for(int i=0; i< 1000; i++){ sum = sum+(Math.pow(i, 2)); } System.out.println("Sum of squares of the numbers from 1 to 10000: "+sum); } public void demoMethod2(){ int sum = 0; for(int i=0; i< 10000; i++){ sum = sum+i; } System.out.println("Sum of the numbers from 1 to 10000: "+sum); } public static void main(String[] args) { ProfilerExample obj = new ProfilerExample(); //Creating a profiler Profiler profiler = new Profiler("Sample"); //Starting a child stop watch and stopping the previous one. profiler.start("Task 1"); obj.demoMethod1(); //Starting another child stop watch and stopping the previous one. profiler.start("Task 2"); obj.demoMethod2(); //Stopping the current child watch and the global watch. TimeInstrument tm = profiler.stop(); //printing the contents of the time instrument tm.print(); } }
輸出
執行後,上述程式將生成以下輸出:
Sum of squares of the numbers from 1 to 10000: 3.328335E8 Sum of the numbers from 1 to 10000: 49995000 + Profiler [BASIC] |-- elapsed time [Task 1] 2291.827 microseconds. |-- elapsed time [Task 2] 225.802 microseconds. |-- Total [BASIC] 3221.598 microseconds.
記錄效能分析器資訊
要將效能分析器的結果記錄到日誌中,而不是列印此資訊,您需要:
使用LoggerFactory類建立一個日誌記錄器。
透過例項化 Profiler 類來建立效能分析器。
透過將建立的日誌記錄器物件傳遞給Profiler類的setLogger()方法,將日誌記錄器與效能分析器關聯。
最後,使用log()方法記錄效能分析器的資訊,而不是列印它。
示例
在以下示例中,與前面的示例不同(而不是列印),我們試圖記錄時間儀器的內容。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.profiler.Profiler; import org.slf4j.profiler.TimeInstrument; public class ProfilerExample_logger { public void demoMethod1(){ double sum = 0; for(int i=0; i< 1000; i++){ sum = sum+(Math.pow(i, 2)); } System.out.println("Sum of squares of the numbers from 1 to 10000: "+sum); } public void demoMethod2(){ int sum = 0; for(int i=0; i< 10000; i++){ sum = sum+i; } System.out.println("Sum of the numbers from 1 to 10000: "+sum); } public static void main(String[] args) { ProfilerExample_logger obj = new ProfilerExample_logger(); //Creating a logger Logger logger = LoggerFactory.getLogger(ProfilerExample_logger.class); //Creating a profiler Profiler profiler = new Profiler("Sample"); //Adding logger to the profiler profiler.setLogger(logger); //Starting a child stop watch and stopping the previous one. profiler.start("Task 1"); obj.demoMethod1(); //Starting another child stop watch and stopping the previous one. profiler.start("Task 2"); obj.demoMethod2(); //Stopping the current child watch and the global watch. TimeInstrument tm = profiler.stop(); //Logging the contents of the time instrument tm.log(); } }
輸出
執行後,上述程式將生成以下輸出。
Sum of squares of the numbers from 1 to 10000: 3.328335E8 Sum of the numbers from 1 to 10000: 49995000