View Javadoc
1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.profiler;
26  
27  /**
28   * 
29   * This demo illustrates usage of SLF4J profilers.
30   * 
31   * <p>
32   * We have been given the task of generating a large number, say N, of random
33   * integers. We need to transform that array into a smaller array containing
34   * only prime numbers. The new array has to be sorted.
35   * 
36   * <p>
37   * While tackling this problem, we would like to measure the time spent in each
38   * subtask.
39   * 
40   * <p>
41   * A typical output for this demo would be:
42   * 
43   * <pre>
44     + Profiler [BASIC]
45     |-- elapsed time                      [A]   213.186 milliseconds.
46     |-- elapsed time                      [B]  2499.107 milliseconds.
47     |-- elapsed time                  [OTHER]  3300.752 milliseconds.
48     |-- Total                         [BASIC]  6014.161 milliseconds.
49    </pre>
50   * 
51   * @author Ceki Gulcu
52   */
53  public class BasicProfilerDemo {
54  
55      public static void main(String[] args) {
56          // create a profiler called "BASIC"
57          Profiler profiler = new Profiler("BASIC");
58          profiler.start("A");
59          doA();
60  
61          profiler.start("B");
62          doB();
63  
64          profiler.start("OTHER");
65          doOther();
66          profiler.stop().print();
67      }
68  
69      static private void doA() {
70          delay(200);
71      }
72  
73      static private void doB() {
74          delay(2500);
75      }
76  
77      static private void doOther() {
78          delay(3300);
79      }
80  
81      static private void delay(int millis) {
82          try {
83              Thread.sleep(millis);
84          } catch (InterruptedException e) {
85          }
86      }
87  }