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