SLF4J - 分析

SLF4J 发行版提供 slf4j-ext.jar,其中包含用于分析、扩展日志记录、事件日志记录和使用 Java 代理进行日志记录等功能的 API。

分析

有时程序员想要测量一些属性,如内存使用、时间复杂度或程序特定指令的使用,以测量该程序的实际能力。这种对程序的测量称为分析。分析使用动态程序分析来进行此类测量。

SLF4J 在 org.slf4j.profiler 包中提供了一个名为 Profiler 的类用于分析目的。这被称为穷人的分析器。使用它,程序员可以找出执行长时间任务所花费的时间。

使用 Profiler 类进行分析

分析器包含秒表和子秒表,我们可以使用分析器类提供的方法启动和停止它们。

要继续使用分析器类进行分析,请按照以下步骤操作。

步骤 1 - 实例化分析器类

通过传递表示分析器名称的字符串值来实例化分析器类。当我们实例化 Profiler 类时,将启动全局秒表。

//创建分析器
Profiler profiler = new Profiler("Sample");

步骤 2 - 启动子秒表

当我们调用 start() 方法时,它将启动一个新的子秒表(已命名)并停止之前的子秒表(或时间仪器)。

通过传递表示要创建的子秒表名称的字符串值来调用 Profiler 类的 start() 方法。

//启动子秒表并停止前一个子秒表。
profiler.start("Task 1");
obj.demoMethod1();

创建这些秒表后,您可以执行任务或调用运行任务的方法。

步骤 3:启动另一个子秒表(如果您愿意)

如果需要,使用 start() 方法创建另一个秒表并执行所需的任务。如果这样做,它将启动一个新的秒表并停止前一个秒表(即任务 1)。

//启动另一个子秒表并停止前一个秒表。
profiler.start("Task 2");
obj.demoMethod2();

步骤 4:停止手表

当我们调用 stop() 方法时,它将停止最近的子秒表和全局秒表并返回当前的时间仪器。

// 停止当前子秒表和全局秒表。
TimeInstrument tm = profiler.stop();

步骤 5:打印时间仪器的内容。

使用 print() 方法打印当前时间仪器的内容。

//打印时间仪器的内容
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();
    
    //创建一个分析器
    Profiler profiler = new Profiler("Sample");
    
    //启动一个子秒表并停止前一个秒表。
    profiler.start("Task 1");
    obj.demoMethod1();
    
    //启动另一个子秒表并停止前一个秒表。
    profiler.start("Task 2");
    obj.demoMethod2();
    
    //停止当前子秒表和全局秒表。
    TimeInstrument tm = profiler.stop();
    
    //打印时间仪器的内容
    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();
    
    //创建一个记录器
    Logger logger = LoggerFactory.getLogger(ProfilerExample_logger.class);
    
    //创建一个分析器
    Profiler profiler = new Profiler("Sample");
    
    //将记录器添加到分析器
    profiler.setLogger(logger);
    
    //启动子秒表并停止前一个秒表。
    profiler.start("Task 1");
    obj.demoMethod1();
    
    //启动另一个子秒表并停止前一个秒表。
    profiler.start("Task 2");
    obj.demoMethod2();
    
    //停止当前子秒表和全局秒表。
    TimeInstrument tm = profiler.stop();
    
    //记录时间仪器的内容
    tm.log();
   }
}

输出

执行后,上述程序生成以下输出。

Sum of squares of the numbers from 1 to 10000: 3.328335E8
Sum of the numbers from 1 to 10000: 49995000