1   package org.slf4j.profiler;
2   
3   /**
4    * 
5    * This demo illustrates usage of SLF4J profilers.
6    * 
7    * <p>
8    * We have been given the task of generating a large number, say N, of random
9    * integers. We need to transform that array into a smaller array containing
10   * only prime numbers. The new array has to be sorted.
11   * 
12   * <p>
13   * While tackling this problem, we would like to measure the time spent in each
14   * subtask.
15   * 
16   * <p>
17   * A typical output for this demo would be:
18   * 
19   * <pre>
20     + Profiler [BASIC]
21     |-- elapsed time                      [A]   213.186 milliseconds.
22     |-- elapsed time                      [B]  2499.107 milliseconds.
23     |-- elapsed time                  [OTHER]  3300.752 milliseconds.
24     |-- Total                         [BASIC]  6014.161 milliseconds.
25    </pre>
26   * 
27   * @author Ceki Gulcu
28   */
29  public class BasicProfilerDemo {
30  
31    public static void main(String[] args) {
32      // create a profiler called "BASIC"
33      Profiler profiler = new Profiler("BASIC");
34      profiler.start("A");
35      doA();
36  
37      profiler.start("B");
38      doB();
39  
40      profiler.start("OTHER");
41      doOther();
42      profiler.stop().print();
43    }
44  
45    static private void doA() {
46      delay(200);
47    }
48  
49    static private void doB() {
50      delay(2500);
51    }
52  
53    static private void doOther() {
54      delay(3300);
55    }
56  
57    static private void delay(int millis) {
58      try {
59        Thread.sleep(millis);
60      } catch (InterruptedException e) {
61      }
62    }
63  }