View Javadoc

1   package net.obsearch.stats;
2   
3   import hep.aida.bin.StaticBin1D;
4   
5   import java.util.HashMap;
6   import java.util.Map.Entry;
7   
8   import net.obsearch.index.utils.IntegerHolder;
9   import net.obsearch.index.utils.StatsUtil;
10  
11  /*
12   OBSearch: a distributed similarity search engine This project is to
13   similarity search what 'bit-torrent' is to downloads. 
14   Copyright (C) 2008 Arnoldo Jose Muller Molina
15  
16   This program is free software: you can redistribute it and/or modify
17   it under the terms of the GNU General Public License as published by
18   the Free Software Foundation, either version 3 of the License, or
19   (at your option) any later version.
20  
21   This program is distributed in the hope that it will be useful,
22   but WITHOUT ANY WARRANTY; without even the implied warranty of
23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24   GNU General Public License for more details.
25  
26   You should have received a copy of the GNU General Public License
27   along with this program.  If not, see <http://www.gnu.org/licenses/>.
28   */
29  
30  /**
31   * Statistics gathered during the execution of a program.
32   * @author Arnoldo Jose Muller Molina
33   */
34  
35  public class Statistics {
36  	
37  	/**
38  	 * Additional counters.
39  	 */
40  	private HashMap<String, IntegerHolder> extra = new HashMap<String, IntegerHolder>();
41  	
42  	
43  	/**
44  	 * Additional information regarding the index.
45  	 */
46  	private HashMap<String, StaticBin1D> extraStats = new HashMap<String, StaticBin1D>();
47  	
48  	
49  	/**
50  	 * Additional information regarding the index.
51  	 */
52  	private HashMap<String, Object> extraObjects = new HashMap();
53  
54      /**
55       * # of distance computations.
56       */
57      private long distanceCount = 0;
58      /**
59       * Number of SMAP vectors searched.
60       */
61      private long smapCount = 0;
62      
63      /**
64       * Amount of reads.
65       */
66      private long diskAccessCount = 0;
67      
68      /**
69       * Queries performed so far.
70       */
71      private long queryCount = 0; 
72      
73      /**
74       * Amount of data read.
75       */
76      private long dataRead = 0;
77      
78      /**
79       * # of buckets read
80       */
81      private long bucketsRead = 0;
82      
83      
84      
85      /**
86       * Adds x to the current value of {@link #bucketsRead}.
87       * @param x
88       */
89      public void incBucketsRead(long x){
90          bucketsRead += x;
91      }
92      
93      /**
94       * Increment the # of buckets read.
95       */
96      public void incBucketsRead(){
97          bucketsRead++;
98      }
99      
100     /**
101      * Increment an extra value.
102      * @param key
103      */
104     public void incExtra(String key){
105     	incExtra(key, 1);
106     }
107     
108     /**
109      * Increment an extra value.
110      * @param key
111      */
112     public void incExtra(String key, int value){
113     	IntegerHolder i = extra.get(key);
114     	if(i == null){
115     		i = new IntegerHolder(0);
116     		extra.put(key, i);
117     	}
118     	i.add(value);
119     }
120     
121     /**
122      * Increment an extra value.
123      * @param key
124      */
125     public void setExtra(String key, int value){
126     	IntegerHolder i = extra.get(key);
127     	if(i == null){
128     		i = new IntegerHolder(0);
129     		extra.put(key, i);
130     	}
131     	i.setValue(value);
132     }
133     
134     /**
135      * Increment an extra value.
136      * @param key
137      */
138     public int getExtra(String key){
139     	IntegerHolder i = extra.get(key);
140     	if(i == null){
141     		return 0;
142     	}
143     	return i.getValue();
144     }
145     
146     /**
147      * Increment distance count by distance.
148      */
149     public void incDistanceCount(long distance) {
150         distanceCount+= distance;
151     }
152     
153     /**
154      * Increment distance count.
155      */
156     public void incDistanceCount() {
157         distanceCount++;
158     }
159     
160     /**
161      * Increment smap count.
162      */
163     public void incSmapCount() {
164         smapCount++;
165     }
166     
167     /**
168      * Increment smap count by count.
169      * @param count the amount that will be increased
170      */
171     public void incSmapCount(long count) {
172         smapCount += count;
173     }
174     
175     /**
176      * Increment disk access count.
177      */
178     public void incDiskAccessCount() {
179         diskAccessCount++;
180     }
181     
182     /**
183      * Increment query count.
184      */
185     public void incQueryCount() {
186         queryCount++;
187     }
188     
189     /**
190      * Increment data read
191      * @param dataRead The amount read.
192      */
193     public void incDataRead(long dataRead) {
194         this.dataRead += dataRead;
195     }
196     
197 
198     /**
199      * @return the distanceCount
200      */
201     public long getDistanceCount() {
202         return distanceCount;
203     }
204 
205     /**
206      * @param distanceCount the distanceCount to set
207      */
208     public void setDistanceCount(long distanceCount) {
209         this.distanceCount = distanceCount;
210     }
211 
212     /**
213      * @return the smapCount
214      */
215     public long getSmapCount() {
216         return smapCount;
217     }
218 
219     /**
220      * @param smapCount the smapCount to set
221      */
222     public void setSmapCount(long smapCount) {
223         this.smapCount = smapCount;
224     }
225 
226     /**
227      * @return the diskAccessCount
228      */
229     public long getDiskAccessCount() {
230         return diskAccessCount;
231     }
232 
233     /**
234      * @param diskAccessCount the diskAccessCount to set
235      */
236     public void setDiskAccessCount(long diskAccessCount) {
237         this.diskAccessCount = diskAccessCount;
238     }
239 
240     /**
241      * @return the queryCount
242      */
243     public long getQueryCount() {
244         return queryCount;
245     }
246 
247     /**
248      * @param queryCount the queryCount to set
249      */
250     public void setQueryCount(long queryCount) {
251         this.queryCount = queryCount;
252     }
253 
254     /**
255      * @return the dataRead
256      */
257     public long getDataRead() {
258         return dataRead;
259     }
260 
261     /**
262      * @param dataRead the dataRead to set
263      */
264     public void setDataRead(long dataRead) {
265         this.dataRead = dataRead;
266     } 
267     
268     /**
269      * Reset all the counters to 0.
270      */
271     public void resetStats(){
272         distanceCount = 0;
273         smapCount = 0;        
274         diskAccessCount = 0;        
275         queryCount = 0;         
276         dataRead = 0;
277         this.bucketsRead = 0;
278         extra = new HashMap<String, IntegerHolder>();
279 				extraObjects = new HashMap();
280 				extraStats = new HashMap<String, StaticBin1D>();
281     }
282     
283     /**
284      * Add the given set of statistics.
285      * @param name Name of the statistics
286      * @param stats The stats.
287      */
288     public void putStats(String name, StaticBin1D stats){
289     	this.extraStats.put(name, stats);
290     }
291     
292     public StaticBin1D getStats(String name){
293     	StaticBin1D r = extraStats.get(name);
294     	if(r != null){
295     		return r;
296     	}else{
297     		return new StaticBin1D();
298     	}
299     }
300     
301     /**
302      * Add values to a stats bin, each value
303      * added will be considered as a unit and 
304      * median, std. dev will be calculated on the set
305      * of values added
306      * @param name Name of  the stat
307      * @param value Value to add
308      */
309     public void addExtraStats(String name, double value){
310     	StaticBin1D s = extraStats.get(name);
311     	if(s == null){
312     		s = new StaticBin1D();
313     		extraStats.put(name, s);
314     	}
315     	s.add(value);
316     }
317     
318     /**
319      * Add arbitrary info.
320      * @param name Name of the statistics
321      * @param stats The stats.
322      */
323     public void putObjects(String name, Object stats){
324     	this.extraObjects.put(name, stats);
325     }
326     
327     public String toStringSummary(){
328     	return "Distances: " + distanceCount +
329         " Pivot vector count: " + smapCount +        
330         " Disk access count: " + diskAccessCount +        
331     " Query count: " + queryCount +         
332     " Data read: " + dataRead +
333     " Buckets Read: " + this.bucketsRead + " extra: " + extra;
334     }
335     
336     
337     
338     public long getBucketsRead() {
339 		return bucketsRead;
340 	}
341 
342 	public String toString(){
343         StringBuilder res = new StringBuilder( toStringSummary() );
344         for(Entry<String, StaticBin1D> e : extraStats.entrySet()){
345         	res.append(StatsUtil.prettyPrintStats(e.getKey(), e.getValue()));
346         }
347         for(Entry<String, Object> e : extraObjects.entrySet()){
348         	res.append(e.getKey());
349         	res.append("\n");
350         	res.append(e.getValue());
351         }
352         return res.toString();
353     }
354     
355     
356 }