View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package net.sourceforge.basher;
15  
16  import java.util.Date;
17  
18  /**
19   * @author Johan Lindquist
20   * @version 1.0
21   */
22  public class Average
23  {
24      private long _minTime;
25      private long _maxTime;
26      private long _totalTime;
27      private long _totalEntries;
28      private long _totalSuccessful;
29      private long _totalFailed;
30      private long _totalNotRun;
31      private long _timeElapsed;
32      private int _numThreads;
33      private Date _timeStamp;
34  
35      public Average(final long minTime, final long maxTime, final long totalTime, final long timeElapsed, final long totalSuccessful, final long totalFailed, final long totalNotRun, final int numThreads)
36      {
37          _minTime = minTime;
38          _maxTime = maxTime;
39          _totalTime = totalTime;
40          _totalSuccessful = totalSuccessful;
41          _totalFailed = totalFailed;
42          _totalNotRun = totalNotRun;
43  
44          _totalEntries = (totalFailed + totalSuccessful);
45  
46          _timeElapsed = timeElapsed;
47          _numThreads = numThreads;
48          _timeStamp = new Date();
49      }
50  
51      public long getTimeElapsed()
52      {
53          return _timeElapsed;
54      }
55  
56      public long getAverage()
57      {
58          if (_totalEntries == 0)
59          {
60              return 0;
61          }
62          return _totalTime / _totalEntries;
63      }
64  
65      public long getMinTime()
66      {
67          return _minTime;
68      }
69  
70      public long getMaxTime()
71      {
72          return _maxTime;
73      }
74  
75      public long getTotalTime()
76      {
77          return _totalTime;
78      }
79  
80      public long getTotalEntries()
81      {
82          return _totalEntries;
83      }
84  
85      public Date getTimeStamp()
86      {
87          return new Date(_timeStamp.getTime());
88      }
89  
90      public float getTPS()
91      {
92          final float seconds = (_timeElapsed / 1000);
93          if (seconds == 0)
94          {
95              return 0f;
96          }
97          return _totalEntries / seconds;
98      }
99  
100     public int getNumThreads()
101     {
102         return _numThreads;
103     }
104 
105     public long getTotalSuccessful()
106     {
107         return _totalSuccessful;
108     }
109 
110     public long getTotalFailed()
111     {
112         return _totalFailed;
113     }
114 
115     public long getTotalNotRun()
116     {
117         return _totalNotRun;
118     }
119 
120     public String formatOutput()
121     {
122         return String.format( "elapsed time: %d min time: %s max time: %s total time: %s invocations: %d (%dS/%dF) not run: %d average time: %d tps: %f num threads: %d",
123                 _timeElapsed, (_minTime == Long.MAX_VALUE ? "N/A" : _minTime), _maxTime,_totalTime,
124                 _totalEntries,_totalSuccessful, _totalFailed, _totalNotRun, getAverage(),getTPS(),getNumThreads()
125         );
126     }
127 
128     public String toString()
129     {
130         return "elapsed time: " + _timeElapsed + " min time: " + (_minTime == Long.MAX_VALUE ? "N/A" : "" + _minTime)
131                 + " max time: " + _maxTime + " total time: " + _totalTime + " invocations: " + _totalEntries + " average time: "
132                 + getAverage() + " tps: " + getTPS() + " num threads: " + getNumThreads();
133     }
134 
135     @Override
136     public boolean equals( final Object o )
137     {
138         if ( this == o )
139         {
140             return true;
141         }
142         if ( o == null || getClass() != o.getClass() )
143         {
144             return false;
145         }
146 
147         final Average average = ( Average ) o;
148 
149         if ( _maxTime != average._maxTime )
150         {
151             return false;
152         }
153         if ( _minTime != average._minTime )
154         {
155             return false;
156         }
157         if ( _numThreads != average._numThreads )
158         {
159             return false;
160         }
161         if ( _timeElapsed != average._timeElapsed )
162         {
163             return false;
164         }
165         if ( _totalEntries != average._totalEntries )
166         {
167             return false;
168         }
169         if ( _totalFailed != average._totalFailed )
170         {
171             return false;
172         }
173         if ( _totalNotRun != average._totalNotRun )
174         {
175             return false;
176         }
177         if ( _totalSuccessful != average._totalSuccessful )
178         {
179             return false;
180         }
181         if ( _totalTime != average._totalTime )
182         {
183             return false;
184         }
185         if ( _timeStamp != null ? !_timeStamp.equals( average._timeStamp ) : average._timeStamp != null )
186         {
187             return false;
188         }
189 
190         return true;
191     }
192 }