001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.common.statistic;
017
018import static org.hamcrest.core.Is.is;
019import static org.hamcrest.number.IsCloseTo.closeTo;
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertThat;
024import static org.junit.Assert.assertTrue;
025import static org.junit.Assert.fail;
026import java.util.concurrent.TimeUnit;
027import org.junit.Before;
028import org.junit.Test;
029
030public class StopwatchTest {
031
032    private Stopwatch stopwatch;
033
034    @Before
035    public void beforeEach() {
036        this.stopwatch = new Stopwatch();
037    }
038
039    private void pause( int numberOfMilliseconds ) {
040        try {
041            Thread.sleep(numberOfMilliseconds);
042        } catch (InterruptedException e) {
043            fail("Error while sleeping for " + numberOfMilliseconds + " milliseconds");
044        }
045    }
046
047    @Test
048    public void shouldAllowStartingAndStoppingOnce() {
049        stopwatch.start();
050        pause(100);
051        stopwatch.stop();
052        assertEquals(1, stopwatch.getCount());
053    }
054
055    @Test
056    public void shouldAllowStartingAndStoppingMultipleTimes() {
057        for (int i = 0; i != 3; ++i) {
058            assertEquals(false, stopwatch.isRunning());
059            stopwatch.start();
060            assertEquals(true, stopwatch.isRunning());
061            pause(100);
062            stopwatch.stop();
063            assertEquals(false, stopwatch.isRunning());
064        }
065    }
066
067    @Test
068    public void shouldKnowWhenItsRunning() {
069        assertEquals(false, stopwatch.isRunning());
070        stopwatch.start();
071        assertEquals(true, stopwatch.isRunning());
072        stopwatch.stop();
073        assertEquals(false, stopwatch.isRunning());
074    }
075
076    @Test
077    public void shouldAllowStopToBeCalledWhenNotRunning() {
078        assertEquals(false, stopwatch.isRunning());
079        stopwatch.stop();
080        stopwatch.stop();
081        assertEquals(false, stopwatch.isRunning());
082    }
083
084    @Test
085    public void shouldAllowStartToBeCalledWhenAlreadyRunning() {
086        assertEquals(false, stopwatch.isRunning());
087        stopwatch.start();
088        assertEquals(true, stopwatch.isRunning());
089        stopwatch.start();
090        assertEquals(true, stopwatch.isRunning());
091    }
092
093    @Test
094    public void shouldReportNumberOfTimesStartedAndStopped() {
095        for (int i = 0; i != 3; ++i) {
096            stopwatch.start();
097            pause(10);
098            stopwatch.stop();
099        }
100        assertEquals(3, stopwatch.getCount());
101    }
102
103    @Test
104    public void shouldReportTotalTime() {
105        for (int i = 0; i != 4; ++i) {
106            stopwatch.start();
107            pause(100);
108            stopwatch.stop();
109        }
110        assertThat((double)stopwatch.getTotalDuration().getDuration(TimeUnit.MILLISECONDS), is(closeTo(400, 200)));
111    }
112
113    @Test
114    public void shouldReportAverageTime() {
115        for (int i = 0; i != 4; ++i) {
116            stopwatch.start();
117            pause(100);
118            stopwatch.stop();
119        }
120        assertThat((double)stopwatch.getAverageDuration().getDuration(TimeUnit.MILLISECONDS), is(closeTo(100, 50)));
121    }
122
123    @Test
124    public void shouldReportMinimumTime() {
125        for (int i = 0; i != 3; ++i) {
126            stopwatch.start();
127            pause(50 * (i + 1));
128            stopwatch.stop();
129        }
130        assertThat((double)stopwatch.getMinimumDuration().getDuration(TimeUnit.MILLISECONDS), is(closeTo(50, 20)));
131    }
132
133    @Test
134    public void shouldReportMaximumTime() {
135        for (int i = 0; i != 3; ++i) {
136            stopwatch.start();
137            pause(50 * (i + 1));
138            stopwatch.stop();
139        }
140        assertThat((double)stopwatch.getMaximumDuration().getDuration(TimeUnit.MILLISECONDS), is(closeTo(150, 50)));
141    }
142
143    @Test
144    public void shouldReportValidStatisticsEvenBeforeBeingUsed() {
145        assertEquals(0, stopwatch.getCount());
146
147        assertEquals(0.0d, stopwatch.getTotalDuration().getDuration(TimeUnit.SECONDS), 0.00001);
148        assertEquals(0.0d, stopwatch.getAverageDuration().getDuration(TimeUnit.SECONDS), 0.00001);
149        assertEquals(0.0d, stopwatch.getMinimumDuration().getDuration(TimeUnit.SECONDS), 0.00001);
150        assertEquals(0.0d, stopwatch.getMaximumDuration().getDuration(TimeUnit.SECONDS), 0.00001);
151    }
152
153    @Test
154    public void shouldReportValidStatisticsAfterBeingReset() {
155        for (int i = 0; i != 3; ++i) {
156            stopwatch.start();
157            pause(10 * (i + 1));
158            stopwatch.stop();
159        }
160
161        stopwatch.reset();
162
163        assertEquals(0, stopwatch.getCount());
164
165        assertEquals(0.0d, stopwatch.getTotalDuration().getDuration(TimeUnit.SECONDS), 0.00001);
166        assertEquals(0.0d, stopwatch.getAverageDuration().getDuration(TimeUnit.SECONDS), 0.00001);
167        assertEquals(0.0d, stopwatch.getMinimumDuration().getDuration(TimeUnit.SECONDS), 0.00001);
168        assertEquals(0.0d, stopwatch.getMaximumDuration().getDuration(TimeUnit.SECONDS), 0.00001);
169    }
170
171    @Test
172    public void shouldHaveStringRepresentationWithoutStatisticsForSingleSample() {
173        stopwatch.start();
174        pause(12);
175        stopwatch.stop();
176        String str = stopwatch.toString();
177        System.out.println(str);
178        assertTrue(str.matches("^\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
179        assertFalse(str.matches(".*1 sample.*"));
180        assertFalse(str.matches(".*min=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
181        assertFalse(str.matches(".*max=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
182        assertFalse(str.matches(".*avg=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
183        assertFalse(str.matches(".*median=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
184    }
185
186    @Test
187    public void shouldHaveStringRepresentationWithStatisticsForMultipleSample() {
188        for (int i = 0; i != 3; ++i) {
189            stopwatch.start();
190            pause(12);
191            stopwatch.stop();
192        }
193        String str = stopwatch.toString();
194        System.out.println(str);
195        assertTrue(str.matches("^\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
196        assertTrue(str.matches(".*3 samples.*"));
197        assertTrue(str.matches(".*min=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
198        assertTrue(str.matches(".*max=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
199        assertTrue(str.matches(".*avg=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
200        assertTrue(str.matches(".*median=\\d{2,}:\\d{2}:\\d{2}\\.\\d{3}(,\\d{1,3})?.*"));
201    }
202
203    @Test
204    public void shouldHaveAHistogramWithZeroSigma() {
205        for (int i = 0; i != 3; ++i) {
206            stopwatch.start();
207            pause(12);
208            stopwatch.stop();
209        }
210        assertNotNull(stopwatch.getHistogram(0));
211    }
212
213    @Test
214    public void shouldHaveAHistogramWithOneSigma() {
215        for (int i = 0; i != 3; ++i) {
216            stopwatch.start();
217            pause(12);
218            stopwatch.stop();
219        }
220        assertNotNull(stopwatch.getHistogram(1));
221    }
222
223}