View Javadoc

1   // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.statistics;
23  
24  
25  /**
26   * Common queries against the standard statistics.
27   *
28   * @author Philip Aston
29   */
30  public final class TestStatisticsQueries {
31  
32    private final StatisticsIndexMap.LongIndex m_errorsIndex;
33    private final StatisticsIndexMap.LongIndex m_untimedTestsIndex;
34    private final StatisticsIndexMap.LongSampleIndex m_timedTestsIndex;
35  
36    /**
37     * Constructor.
38     *
39     * @param statisticsIndexMap The index map to use.
40     */
41    TestStatisticsQueries(StatisticsIndexMap statisticsIndexMap) {
42      m_errorsIndex = statisticsIndexMap.getLongIndex("errors");
43      m_untimedTestsIndex = statisticsIndexMap.getLongIndex("untimedTests");
44      m_timedTestsIndex = statisticsIndexMap.getLongSampleIndex("timedTests");
45    }
46  
47    /**
48     * Return the number of tests. This is equal to the sum of the
49     * <em>timedTests</em> <em>count</em> value and the
50     * <em>untimedTests</em> value.
51     *
52     * @param statistics The statistics to query.
53     * @return a <code>long</code> value
54     */
55    public long getNumberOfTests(StatisticsSet statistics) {
56      return
57        statistics.getCount(m_timedTestsIndex) +
58        statistics.getValue(m_untimedTestsIndex);
59    }
60  
61    /**
62     * Return the value of the <em>errors</em> statistic.
63     *
64     * @param statistics The statistics to query.
65     * @return a <code>long</code> value
66     */
67    public long getNumberOfErrors(StatisticsSet statistics) {
68      return statistics.getValue(m_errorsIndex);
69    }
70  
71    /**
72     * Return the value obtained by dividing the <em>timedTests</em> sample
73     * statistics <em>total</em> attribute by its <em>count</em> attribute.
74     *
75     * @param statistics The statistics to query.
76     * @return a <code>double</code> value
77     */
78    public double getAverageTestTime(StatisticsSet statistics) {
79      final long count = statistics.getCount(m_timedTestsIndex);
80  
81      return
82        count == 0 ?
83        Double.NaN : statistics.getSum(m_timedTestsIndex) / (double)count;
84    }
85  }