-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port weighted sample from JavaMetrics ( dropwizard/metrics#421 )
- Loading branch information
Showing
11 changed files
with
353 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using System.Collections.Generic; | ||
namespace Metrics.Core | ||
{ | ||
public struct Snapshot | ||
public interface Snapshot | ||
{ | ||
private readonly long[] values; | ||
|
||
public Snapshot(IEnumerable<long> values) | ||
{ | ||
this.values = values.OrderBy(v => v).ToArray(); | ||
} | ||
|
||
public int Size { get { return this.values.Length; } } | ||
|
||
public long Max { get { return this.values.LastOrDefault(); } } | ||
public long Min { get { return this.values.FirstOrDefault(); } } | ||
public double Mean { get { return Size == 0 ? 0.0 : this.values.Average(); } } | ||
|
||
public double StdDev | ||
{ | ||
get | ||
{ | ||
if (this.Size <= 1) | ||
{ | ||
return 0; | ||
} | ||
|
||
double avg = values.Average(); | ||
double sum = values.Sum(d => Math.Pow(d - avg, 2)); | ||
return Math.Sqrt((sum) / (values.Count() - 1)); | ||
} | ||
} | ||
|
||
public double Median { get { return GetValue(0.5d); } } | ||
public double Percentile75 { get { return GetValue(0.75d); } } | ||
public double Percentile95 { get { return GetValue(0.95d); } } | ||
public double Percentile98 { get { return GetValue(0.98d); } } | ||
public double Percentile99 { get { return GetValue(0.99d); } } | ||
public double Percentile999 { get { return GetValue(0.999d); } } | ||
|
||
public IEnumerable<long> Values { get { return this.values.AsEnumerable(); } } | ||
|
||
public double GetValue(double quantile) | ||
{ | ||
if (quantile < 0.0 || quantile > 1.0) | ||
{ | ||
throw new ArgumentException(string.Format("{0} is not in [0..1]", quantile)); | ||
} | ||
|
||
if (this.Size == 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
double pos = quantile * (values.Length + 1); | ||
|
||
if (pos < 1) | ||
{ | ||
return values[0]; | ||
} | ||
|
||
if (pos >= values.Length) | ||
{ | ||
return values[values.Length - 1]; | ||
} | ||
|
||
double lower = values[(int)pos - 1]; | ||
double upper = values[(int)pos]; | ||
return lower + (pos - Math.Floor(pos)) * (upper - lower); | ||
} | ||
double GetValue(double quantile); | ||
long Max { get; } | ||
double Mean { get; } | ||
double Median { get; } | ||
long Min { get; } | ||
double Percentile75 { get; } | ||
double Percentile95 { get; } | ||
double Percentile98 { get; } | ||
double Percentile99 { get; } | ||
double Percentile999 { get; } | ||
int Size { get; } | ||
double StdDev { get; } | ||
IEnumerable<long> Values { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.