Skip to content

Commit

Permalink
Implemented TimerMetric
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcrenna committed Apr 29, 2011
1 parent 25ee298 commit c27d0a7
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 39 deletions.
7 changes: 2 additions & 5 deletions metrics/Core/IMetered.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using metrics.Support;

namespace metrics.Core
namespace metrics.Core
{
public interface IMetered
{
Expand All @@ -15,7 +12,7 @@ public interface IMetered
/// Returns the type of events the meter is measuring
/// </summary>
/// <returns></returns>
String EventType { get; }
string EventType { get; }

/// <summary>
/// Returns the number of events which have been marked
Expand Down
12 changes: 11 additions & 1 deletion metrics/Core/MeterMetric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace metrics.Core
/// <summary>
/// A meter metric which measures mean throughput and one-, five-, and fifteen-minute exponentially-weighted moving average throughputs
/// </summary>
/// <see href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average" />
/// <see href="http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average">EMA</see>
public class MeterMetric : IMetered
{
public TimeUnit RateUnit
Expand Down Expand Up @@ -43,5 +43,15 @@ public double OneMinuteRate()
{
throw new NotImplementedException();
}

public static MeterMetric New(string calls, TimeUnit rateUnit)
{
throw new NotImplementedException();
}

public void Mark()
{
throw new NotImplementedException();
}
}
}
213 changes: 194 additions & 19 deletions metrics/Core/TimerMetric.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,235 @@
using System;
using metrics.Support;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json;

namespace metrics.Core
{
/// <summary>
/// A timer metric which aggregates timing durations and provides duration
/// statistics, plus throughput statistics via <see cref="MeterMetric" />.
/// </summary>
public class TimerMetric : IMetric, IMetered
{
public TimeUnit DurationUnit { get; set; }
private readonly TimeUnit _durationUnit;
private readonly TimeUnit _rateUnit;
private readonly MeterMetric _meter;
private readonly HistogramMetric _histogram;

public IMetric Copy
public TimerMetric(TimeUnit durationUnit, TimeUnit rateUnit)
: this(durationUnit, rateUnit, MeterMetric.New("calls", rateUnit), new HistogramMetric(HistogramMetric.SampleType.Biased), true /* clear */)
{

}

private TimerMetric(TimeUnit durationUnit, TimeUnit rateUnit, MeterMetric meter, HistogramMetric histogram, bool clear)
{
get { throw new NotImplementedException(); }
_durationUnit = durationUnit;
_rateUnit = rateUnit;
_meter = meter;
_histogram = histogram;
if(clear)
{
Clear();
}
}

/// <summary>
/// Returns the timer's duration scale unit
/// </summary>
public TimeUnit DurationUnit
{
get { return _durationUnit; }
}

/// <summary>
/// Returns the meter's rate unit
/// </summary>
/// <returns></returns>
public TimeUnit RateUnit
{
get { throw new NotImplementedException(); }
get { return _rateUnit; }
}

public string EventType
/// <summary>
/// Clears all recorded durations
/// </summary>
public void Clear()
{
get { throw new NotImplementedException(); }
_histogram.Clear();
}

public long Count
public void Update(long duration, TimeUnit unit)
{
Update(unit.ToNanos(duration));
}

/// <summary>
/// Times and records the duration of an event
/// </summary>
/// <typeparam name="T">The type of the value returned by the event</typeparam>
/// <param name="event">A function whose duration should be timed</param>
public T Time<T>(Func<T> @event)
{
get { throw new NotImplementedException(); }
var stopwatch = new Stopwatch();
try
{
stopwatch.Start();
return @event.Invoke();
}
finally
{
stopwatch.Stop();
Update(stopwatch.Elapsed.Ticks);
}
}

public double Min { get; set; }
public double Max { get; set; }
public double Mean { get; set; }
public double StdDev { get; set; }
/// <summary>
/// Returns the number of events which have been marked
/// </summary>
/// <returns></returns>
public long Count
{
get { return _histogram.Count; }
}

/// <summary>
/// Returns the fifteen-minute exponentially-weighted moving average rate at
/// which events have occured since the meter was created
/// <remarks>
/// This rate has the same exponential decay factor as the fifteen-minute load
/// average in the top Unix command.
/// </remarks>
/// </summary>
public double FifteenMinuteRate()
{
throw new NotImplementedException();
return _meter.FifteenMinuteRate();
}

/// <summary>
/// Returns the five-minute exponentially-weighted moving average rate at
/// which events have occured since the meter was created
/// <remarks>
/// This rate has the same exponential decay factor as the five-minute load
/// average in the top Unix command.
/// </remarks>
/// </summary>
public double FiveMinuteRate()
{
throw new NotImplementedException();
return _meter.FiveMinuteRate();
}

/// <summary>
/// Returns the mean rate at which events have occured since the meter was created
/// </summary>
public double MeanRate()
{
throw new NotImplementedException();
return _meter.MeanRate();
}

/// <summary>
/// Returns the one-minute exponentially-weighted moving average rate at
/// which events have occured since the meter was created
/// <remarks>
/// This rate has the same exponential decay factor as the one-minute load
/// average in the top Unix command.
/// </remarks>
/// </summary>
/// <returns></returns>
public double OneMinuteRate()
{
throw new NotImplementedException();
return _meter.OneMinuteRate();
}

/// <summary>
/// Returns the longest recorded duration
/// </summary>
public double Max
{
get { return ConvertFromNanos(_histogram.Max); }
}

/// <summary>
/// Returns the shortest recorded duration
/// </summary>
public double Min
{
get { return ConvertFromNanos(_histogram.Min); }
}

public double[] Percentiles(params double[] args)
/// <summary>
/// Returns the arithmetic mean of all recorded durations
/// </summary>
public double Mean
{
get { return ConvertFromNanos(_histogram.Mean); }
}

/// <summary>
/// Returns the standard deviation of all recorded durations
/// </summary>
public double StdDev
{
get { return ConvertFromNanos(_histogram.StdDev); }
}

/// <summary>
/// Returns an array of durations at the given percentiles
/// </summary>
public double[] Percentiles(params double[] percentiles)
{
var scores = _histogram.Percentiles(percentiles);
for (var i = 0; i < scores.Length; i++)
{
scores[i] = ConvertFromNanos(scores[i]);
}

return scores;
}

/// <summary>
/// Returns the type of events the meter is measuring
/// </summary>
/// <returns></returns>
public string EventType
{
get { return _meter.EventType; }
}

/// <summary>
/// Returns a list of all recorded durations in the timers's sample
/// </summary>
public ICollection<double> Values
{
get
{
return _histogram.Values.Select(value => ConvertFromNanos(value)).ToList();
}
}

private void Update(long duration)
{
if (duration < 0) return;
_histogram.Update(duration);
_meter.Mark();
}

private double ConvertFromNanos(double nanos)
{
return nanos / TimeUnit.Nanoseconds.Convert(1, _durationUnit);
}

[JsonIgnore]
public IMetric Copy
{
throw new NotImplementedException();
get
{
var copy = new TimerMetric(
_durationUnit, _rateUnit, _meter, _histogram, false /* clear */
);
return copy;
}
}
}
}
Loading

0 comments on commit c27d0a7

Please sign in to comment.