Initial commit
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2018 The Cartographer Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CartographerSharp.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// Histogram metric for tracking value distributions.
|
||||
/// </summary>
|
||||
public abstract class Histogram
|
||||
{
|
||||
/// <summary>
|
||||
/// Bucket boundaries for histogram.
|
||||
/// </summary>
|
||||
public class BucketBoundaries : List<double>
|
||||
{
|
||||
public BucketBoundaries() : base() { }
|
||||
public BucketBoundaries(IEnumerable<double> collection) : base(collection) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Histogram instance that does nothing. Safe for use in static initializers.
|
||||
/// </summary>
|
||||
public static Histogram Null()
|
||||
{
|
||||
return new NullHistogram();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates fixed-width bucket boundaries.
|
||||
/// </summary>
|
||||
public static BucketBoundaries FixedWidth(double width, int numFiniteBuckets)
|
||||
{
|
||||
var result = new BucketBoundaries();
|
||||
double boundary = 0;
|
||||
for (int i = 0; i < numFiniteBuckets; i++)
|
||||
{
|
||||
boundary += width;
|
||||
result.Add(boundary);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates scaled powers-of bucket boundaries.
|
||||
/// </summary>
|
||||
public static BucketBoundaries ScaledPowersOf(double baseValue, double scaleFactor, double maxValue)
|
||||
{
|
||||
if (baseValue <= 1)
|
||||
throw new ArgumentException("Base must be greater than 1", nameof(baseValue));
|
||||
if (scaleFactor <= 0)
|
||||
throw new ArgumentException("Scale factor must be greater than 0", nameof(scaleFactor));
|
||||
|
||||
var result = new BucketBoundaries();
|
||||
double boundary = scaleFactor;
|
||||
while (boundary < maxValue)
|
||||
{
|
||||
result.Add(boundary);
|
||||
boundary *= baseValue;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Observes a value in the histogram.
|
||||
/// </summary>
|
||||
public abstract void Observe(double value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Null implementation of histogram that does nothing.
|
||||
/// </summary>
|
||||
internal class NullHistogram : Histogram
|
||||
{
|
||||
public override void Observe(double value)
|
||||
{
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user