SLF4J分發提供了slf4j-ext.jar
,它包含用於分析,擴展日誌記錄,事件日誌記錄和使用java代理進行日誌記錄等功能的API。
剖析
有時,程式員想要測量一些屬性,如使用記憶體,時間複雜度或使用有關程式的特定指令來測量程式的實際能力。關於程式的這種測量稱為剖析。分析使用動態程式分析來進行此類測量。
SLF4J在org.slf4j.profiler
包中提供了一個Profiler
類,用於剖析目的。使用它,程式員可以找出執行長時間任務所需的時間。
使用Profiler類剖析
Profiler
類包含碼錶和子碼錶,我們可以使用Profiler
類提供的方法啟動和停止這些。
要使用Profiler
類類繼續進行性能分析,請按照下麵給出的步驟進行操作。
第1步 - 實例化Profiler類
通過傳遞表示profiler
名稱的字串值來實例化Profiler
類。當實例化Profiler
類時,將啟動一個全局碼錶。如下示例代碼:
//Creating a profiler
Profiler profiler = new Profiler("Sample");
第2步 - 啟動碼錶
當調用start()
方法時,它將啟動一個新的碼錶(命名),並停止早期的碼錶(或時間工具)。
通過傳遞表示要創建的碼錶名稱的String
值來調用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.
記錄Profiler資訊
不需要列印探查器的結果來記錄此信息,而是 -
- 使用
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