View Javadoc

1   // Copyright (C) 2003 - 2009 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.console.model;
23  
24  import net.grinder.statistics.PeakStatisticExpression;
25  import net.grinder.statistics.StatisticsSet;
26  import net.grinder.statistics.StatisticsIndexMap;
27  import net.grinder.statistics.StatisticsSetFactory;
28  import net.grinder.util.ListenerSupport;
29  
30  
31  /**
32   * Manages the cumulative statistics for a single test or set of tests.
33   *
34   * @author Philip Aston
35   */
36  final class SampleAccumulator {
37  
38    private final ListenerSupport<SampleListener> m_listeners =
39      new ListenerSupport<SampleListener>();
40  
41    private final PeakStatisticExpression m_peakTPSExpression;
42    private final StatisticsIndexMap.LongIndex m_periodIndex;
43    private final StatisticsSetFactory m_statisticsSetFactory;
44  
45    private final StatisticsSet m_cumulativeStatistics;
46    private StatisticsSet m_intervalStatistics;
47    private StatisticsSet m_lastSampleStatistics;
48  
49    public SampleAccumulator(PeakStatisticExpression peakTPSExpression,
50                             StatisticsIndexMap.LongIndex periodIndex,
51                             StatisticsSetFactory statisticsSetFactory) {
52  
53      m_peakTPSExpression = peakTPSExpression;
54      m_periodIndex = periodIndex;
55      m_statisticsSetFactory = statisticsSetFactory;
56  
57      m_cumulativeStatistics = m_statisticsSetFactory.create();
58      m_intervalStatistics = m_statisticsSetFactory.create();
59      m_lastSampleStatistics = m_statisticsSetFactory.create();
60    }
61  
62    public void addSampleListener(SampleListener listener) {
63      m_listeners.add(listener);
64    }
65  
66    public void addIntervalStatistics(StatisticsSet report) {
67      m_intervalStatistics.add(report);
68    }
69  
70    public void addCumulativeStaticstics(StatisticsSet report) {
71      m_cumulativeStatistics.add(report);
72    }
73  
74    public void fireSample(long sampleInterval, long period) {
75  
76      m_intervalStatistics.setValue(m_periodIndex, sampleInterval);
77      m_cumulativeStatistics.setValue(m_periodIndex, period);
78  
79      m_peakTPSExpression.update(m_intervalStatistics, m_cumulativeStatistics);
80  
81      m_listeners.apply(
82        new ListenerSupport.Informer<SampleListener>() {
83          public void inform(SampleListener l) {
84            l.update(m_intervalStatistics, m_cumulativeStatistics);
85          }
86        });
87  
88      m_lastSampleStatistics = m_intervalStatistics;
89  
90      // We create new statistics each time to ensure that
91      // m_lastSampleStatistics is always valid and fixed.
92      m_intervalStatistics = m_statisticsSetFactory.create();
93    }
94  
95    public void zero() {
96      m_intervalStatistics.reset();
97      m_lastSampleStatistics.reset();
98      m_cumulativeStatistics.reset();
99    }
100 
101   public StatisticsSet getLastSampleStatistics() {
102     return m_lastSampleStatistics;
103   }
104 
105   public StatisticsSet getCumulativeStatistics() {
106     return m_cumulativeStatistics;
107   }
108 }