Changeset 085ca76
 Timestamp:
 Jul 20, 2011 1:27:44 PM (9 years ago)
 Branches:
 master
 Children:
 6e98cf4f, 73f9b72
 Parents:
 9a6888d
 Location:
 core/java/src/net/i2p/stat
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

core/java/src/net/i2p/stat/Rate.java
r9a6888d r085ca76 120 120 121 121 /** 122 * 122 * A rate with period shorter than Router.COALESCE_TIME = 50*1000 has to 123 * be manually coalesced before values are fetched from it. 123 124 * @param period number of milliseconds in the period this rate deals with 124 125 * @throws IllegalArgumentException if the period is not greater than 0 
core/java/src/net/i2p/stat/RateStat.java
r9a6888d r085ca76 4 4 import java.io.OutputStream; 5 5 import java.util.Arrays; 6 import java.util.Map.Entry; 6 7 import java.util.Properties; 8 import java.util.concurrent.ConcurrentHashMap; 7 9 8 10 import net.i2p.data.DataHelper; 11 import net.i2p.util.ConcurrentHashSet; 9 12 import net.i2p.util.Log; 10 13 … … 19 22 private final String _description; 20 23 /** actual rate objects for this statistic */ 21 private final Rate _rates[];24 private final ConcurrentHashMap<Long, Rate> _rates; 22 25 /** component we tell about events as they occur */ 23 26 private StatLog _statLog; … … 27 30 _description = description; 28 31 _groupName = group; 29 _rates = new Rate[periods.length];32 _rates = new ConcurrentHashMap<Long, Rate>(); 30 33 for (int i = 0; i < periods.length; i++) { 31 _rates[i] = new Rate(periods[i]); 32 _rates[i].setRateStat(this); 34 Rate rate = new Rate(periods[i]); 35 rate.setRateStat(this); 36 _rates.put(rate.getPeriod(),rate);; 33 37 } 34 38 } … … 40 44 public void addData(long value, long eventDuration) { 41 45 if (_statLog != null) _statLog.addData(_groupName, _statName, value, eventDuration); 42 for ( int i = 0; i < _rates.length; i++)43 _rates[i].addData(value, eventDuration);46 for (Entry<Long, Rate> e: _rates.entrySet()) 47 e.getValue().addData(value, eventDuration); 44 48 } 45 49 46 50 /** coalesce all the stats */ 47 51 public void coalesceStats() { 48 for (int i = 0; i < _rates.length; i++) 49 _rates[i].coalesce(); 52 for (Entry<Long, Rate> e: _rates.entrySet()){ 53 e.getValue().coalesce(); 54 if (_statName == "bw.sendRate"){ 55 System.out.println("Coalescing Rate(" + e.getValue().getPeriod() + ")"); 56 } 57 } 50 58 } 51 59 … … 63 71 64 72 public long[] getPeriods() { 65 long rv[] = new long[_rates.length]; 66 for (int i = 0; i < _rates.length; i++) 67 rv[i] = _rates[i].getPeriod(); 73 long rv[] = new long[_rates.size()]; 74 int counter = 0; 75 for (Entry<Long, Rate> e: _rates.entrySet()) 76 rv[counter++] = e.getValue().getPeriod(); 68 77 return rv; 69 78 } 70 79 71 80 public double getLifetimeAverageValue() { 72 if ( (_rates == null)  (_rates. length<= 0) ) return 0;73 return _rates [0].getLifetimeAverageValue();81 if ( (_rates == null)  (_rates.size() <= 0) ) return 0; 82 return _rates.entrySet().iterator().next().getValue().getLifetimeAverageValue(); 74 83 } 75 84 public long getLifetimeEventCount() { 76 if ( (_rates == null)  (_rates.length <= 0) ) return 0; 77 return _rates[0].getLifetimeEventCount(); 78 } 79 85 if ( (_rates == null)  (_rates.size() <= 0) ) return 0; 86 return _rates.entrySet().iterator().next().getValue().getLifetimeEventCount(); 87 } 88 89 /** 90 * Returns rate with requested period if it exists, 91 * otherwise creates new rate with requested period, adds it to list of rates and returns it. 92 * @param period 93 * @return 94 */ 80 95 public Rate getRate(long period) { 81 for (int i = 0; i < _rates.length; i++) { 82 if (_rates[i].getPeriod() == period) return _rates[i]; 83 } 84 return null; 96 if (_rates.containsKey(period)){ 97 return _rates.get(period); 98 } else { 99 Rate rate = new Rate(period); 100 rate.setRateStat(this); 101 System.out.println("New Rate(" + _statName + ", " + rate.getPeriod() + ")"); 102 _rates.put(period, rate); 103 return rate; 104 } 105 } 106 107 /** 108 * Adds a new rate with the requested period, provided that 109 * a rate with that period does not already exist. 110 * @param period 111 */ 112 public void addRate(long period) { 113 if (!_rates.containsKey(period)){ 114 Rate rate = new Rate(period); 115 rate.setRateStat(this); 116 _rates.put(period, rate); 117 } 118 } 119 120 /** 121 * If a rate with the provided period exists, remove it. 122 * @param period 123 */ 124 public void removeRate(long period) { 125 _rates.remove(period); 126 } 127 128 /** 129 * Tests if a rate with the provided period exists within this RateStat. 130 * @param period 131 * @return 132 */ 133 public boolean containsRate(long period) { 134 return _rates.containsKey(period); 85 135 } 86 136 … … 113 163 if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription()) 114 164 && DataHelper.eq(getName(), rs.getName())) { 115 for ( int i = 0; i < _rates.length; i++)116 if (! _rates[i].equals(rs.getRate(_rates[i].getPeriod()))) return false;165 for (Entry<Long, Rate> e: _rates.entrySet()) 166 if (!e.getValue().equals(rs.getRate(e.getValue().getPeriod()))) return false; 117 167 return true; 118 168 } … … 130 180 out.write(buf.toString().getBytes()); 131 181 buf.setLength(0); 132 for ( int i = 0; i < _rates.length; i++){182 for (Entry<Long, Rate> e: _rates.entrySet()){ 133 183 buf.append("#######").append(NL); 134 buf.append("# Period : ").append(DataHelper.formatDuration( _rates[i].getPeriod())).append(" for rate ")184 buf.append("# Period : ").append(DataHelper.formatDuration(e.getValue().getPeriod())).append(" for rate ") 135 185 .append(_groupName).append("  ").append(_statName).append(NL); 136 186 buf.append(NL); 137 String curPrefix = prefix + "." + DataHelper.formatDuration( _rates[i].getPeriod());138 _rates[i].store(curPrefix, buf);187 String curPrefix = prefix + "." + DataHelper.formatDuration(e.getValue().getPeriod()); 188 e.getValue().store(curPrefix, buf); 139 189 out.write(buf.toString().getBytes()); 140 190 buf.setLength(0); … … 157 207 */ 158 208 public void load(Properties props, String prefix, boolean treatAsCurrent) throws IllegalArgumentException { 159 for ( int i = 0; i < _rates.length; i++) {160 long period = _rates[i].getPeriod();209 for (Entry<Long, Rate> e: _rates.entrySet()) { 210 long period = e.getValue().getPeriod(); 161 211 String curPrefix = prefix + "." + DataHelper.formatDuration(period); 162 212 try { 163 _rates[i].load(props, curPrefix, treatAsCurrent);213 e.getValue().load(props, curPrefix, treatAsCurrent); 164 214 } catch (IllegalArgumentException iae) { 165 _rates[i] = new Rate(period); 166 _rates[i].setRateStat(this); 215 Rate rate = new Rate(period); 216 rate.setRateStat(this); 217 _rates.put(rate.getPeriod(), rate); 167 218 if (_log.shouldLog(Log.WARN)) 168 219 _log.warn("Rate for " + prefix + " is corrupt, reinitializing that period"); … … 172 223 173 224 /********* 225 */ 174 226 public static void main(String args[]) { 175 227 RateStat rs = new RateStat("moo", "moo moo moo", "cow trueisms", new long[] { 60 * 1000, 60 * 60 * 1000, 176 228 24 * 60 * 60 * 1000}); 177 for (int i = 0; i < 50; i++) { 229 230 rs.getRate(5500L); 231 System.out.println("Adding data.."); 232 for (int i = 0; i < 500; i++) { 178 233 try { 179 234 Thread.sleep(20); … … 182 237 rs.addData(i * 100, 20); 183 238 } 239 240 Rate rate5500 = rs.getRate(5500L); 241 System.out.println("Nbr of events: " + rate5500.getCurrentEventCount()); 242 System.out.println("Average :" + rate5500.getAverageValue()); 243 244 System.out.println("Coalescing stats.."); 184 245 rs.coalesceStats(); 246 System.out.println("Average :" + rate5500.getAverageValue()); 247 System.out.println("Coalescing this rate.."); 248 rate5500.coalesce(); 249 System.out.println("Average :" + rate5500.getAverageValue()); 250 System.out.println("Lifetime average :" + rate5500.getLifetimeAverageValue()); 251 185 252 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(2048); 186 253 try { … … 208 275 } 209 276 } 277 /** 210 278 *********/ 211 279 }
Note: See TracChangeset
for help on using the changeset viewer.