Slf4j-profiling

提供:Dev Guides
移動先:案内検索

SLF4J-プロファイリング

SLF4Jディストリビューションは、 slf4j-ext.jar を提供します。これには、プロファイリング、拡張ログ、イベントログ、Javaエージェントによるログなどの機能用のAPIが含まれています。

プロファイリング

プログラマーは、メモリの使用、時間の複雑さ、またはプログラムに関する特定の命令の使用などの属性を測定して、そのプログラムの実際の能力を測定したいことがあります。 このようなプログラムに関する測定は、プロファイリングと呼ばれます。 プロファイリングでは、動的プログラム分析を使用してこのような測定を行います。

SLF4Jは、プロファイリング用に org.slf4j.profiler パッケージに Profiler という名前のクラスを提供します。 これは貧乏人のプロファイラーとして知られています。 これを使用して、プログラマは長時間のタスクを実行するのにかかる時間を知ることができます。

Profilerクラスを使用したプロファイリング

プロファイラーにはストップウォッチと子ストップウォッチが含まれており、プロファイラークラスによって提供されるメソッドを使用してこれらを開始および停止できます。

プロファイラクラスを使用してプロファイリングを続行するには、以下の手順に従います。

ステップ1-プロファイラークラスのインスタンス化

プロファイラーの名前を表す文字列値を渡すことにより、プロファイラークラスをインスタンス化します。 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のプロファイラークラスを使用したプロファイリングを示しています。 ここでは、1から10000までの数値の平方和を印刷し、1から10000までの数値の合計を印刷する2つのサンプルタスクを取りました。 これら2つのタスクにかかる時間を取得しようとしています。

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