blob: 2ea552be97ff35a6c4b75d6277a6ce7f3f6cf527 [file] [log] [blame]
Ceki Gulcu7ba06052011-10-16 10:28:44 +02001/**
2 * Copyright (c) 2004-2011 QOS.ch
Ceki Gulcu88c4c452009-12-03 19:16:42 +01003 * All rights reserved.
Ceki Gulcu7ba06052011-10-16 10:28:44 +02004 *
Ceki Gulcu88c4c452009-12-03 19:16:42 +01005 * 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:
Ceki Gulcu7ba06052011-10-16 10:28:44 +020012 *
Ceki Gulcu88c4c452009-12-03 19:16:42 +010013 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
Ceki Gulcu7ba06052011-10-16 10:28:44 +020015 *
Ceki Gulcu88c4c452009-12-03 19:16:42 +010016 * 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.
Ceki Gulcu7ba06052011-10-16 10:28:44 +020023 *
Ceki Gulcu88c4c452009-12-03 19:16:42 +010024 */
Ceki Gulcu88c4c452009-12-03 19:16:42 +010025package org.slf4j.profiler;
26
27import junit.framework.TestCase;
28
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
Ceki Gulcu31212432015-03-26 12:59:25 +010032public class ProfilerTest extends TestCase {
Ceki Gulcu88c4c452009-12-03 19:16:42 +010033
Ceki Gulcu31212432015-03-26 12:59:25 +010034 Logger logger = LoggerFactory.getLogger(ProfilerTest.class);
Ceki Gulcu88c4c452009-12-03 19:16:42 +010035
Ceki Gulcu31212432015-03-26 12:59:25 +010036 public void setUp() throws Exception {
37 super.setUp();
Ceki Gulcu88c4c452009-12-03 19:16:42 +010038 }
Ceki Gulcu88c4c452009-12-03 19:16:42 +010039
Ceki Gulcu31212432015-03-26 12:59:25 +010040 public void testSmoke() {
41 Profiler profiler = new Profiler("SMOKE");
42 profiler.stop();
43 StopWatch gSW = profiler.globalStopWatch;
Ceki Gulcu88c4c452009-12-03 19:16:42 +010044
Ceki Gulcu31212432015-03-26 12:59:25 +010045 // verify
46 profiler.sanityCheck();
47 assertEquals(TimeInstrumentStatus.STOPPED, gSW.status);
48 assertEquals(0, profiler.childTimeInstrumentList.size());
49 assertNull(profiler.getLastTimeInstrument());
Ceki Gulcu88c4c452009-12-03 19:16:42 +010050 }
Ceki Gulcu31212432015-03-26 12:59:25 +010051
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 }
Ceki Gulcu88c4c452009-12-03 19:16:42 +0100138}