/** * Returns a sparse histogram of all the data accumulated so far. */ public SparseHistogram getSparseHistogram() { List<IndexedBin> res = new ArrayList<IndexedBin>(binData.size()); for (Map.Entry<Integer, MutableLong> ent : binData.entrySet()) { res.add(new IndexedBin(ent.getKey().intValue(), ent.getValue().get())); } sort(res); return new SparseHistogram(def, res); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; IndexedBin other = (IndexedBin) obj; if (getBinNumber() != other.getBinNumber()) return false; if (getCount() != other.getCount()) return false; return true; }
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { long version = in.readLong(); if (version != serialVersionUID) { throw new StreamCorruptedException("Mismatched serialVersionUID - expected" + serialVersionUID + ", actual=" + version); } int size = in.readInt(), maxNumber=(-1); bins = (size > 0) ? new ArrayList<IndexedBin>(size) : null; boolean readHistogramDefinition=false; for (size-- ; size >= 0; size--) { IndexedBin bin=new IndexedBin(in.readInt(), in.readLong()); int binNumber=bin.getBinNumber(); if (binNumber > maxNumber) { maxNumber = binNumber; } if ((size == 0) && (maxNumber == binNumber) && (bin.getCount() == withHistogramDefinitionUID)) { readHistogramDefinition = true; } else { bins.add(bin); } } bins = (bins == null) ? Collections.<IndexedBin>emptyList() : Collections.unmodifiableList(bins); if (readHistogramDefinition) { histogramDefinition = (HistogramDefinition) in.readObject(); } else { histogramDefinition = InsightGaugeHistogramDefinition.getInstance(); } }
public long getTotalCount() { long total = 0L; for (IndexedBin bin : bins) { total += bin.getCount(); } return total; }
public int compareTo(IndexedBin o) { return getBinNumber() - o.getBinNumber(); } }
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + getBinNumber(); result = prime * result + MathUtil.hashValue(getCount()); return result; }
public void fromData(DataInput dataInput) throws IOException { super.fromData(serialVersionUID, dataInput); int numSlots = dataInput.readInt(); GaugeMetricValue[] slots = new GaugeMetricValue[numSlots]; int usedSlots = dataInput.readInt(); for (int i=0; i<usedSlots; i++) { int slotIdx = dataInput.readInt(); double min = dataInput.readDouble(); double max = dataInput.readDouble(); long n = dataInput.readLong(); double sum = dataInput.readDouble(); double sumOfSquares = dataInput.readDouble(); long rangeStart = dataInput.readLong(); long rangeEnd = rangeStart + getEntryGranularity().getNanos(); int numBins = dataInput.readInt(); List<IndexedBin> bins = new ArrayList<IndexedBin>(numBins); for (int b=0; b<numBins; b++) { bins.add(new IndexedBin(dataInput.readInt(), dataInput.readLong())); } slots[slotIdx] = new GaugeMetricValue(TimeRange.nanoTimeRange(rangeStart, rangeEnd), min, max, n, sum, sumOfSquares, new SparseHistogram(InsightGaugeHistogramDefinition.getInstance(), bins)); } this.slots = Arrays.asList(slots); }
public Map<Integer,MutableDouble> getBinCountsMap () { if (bins.isEmpty()) { return Collections.emptyMap(); } Map<Integer,MutableDouble> binCountsMap=new TreeMap<Integer,MutableDouble>(); for (IndexedBin indexedBin : bins) { int binIndex=indexedBin.getBinNumber(); double binCount=indexedBin.getCount(); MutableDouble prevCount=binCountsMap.put(Integer.valueOf(binIndex), new MutableDouble(binCount)); if (prevCount != null) { throw new IllegalStateException("Multiple counts for bin=" + binIndex + " current=" + binCount + "/prev=" + prevCount); } } return binCountsMap; }
public SparseHistogram getSparseHistogram (HistogramDefinition def) { if (def == null) { throw new IllegalArgumentException("No target definition"); } if (def.equals(getHistogramDefinition())) { return this; } Map<Integer,MutableDouble> binCountsMap=translate(def); if (MapUtil.size(binCountsMap) <= 0) { return new SparseHistogram(def, null); } List<IndexedBin> resBins=new ArrayList<IndexedBin>(binCountsMap.size()); for (Map.Entry<Integer,MutableDouble> binEntry : binCountsMap.entrySet()) { Integer binNumber=binEntry.getKey(); MutableDouble binValue=binEntry.getValue(); long binCount=Math.round(binValue.get()); if (binCount == 0L) { continue; } resBins.add(new IndexedBin(binNumber.intValue(), binCount)); } Collections.sort(resBins); return new SparseHistogram(def, resBins); }
double currentPercentile = runningCount / total; if (currentPercentile >= cutoff) { return bin.getBinNumber(); runningCount += bin.getCount(); lastBinIndex = bin.getBinNumber();
public static UpdateMetricDistributionCommand getUpdateGauge() { ResourceMetricKey key = ResourceMetricKey.valueOf(getServerResourceCommand("butter", "butter-label").getResource().getKey(), Metrics.ENDPOINT_RESPONSE_TIME); IndexedBin bin1 = new IndexedBin(1, 4); IndexedBin bin2 = new IndexedBin(2, 6); SparseHistogram histo = new SparseHistogram(InsightGaugeHistogramDefinition.getInstance(), Arrays.asList(bin1, bin2)); GaugeMetricValue gauge = new GaugeMetricValue(TimeRange.range(0, 56), 34, 80, 40, 400, 600,histo); return new UpdateMetricDistributionCommand(key, gauge); }
public void writeExternal(ObjectOutput out) throws IOException { if (histogramDefinition == null) { throw new StreamCorruptedException("No current histogram definition"); } out.writeLong(serialVersionUID); int numBins=bins.size(); out.writeInt(numBins+1); int maxNumber=(-1); for (IndexedBin bin: bins) { int binNumber=bin.getBinNumber(); out.writeInt(binNumber); out.writeLong(bin.getCount()); if (binNumber > maxNumber) { maxNumber = binNumber; } } /* * We need to preserve backward compatibility, so we add a "fake" extra bin * to signal the existence of the histogram definition */ out.writeInt(maxNumber + 1); out.writeLong(withHistogramDefinitionUID); out.writeObject(histogramDefinition); }
/** * Add all data from another histogram into this accumulator. The passed * histogram must be created against the same {@link HistogramDefinition} as * any other histograms passed into this accumulator. */ public void add(SparseHistogram histo) { for (IndexedBin addBin : histo.getBins()) { MutableLong bin = getOrCreateBin(addBin.getBinNumber()); bin.increment(addBin.getCount()); } }
public void toData(DataOutput dataOutput) throws IOException { super.toData(serialVersionUID, dataOutput); dataOutput.writeInt(slots.size()); int usedSlots = 0; for (GaugeMetricValue v : slots) { if (v != null) usedSlots++; } dataOutput.writeInt(usedSlots); int idx = -1; for (GaugeMetricValue v : slots) { idx++; if (v == null) { continue; } dataOutput.writeInt(idx); dataOutput.writeDouble(v.getMin()); dataOutput.writeDouble(v.getMax()); dataOutput.writeLong(v.getN()); dataOutput.writeDouble(v.getSum()); dataOutput.writeDouble(v.getSumOfSquares()); dataOutput.writeLong(v.getRange().getStart()); SparseHistogram histo = v.getHistogram(); dataOutput.writeInt(histo.getBins().size()); for (IndexedBin bin : histo.getBins()) { dataOutput.writeInt(bin.getBinNumber()); dataOutput.writeLong(bin.getCount()); } } }
long[] binCountLeft = new long[bins.size()]; for (int i=0; i<bins.size(); i++) { binCountLeft[i] = bins.get(i).getCount(); target.addHistogramEntry(bins.get(b).getBinNumber(), binAllocated);
int binIndex=indexedBin.getBinNumber(); BinDefinition binDef=histogramDefinition.getBinDefinition(binIndex); double binStart=binDef.getStart(), binEnd=binDef.getEnd(); double binCount=indexedBin.getCount(), binSize=binEnd - binStart;
Map<Integer,Bin> binsMap=new TreeMap<Integer, Bin>(); for (IndexedBin indexBin : populatedBins) { int mappedIndex=indexBin.getBinNumber(); BinDefinition mappedBin=def.getBinDefinition(mappedIndex); long mappedValue=indexBin.getCount(); if (mappedValue == 0L) { continue;