1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
40 public void testSmoke() {
41 Profiler profiler = new Profiler("SMOKE");
42 profiler.stop();
43 StopWatch gSW = profiler.globalStopWatch;
44
45
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
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
75
76
77
78
79
80
81
82
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
101 profiler.sanityCheck();
102 StopWatch gSW = profiler.globalStopWatch;
103 assertEquals(TimeInstrumentStatus.STOPPED, gSW.status);
104
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 }