View Javadoc

1   // Copyright (C) 2006 - 2012 Philip Aston
2   // Copyright (C) 2012 Marc Holden
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package net.grinder.console.model;
24  
25  import java.util.Collection;
26  import java.util.EventListener;
27  import java.util.Set;
28  
29  import net.grinder.common.Test;
30  import net.grinder.statistics.StatisticExpression;
31  import net.grinder.statistics.StatisticsSet;
32  import net.grinder.statistics.TestStatisticsMap;
33  
34  
35  /**
36   * Interface to {@link SampleModelImplementation}.
37   *
38   * @author Philip Aston
39   */
40  public interface SampleModel {
41  
42    /**
43     * Discard all of the information held by the model.
44     *
45     * <p>This doesn't change the model state.</p>
46     */
47    void reset();
48  
49    /**
50     * Zero the statistics.
51     *
52     * @since 3.10
53     */
54    void zeroStatistics();
55  
56    /**
57     * Start the model.
58     */
59    void start();
60  
61    /**
62     * Stop the model.
63     */
64    void stop();
65  
66    /**
67     * Get the current model state.
68     *
69     * @return The model state.
70     */
71    State getState();
72  
73    /**
74     * A snapshot of the model state.
75     *
76     * @see SampleModel#getState()
77     */
78    interface State {
79      /**
80       * The primary state.
81       */
82      enum Value {
83        /** Stopped - all test reports are ignored. */
84        Stopped,
85  
86        /** Waiting for the first test report to start the sampling. */
87        WaitingForFirstReport,
88  
89        /** Waiting until the configured number of samples have been ignored. */
90        IgnoringInitialSamples,
91  
92        /** Capturing samples. */
93        Recording;
94      }
95  
96      Value getValue();
97  
98      /**
99       * A presentable description of the state.
100      *
101      * @return The description.
102      */
103     String getDescription();
104 
105     /**
106      * Return the sample count.
107      *
108      * @return The sample count.
109      */
110     long getSampleCount();
111   }
112 
113   /**
114    * Get the statistics expression for TPS.
115    *
116    * @return The TPS expression for this model.
117    */
118   StatisticExpression getTPSExpression();
119 
120   /**
121    * Get the expression for peak TPS.
122    *
123    * @return The peak TPS expression for this model.
124    */
125   StatisticExpression getPeakTPSExpression();
126 
127   /**
128    * Get the cumulative statistics for this model.
129    *
130    * @return The cumulative statistics.
131    */
132   StatisticsSet getTotalCumulativeStatistics();
133 
134   /**
135    * Get the total statistics for the latest sample.
136    *
137    * @return The cumulative statistics.
138    */
139   StatisticsSet getTotalLatestStatistics();
140 
141   /**
142    * Add a new model listener.
143    *
144    * @param listener The listener.
145    */
146   void addModelListener(Listener listener);
147 
148   /**
149    * Add a new total sample listener.
150    *
151    * @param listener The sample listener.
152    */
153   void addTotalSampleListener(SampleListener listener);
154 
155   /**
156    * Add a new sample listener for the specific test.
157    *
158    * @param test The test to add the sample listener for.
159    * @param listener The sample listener.
160    */
161   void addSampleListener(Test test, SampleListener listener);
162 
163   /**
164    * Register new tests.
165    *
166    * @param tests The new tests.
167    */
168   void registerTests(Collection<Test> tests);
169 
170   /**
171    * Add a new test report.
172    *
173    * @param statisticsDelta The new test statistics.
174    */
175   void addTestReport(TestStatisticsMap statisticsDelta);
176 
177 
178   /**
179    * Interface for listeners to {@link SampleModelImplementation}.
180    */
181   interface Listener extends EventListener {
182 
183     /**
184      * Called when the model state has changed.
185      */
186     void stateChanged();
187 
188     /**
189      * Called when the model has a new sample.
190      */
191     void newSample();
192 
193     /**
194      * Called when new tests have been added to the model.
195      *
196      * @param newTests The new tests.
197      * @param modelTestIndex New index structure for the model's tests.
198      */
199     void newTests(Set<Test> newTests, ModelTestIndex modelTestIndex);
200 
201     /**
202      * Called when existing tests and statistics views should be
203      * discarded.
204      */
205     void resetTests();
206   }
207 
208   /**
209    * Skeleton implementation of {@link SampleModel.Listener}.
210    */
211   abstract class AbstractListener implements Listener {
212 
213     @Override
214     public void newSample() { }
215 
216     @Override
217     public void newTests(final Set<Test> newTests,
218                          final ModelTestIndex modelTestIndex) { }
219 
220     @Override
221     public void resetTests() { }
222 
223     @Override
224     public void stateChanged() { }
225   }
226 }