View Javadoc

1   /* ==========================================
2    * JperfProbe : Java Performance Probes
3    * ==========================================
4    *
5    * Project Info:  http://jperfprobe.sourceforge.net/
6    * Project Lead:  Tor-Erik Larsen (http://sourceforge.net/users/uptime62)
7    *
8    * (C) Copyright 2005, by Tor-Erik Larsen and Contributors.
9    *
10   * This library is free software; you can redistribute it and/or modify it
11   * under the terms of the GNU Lesser General Public License as published by
12   * the Free Software Foundation; either version 2.1 of the License, or
13   * (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful, but
16   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17   * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18   * License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23   */
24  package net.sf.jperfprobe;
25  
26  import static org.junit.Assert.*;
27  import org.junit.*;
28  import org.slf4j.*;
29  
30  public class ResultTest {
31      final Logger log = LoggerFactory.getLogger(ResultTest.class);
32  
33      @Test
34      public void testGetName() {
35          Result result = new Result("PROMP");
36  
37          assertEquals("Wrong name", "PROMP", result.getName());
38  
39      }
40  
41      @Test
42      public void testNumberOfSamples() {
43          Result result = new Result("PROMP");
44  
45          for (int i = 0; i < 623; i++) {
46              result.addSample(10);
47          }
48  
49          assertEquals("Number of collected samples is wrong", 623, result.getNSamples());
50  
51          result.addSample(234L);
52          assertEquals("wrong last sample", 234L, result.getLastSample());
53      }
54  
55      @Test
56      public void testTimingAverageValue() {
57          Result result = new Result("PROMP");
58  
59          for (int i = 0; i < 5; i++) {
60              result.addSample(5);
61  
62          }
63  
64          assertEquals("The result average is wrong", 5.0, result.getAverage(), 0.0);
65          result.addSample(5);
66      }
67  
68      @Test
69      public void testgetSamples() {
70          Result res = new Result("START");
71          assertEquals("Number of samples should be 0", 0, res.getNSamples());
72          long testSamples[] = new long[100];
73          for (int i = 0; i < 100; i++) {
74              testSamples[i] = i;
75              res.addSample(i);
76          }
77  
78          assertEquals("Number of samples should be 100", 100, res.getNSamples());
79  
80          assertArrayEquals("wrong samples", testSamples, res.getSamples());
81      }
82  
83      @Test
84      public void testAverageCalculation() {
85          Result p = new Result("probe");
86          assertEquals("wrong average sample120l", 0.0d, p.getAverage(), 0.0);
87  
88          p.addSample(120l);
89          assertEquals("wrong number of samples", 1, p.getNSamples());
90          assertEquals("wrong average sample120l", 120.0d, p.getAverage(), 0.0);
91          p.addSample(120l);
92          assertEquals("wrong number of samples", 2, p.getNSamples());
93          assertEquals("wrong average sample120l", 120.0d, p.getAverage(), 0.0);
94          p.addSample(60l);
95          assertEquals("wrong number of samples", 3, p.getNSamples());
96          assertEquals("wrong average sample120l", 100.0d, p.getAverage(), 0.0);
97          p.addSample(60l);
98          assertEquals("wrong number of samples", 4, p.getNSamples());
99          assertEquals("wrong average sample120l", 90.0d, p.getAverage(), 0.0);
100         long[] samples = p.getSamples();
101         assertEquals("wrong sample120l value:", 120l, samples[0]);
102         assertEquals("wrong sample120l value:", 120l, samples[1]);
103         assertEquals("wrong sample120l value:", 60l, samples[2]);
104         assertEquals("wrong sample120l value:", 60l, samples[3]);
105 
106     }
107 
108     @Test
109     public void testMinMax() {
110         Result p = new Result("test");
111 
112         p.addSample(120l);
113         p.addSample(60l);
114         assertEquals("wrong number of samples", 2, p.getNSamples());
115         assertEquals("wrong max sample", 120.0, p.getMax(), 0.0);
116         assertEquals("wrong min sample", 60.0, p.getMin(), 0.0);
117 
118         p.addSample(100l);
119         assertEquals("wrong number of samples", 3, p.getNSamples());
120         assertEquals("wrong max sample", 120.0, p.getMax(), 0.0);
121         assertEquals("wrong min sample", 60.0, p.getMin(), 0.0);
122 
123         p.addSample(200l);
124         assertEquals("wrong number of samples", 4, p.getNSamples());
125         assertEquals("wrong max sample", 200.0, p.getMax(), 0.0);
126         assertEquals("wrong min sample", 60.0, p.getMin(), 0.0);
127 
128     }
129 
130     @Test
131     public void testSamplesToSkip() {
132         Result result = new Result(3, "PROMP");
133 
134         result.addSample(10);
135         result.addSample(10);
136         result.addSample(10);
137 
138         assertResult("wrong result", 0.0, 0.0, 0.0, 0, result);
139 
140         result.addSample(42);
141         assertResult("wrong result", 42.0, 42.0, 42.0, 1, result);
142 
143         result.addSample(42);
144         assertResult("wrong result", 42.0, 42.0, 42.0, 2, result);
145 
146         result.addSample(6);
147         assertResult("wrong result", 42.0, 6.0, 30.0, 3, result);
148     }
149 
150     @Test
151     public void testTotals() {
152         Result result = new Result("PROMP");
153 
154         result.addSample(11);
155         result.addSample(12);
156         result.addSample(13);
157 
158         assertEquals("wrong totals", 36, result.getTotal());
159 
160         result = new Result("PROMP");
161         assertEquals("wrong totals", 0, result.getTotal());
162     }
163 
164     @Test
165     public void testManyTotals() {
166         Result result = new Result("PROMP");
167         int madmax = 100000;
168         long total = 0l;
169         for (int i = 0; i < madmax; i++) {
170             total += i;
171             result.addSample(i);
172         }
173 
174         assertEquals("wrong totals", total, result.getTotal());
175     }
176 
177     @Test
178     public void testStdevCalculation() {
179         Result p = new Result("probe");
180         p.addSample(3);
181         p.addSample(7);
182         p.addSample(7);
183         p.addSample(19);
184         assertEquals("wrong stdev calculation", 6.928203230275509d, p.getStdev(), 0.0);
185     }
186 
187 
188     public static void assertResult(String message, double max, double min, double average, int nSamples, Result result) {
189         assertEquals(message + " wrong max value", (double)max, result.getMax(), 0.0);
190         assertEquals(message + " wrong min value", (double)min, result.getMin(), 0.0);
191         assertEquals(message + " wrong average value", (double)average, result.getAverage(), 0.0);
192         assertEquals(message + " wrong # samples", nSamples, result.getNSamples());
193     }
194 }