ProfilerTest xref

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  import junit.framework.TestCase;
    28
    29  import org.slf4j.Logger;
    30  import org.slf4j.LoggerFactory;
    31
    32  public class ProfilerTest  extends TestCase {
    33
    34    Logger logger = LoggerFactory.getLogger(ProfilerTest.class);
    35
    36    public void setUp() throws Exception {
    37      super.setUp();
    38    }
    39    public void testSmoke() {
    40      Profiler profiler = new Profiler("SMOKE");
    41      profiler.stop();
    42      StopWatch gSW = profiler.globalStopWatch;
    43
    44      // verify
    45      profiler.sanityCheck();
    46      assertEquals(TimeInstrumentStatus.STOPPED,  gSW.status);
    47      assertEquals(0, profiler.childTimeInstrumentList.size());
    48      assertNull(profiler.getLastTimeInstrument());
    49    }
    50
    51    public void testBasicProfiling() {
    52      Profiler profiler = new Profiler("BAS");
    53
    54      profiler.start("doX");
    55      doX(1);
    56
    57      profiler.start("doY");
    58      doY(10);
    59
    60      profiler.start("doZ");
    61      doZ(2);
    62      profiler.stop();
    63
    64      // verify
    65      profiler.sanityCheck();
    66      StopWatch gSW = profiler.globalStopWatch;
    67      assertEquals(TimeInstrumentStatus.STOPPED,  gSW.status);
    68      assertEquals(3, profiler.childTimeInstrumentList.size());
    69      assertNotNull(profiler.getLastTimeInstrument());
    70      assertEquals("doZ", profiler.getLastTimeInstrument().getName());
    71    }
    72
    73    // + Profiler [BAS]
    74    // |-- elapsed time                          [doX]     1.272 milliseconds.
    75    // |-- elapsed time                      [doYYYYY]    25.398 milliseconds.
    76    // |--+ Profiler [subtask]
    77    //    |-- elapsed time                           [n1]     1.434 milliseconds.
    78    //    |-- elapsed time                           [n2]     5.855 milliseconds.
    79    //    |-- Total elapsed time                [subtask]     7.321 milliseconds.
    80    // |-- elapsed time                          [doZ]     3.211 milliseconds.
    81    // |-- Total elapsed time                    [BAS]    30.317 milliseconds.
    82    public void testNestedProfiling() {
    83
    84      Profiler profiler = new Profiler("BAS");
    85      profiler.setLogger(logger);
    86      profiler.start("doX");
    87      doX(1);
    88
    89      profiler.start("doYYYYY");
    90      for (int i = 0; i < 5; i++) {
    91        doY(i);
    92      }
    93      Profiler nested = profiler.startNested("subtask");
    94      doSubtask(nested);
    95      profiler.start("doZ");
    96      doZ(2);
    97      profiler.stop();
    98
    99      // verify
    100     profiler.sanityCheck();
    101     StopWatch gSW = profiler.globalStopWatch;
    102     assertEquals(TimeInstrumentStatus.STOPPED,  gSW.status);
    103     //assertEquals(3, profiler.stopwatchList.size());
    104     assertEquals(4, profiler.childTimeInstrumentList.size());
    105     assertNotNull(profiler.getLastTimeInstrument());
    106     assertEquals("doZ", profiler.getLastTimeInstrument().getName());
    107
    108   }
    109
    110   private void doX(int millis) {
    111     delay(millis);
    112   }
    113   private void doY(int millis) {
    114     delay(millis);
    115   }
    116   private void doZ(int millis) {
    117     delay(millis);
    118   }
    119
    120   public void doSubtask(Profiler nested) {
    121     nested.start("n1");
    122     doX(1);
    123
    124     nested.start("n2");
    125     doX(5);
    126     nested.stop();
    127   }
    128
    129
    130   void delay(int millis) {
    131     try {
    132       Thread.sleep(millis);
    133     } catch (InterruptedException e) {
    134     }
    135   }
    136 }