protected RollingDistributionStream(IHystrixEventStream <Event> stream, int numBuckets, int bucketSizeInMs, Func <LongHistogram, Event, LongHistogram> addValuesToBucket) { var emptyDistributionsToStart = new List <LongHistogram>(); for (var i = 0; i < numBuckets; i++) { emptyDistributionsToStart.Add(CachedValuesHistogram.GetNewHistogram()); } Func <IObservable <Event>, IObservable <LongHistogram> > reduceBucketToSingleDistribution = (bucket) => { var result = bucket.Aggregate(CachedValuesHistogram.GetNewHistogram(), (arg1, arg2) => addValuesToBucket(arg1, arg2)).Select(n => n); return(result); }; _rollingDistributionStream = stream .Observe() .Window(TimeSpan.FromMilliseconds(bucketSizeInMs), NewThreadScheduler.Default) // stream of unaggregated buckets .SelectMany((d) => reduceBucketToSingleDistribution(d)) // stream of aggregated Histograms .StartWith(emptyDistributionsToStart) // stream of aggregated Histograms that starts with n empty .Window(numBuckets, 1) // windowed stream: each OnNext is a stream of n Histograms .SelectMany((w) => ReduceWindowToSingleDistribution(w)) // reduced stream: each OnNext is a single Histogram .Map((h) => CacheHistogramValues(h)) // convert to CachedValueHistogram (commonly-accessed values are cached) .Publish().RefCount(); }
public int GetLatestPercentile(double percentile) { CachedValuesHistogram latest = Latest; if (latest != null) { return(latest.GetValueAtPercentile(percentile)); } else { return(0); } }