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); }
@Override public boolean equals(Object obj) { if (obj == null) return false; if (this == obj) return true; if (getClass() != obj.getClass()) return false; SparseHistogram other = (SparseHistogram) obj; return ObjectUtil.typedEquals(getHistogramDefinition(), other.getHistogramDefinition()) && ListUtil.compareCollections(getBins(), other.getBins()) ; }
public GaugeMetricValue(TimeRange timeRange) { this.range = timeRange; this.min = NaN; this.max = NaN; this.n = 0; this.sum = 0.0; this.sumOfSquares = 0.0; this.histo = new SparseHistogram(InsightGaugeHistogramDefinition.getInstance(), new ArrayList<IndexedBin>()); }
SparseHistogram xlatedSparse=getSparseHistogram(def); List<IndexedBin> populatedBins=xlatedSparse.getBins(); if (ListUtil.size(populatedBins) <= 0) { return new Histogram();
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()); } } }
if (def.equals(getHistogramDefinition())) { return getBinCountsMap();
public double getValueAtPercentile(GaugeMetricValue value, int p) { SparseHistogram histo = value.getHistogram(); int binNo = histo.getBinAfterPercentile(p); if (binNo == -1) { return Double.NaN; } return INSIGHT_HISTO.getBinDefinition(binNo).getStart(); }
double total = getTotalCount(); double cutoff = percentile / 100.0d; long runningCount = 0L;
public Histogram build() { SparseHistogram sparseMetric=metricValue.getHistogram(); Histogram metricHistogram=sparseMetric.getFullHistogram(definition); List<Bin> rawBins = growTo(metricHistogram.getBins(), end, binsCount); List<Bin> histBins = new ArrayList<Bin>(); int binCount = this.binsCount; // force start and end to bin boundaries double startHistory = definition.getBinDefinition(definition.getBinForValue(this.start)).getStart(); double endHistory = definition.getBinDefinition(definition.getBinForValue(this.end)).getEnd(); if (lowOutliers) { binCount--; histBins.add(new Bin(collapse(collect(rawBins, Double.NEGATIVE_INFINITY, startHistory)).getCount(), Double.NEGATIVE_INFINITY, startHistory)); } if (highOutliers) { binCount--; histBins.add(new Bin(collapse(collect(rawBins, endHistory, Double.POSITIVE_INFINITY)).getCount(), endHistory, Double.POSITIVE_INFINITY)); } histBins.addAll(toSize(collect(rawBins, startHistory, endHistory), binCount)); Collections.sort(histBins); return new Histogram(histBins); }
/** * 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); } }
List<IndexedBin> bins = histo.getBins(); long[] binCountLeft = new long[bins.size()]; for (int i=0; i<bins.size(); i++) {
@Override public int hashCode() { return ObjectUtil.hashCode(getHistogramDefinition()) + ListUtil.size(getBins()) // we do not use the hash since list order may vary ; }
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); }
/** * 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()); } }
@Override public void serialize(SparseHistogram value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeStartObject(); jgen.writeObjectField("bins", value.getBins()); jgen.writeObjectField("histogramDefinition", value.getHistogramDefinition()); jgen.writeEndObject(); }
@Override public SparseHistogram deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { HistogramDefinition histo=null; Collection<IndexedBin> bins=null; while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldName = jp.getCurrentName(); jp.nextToken(); if ("bins".equals(fieldName)) { bins = readObjectsList(jp, fieldName, IndexedBin.class, bins); } else if ("histogramDefinition".equals(fieldName)) { if (histo != null) { throw new StreamCorruptedException("Multiple histogram definitions"); } histo = jp.readValueAs(HistogramDefinition.class); } else { throw new StreamCorruptedException("Unknown field: " + fieldName); } } if (histo == null) { histo = InsightGaugeHistogramDefinition.getInstance(); } if (bins == null) { return new SparseHistogram(histo); } List<IndexedBin> binsList=new ArrayList<IndexedBin>(bins); Collections.sort(binsList); return new SparseHistogram(histo, binsList); }
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); }