Changeset d062db3
 Timestamp:
 Dec 10, 2012 9:46:05 AM (7 years ago)
 Branches:
 master
 Children:
 d2b2600
 Parents:
 32a8bb7
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

core/java/src/net/i2p/stat/RateStat.java
r32a8bb7 rd062db3 3 3 import java.io.IOException; 4 4 import java.io.OutputStream; 5 import java.util.Arrays;5 import static java.util.Arrays.*; 6 6 import java.util.Properties; 7 import java.util.concurrent.ConcurrentHashMap; 8 9 import net.i2p.I2PAppContext; 7 10 8 import net.i2p.data.DataHelper; 11 import net.i2p.util.Log;12 9 13 10 /** coordinate a moving rate over various periods */ … … 20 17 private final String _description; 21 18 /** actual rate objects for this statistic */ 22 protected final ConcurrentHashMap<Long, Rate>_rates;19 protected final Rate[] _rates; 23 20 /** component we tell about events as they occur */ 24 21 private StatLog _statLog; … … 28 25 _description = description; 29 26 _groupName = group; 30 _rates = new ConcurrentHashMap<Long, Rate>(4); 31 for (int i = 0; i < periods.length; i++) { 32 Rate rate = new Rate(periods[i]); 27 if (periods.length == 0) 28 throw new IllegalArgumentException(); 29 30 long [] periodsCopy = copyOf(periods, periods.length); 31 sort(periodsCopy); 32 33 _rates = new Rate[periodsCopy.length]; 34 for (int i = 0; i < periodsCopy.length; i++) { 35 Rate rate = new Rate(periodsCopy[i]); 33 36 rate.setRateStat(this); 34 _rates .put(rate.getPeriod(),rate);37 _rates[i] = rate; 35 38 } 36 39 } … … 42 45 public void addData(long value, long eventDuration) { 43 46 if (_statLog != null) _statLog.addData(_groupName, _statName, value, eventDuration); 44 for (Rate r: _rates .values())47 for (Rate r: _rates) 45 48 r.addData(value, eventDuration); 46 49 } … … 53 56 public void addData(long value) { 54 57 if (_statLog != null) _statLog.addData(_groupName, _statName, value, 0); 55 for (Rate r: _rates .values())58 for (Rate r: _rates) 56 59 r.addData(value); 57 60 } … … 59 62 /** coalesce all the stats */ 60 63 public void coalesceStats() { 61 for (Rate r: _rates .values()){64 for (Rate r: _rates) 62 65 r.coalesce(); 63 }64 66 } 65 67 … … 77 79 78 80 public long[] getPeriods() { 79 long rv[] = new long[_rates.size()]; 80 int counter = 0; 81 for (Rate r: _rates.values()) 82 rv[counter++] = r.getPeriod(); 83 Arrays.sort(rv); 81 long [] rv = new long[_rates.length]; 82 for (int i = 0; i < _rates.length; i++) 83 rv[i] = _rates[i].getPeriod(); 84 84 return rv; 85 85 } 86 86 87 87 public double getLifetimeAverageValue() { 88 if (_rates.isEmpty()) return 0; 89 return _rates.values().iterator().next().getLifetimeAverageValue(); 88 return _rates[0].getLifetimeAverageValue(); 90 89 } 91 90 public long getLifetimeEventCount() { 92 if (_rates.isEmpty()) return 0; 93 return _rates.values().iterator().next().getLifetimeEventCount(); 91 return _rates[0].getLifetimeEventCount(); 94 92 } 95 93 … … 101 99 */ 102 100 public Rate getRate(long period) { 103 //if (_rates.containsKey(period)){ 104 return _rates.get(Long.valueOf(period)); 105 //} else { 106 // Rate rate = new Rate(period); 107 // rate.setRateStat(this); 108 // _rates.put(period, rate); 109 // return rate; 110 //} 101 for (Rate r : _rates) { 102 if (r.getPeriod() == period) 103 return r; 104 } 105 106 return null; 107 } 108 109 /** 110 * Adds a new rate with the requested period, provided that 111 * a rate with that period does not already exist. 112 * @param period ms 113 * @since 0.8.8 114 */ 115 @Deprecated 116 public void addRate(long period) { 117 throw new UnsupportedOperationException(); 111 118 } 112 119 113 120 /** 114 * Adds a new rate with the requested period, provided that 115 * a rate with that period does not already exist. 121 * If a rate with the provided period exists, remove it. 116 122 * @param period ms 117 123 * @since 0.8.8 118 124 */ 119 public void addRate(long period) { 120 if (!_rates.containsKey(period)){ 121 Rate rate = new Rate(period); 122 rate.setRateStat(this); 123 _rates.put(period, rate); 124 } 125 } 126 127 /** 128 * If a rate with the provided period exists, remove it. 129 * @param period ms 130 * @since 0.8.8 131 */ 125 @Deprecated 132 126 public void removeRate(long period) { 133 _rates.remove(period);127 throw new UnsupportedOperationException(); 134 128 } 135 129 … … 141 135 */ 142 136 public boolean containsRate(long period) { 143 return _rates.containsKey(period);137 return getRate(period) != null; 144 138 } 145 139 … … 156 150 buf.append(getGroupName()).append('.').append(getName()).append(": ").append(getDescription()).append('\n'); 157 151 long periods[] = getPeriods(); 158 Arrays.sort(periods);152 sort(periods); 159 153 for (int i = 0; i < periods.length; i++) { 160 154 buf.append('\t').append(periods[i]).append(':'); … … 169 163 public boolean equals(Object obj) { 170 164 if ((obj == null)  !(obj instanceof RateStat)) return false; 165 if (obj == this) 166 return true; 171 167 RateStat rs = (RateStat) obj; 172 168 if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription()) 173 169 && DataHelper.eq(getName(), rs.getName())) { 174 return this._rates.equals(rs._rates);170 return deepEquals(this._rates, rs._rates); 175 171 } 176 172 … … 187 183 out.write(buf.toString().getBytes()); 188 184 buf.setLength(0); 189 for (Rate r: _rates .values()){185 for (Rate r: _rates){ 190 186 buf.append("#######").append(NL); 191 187 buf.append("# Period : ").append(DataHelper.formatDuration(r.getPeriod())).append(" for rate ") … … 214 210 */ 215 211 public void load(Properties props, String prefix, boolean treatAsCurrent) throws IllegalArgumentException { 216 for (Rate r : _rates .values()) {212 for (Rate r : _rates) { 217 213 long period = r.getPeriod(); 218 214 String curPrefix = prefix + "." + DataHelper.formatDuration(period); 219 try { 220 r.load(props, curPrefix, treatAsCurrent); 221 } catch (IllegalArgumentException iae) { 222 Rate rate = new Rate(period); 223 rate.setRateStat(this); 224 _rates.put(rate.getPeriod(), rate); 225 Log log = I2PAppContext.getGlobalContext().logManager().getLog(RateStat.class); 226 if (log.shouldLog(Log.WARN)) 227 log.warn("Rate for " + prefix + " is corrupt, reinitializing that period"); 228 } 215 r.load(props, curPrefix, treatAsCurrent); 229 216 } 230 217 }
Note: See TracChangeset
for help on using the changeset viewer.