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 public void testSmoke() {
40 Profiler profiler = new Profiler("SMOKE");
41 profiler.stop();
42 StopWatch gSW = profiler.globalStopWatch;
43
44
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
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
74
75
76
77
78
79
80
81
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
100 profiler.sanityCheck();
101 StopWatch gSW = profiler.globalStopWatch;
102 assertEquals(TimeInstrumentStatus.STOPPED, gSW.status);
103
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 }