diff --git a/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs b/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs index e8b1a628..011f380e 100644 --- a/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs +++ b/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs @@ -116,52 +116,56 @@ public IReadOnlyCollection> GetMeasurements(string nam else return ImmutableList.CreateRange(((IEnumerable>)_byteMeasurements).Where(m => m.Name == name)); } - else if (typeof(T) == typeof(short)) + + if (typeof(T) == typeof(short)) { if (name == null) return ImmutableList.CreateRange((IEnumerable>)_shortMeasurements); else return ImmutableList.CreateRange(((IEnumerable>)_shortMeasurements).Where(m => m.Name == name)); } - else if (typeof(T) == typeof(int)) + + if (typeof(T) == typeof(int)) { if (name == null) return ImmutableList.CreateRange((IEnumerable>)_intMeasurements); else return ImmutableList.CreateRange(((IEnumerable>)_intMeasurements).Where(m => m.Name == name)); } - else if (typeof(T) == typeof(long)) + + if (typeof(T) == typeof(long)) { if (name == null) return ImmutableList.CreateRange((IEnumerable>)_longMeasurements); else return ImmutableList.CreateRange(((IEnumerable>)_longMeasurements).Where(m => m.Name == name)); } - else if (typeof(T) == typeof(float)) + + if (typeof(T) == typeof(float)) { if (name == null) return ImmutableList.CreateRange((IEnumerable>)_floatMeasurements); else return ImmutableList.CreateRange(((IEnumerable>)_floatMeasurements).Where(m => m.Name == name)); } - else if (typeof(T) == typeof(double)) + + if (typeof(T) == typeof(double)) { if (name == null) return ImmutableList.CreateRange((IEnumerable>)_doubleMeasurements); else return ImmutableList.CreateRange(((IEnumerable>)_doubleMeasurements).Where(m => m.Name == name)); } - else if (typeof(T) == typeof(decimal)) + + if (typeof(T) == typeof(decimal)) { if (name == null) return ImmutableList.CreateRange((IEnumerable>)_decimalMeasurements); else return ImmutableList.CreateRange(((IEnumerable>)_decimalMeasurements).Where(m => m.Name == name)); } - else - { - return ImmutableList.Create>(); - } + + return ImmutableList.Create>(); // byte, short, int, long, float, double, decimal } @@ -171,6 +175,16 @@ public int GetCount(string name) where T : struct return GetMeasurements().Count(m => m.Name == name); } + public double GetLast(string name) where T : struct + { + var measurement = GetMeasurements(name) + .Where(m => m.Name == name) + .OrderByDescending(m => m.Timestamp) + .FirstOrDefault(); + + return Convert.ToDouble(measurement.Value); + } + public double GetSum(string name) where T : struct { if (typeof(T) == typeof(byte)) @@ -178,40 +192,44 @@ public double GetSum(string name) where T : struct var measurements = GetMeasurements(name); return measurements.Sum(m => m.Value); } - else if (typeof(T) == typeof(short)) + + if (typeof(T) == typeof(short)) { var measurements = GetMeasurements(name); return measurements.Sum(m => m.Value); } - else if (typeof(T) == typeof(int)) + + if (typeof(T) == typeof(int)) { var measurements = GetMeasurements(name); return measurements.Sum(m => m.Value); } - else if (typeof(T) == typeof(long)) + + if (typeof(T) == typeof(long)) { var measurements = GetMeasurements(name); return measurements.Sum(m => m.Value); } - else if (typeof(T) == typeof(float)) + + if (typeof(T) == typeof(float)) { var measurements = GetMeasurements(name); return measurements.Sum(m => m.Value); } - else if (typeof(T) == typeof(double)) + + if (typeof(T) == typeof(double)) { var measurements = GetMeasurements(name); return measurements.Sum(m => m.Value); } - else if (typeof(T) == typeof(decimal)) + + if (typeof(T) == typeof(decimal)) { var measurements = GetMeasurements(name); return measurements.Sum(m => (double)m.Value); } - else - { - return 0; - } + + return 0; } public double GetAvg(string name) where T : struct @@ -221,40 +239,44 @@ public double GetAvg(string name) where T : struct var measurements = GetMeasurements(name); return measurements.Average(m => m.Value); } - else if (typeof(T) == typeof(short)) + + if (typeof(T) == typeof(short)) { var measurements = GetMeasurements(name); return measurements.Average(m => m.Value); } - else if (typeof(T) == typeof(int)) + + if (typeof(T) == typeof(int)) { var measurements = GetMeasurements(name); return measurements.Average(m => m.Value); } - else if (typeof(T) == typeof(long)) + + if (typeof(T) == typeof(long)) { var measurements = GetMeasurements(name); return measurements.Average(m => m.Value); } - else if (typeof(T) == typeof(float)) + + if (typeof(T) == typeof(float)) { var measurements = GetMeasurements(name); return measurements.Average(m => m.Value); } - else if (typeof(T) == typeof(double)) + + if (typeof(T) == typeof(double)) { var measurements = GetMeasurements(name); return measurements.Average(m => m.Value); } - else if (typeof(T) == typeof(decimal)) + + if (typeof(T) == typeof(decimal)) { var measurements = GetMeasurements(name); return measurements.Average(m => (double)m.Value); } - else - { - return 0; - } + + return 0; } public double GetMax(string name) where T : struct @@ -264,40 +286,44 @@ public double GetMax(string name) where T : struct var measurements = GetMeasurements(name); return measurements.Max(m => m.Value); } - else if (typeof(T) == typeof(short)) + + if (typeof(T) == typeof(short)) { var measurements = GetMeasurements(name); return measurements.Max(m => m.Value); } - else if (typeof(T) == typeof(int)) + + if (typeof(T) == typeof(int)) { var measurements = GetMeasurements(name); return measurements.Max(m => m.Value); } - else if (typeof(T) == typeof(long)) + + if (typeof(T) == typeof(long)) { var measurements = GetMeasurements(name); return measurements.Max(m => m.Value); } - else if (typeof(T) == typeof(float)) + + if (typeof(T) == typeof(float)) { var measurements = GetMeasurements(name); return measurements.Max(m => m.Value); } - else if (typeof(T) == typeof(double)) + + if (typeof(T) == typeof(double)) { var measurements = GetMeasurements(name); return measurements.Max(m => m.Value); } - else if (typeof(T) == typeof(decimal)) + + if (typeof(T) == typeof(decimal)) { var measurements = GetMeasurements(name); return measurements.Max(m => (double)m.Value); } - else - { - return 0; - } + + return 0; } public async Task WaitForCounterAsync(string statName, long count = 1, TimeSpan? timeout = null) where T : struct @@ -355,16 +381,17 @@ public void Dispose() [DebuggerDisplay("{Name}={Value}")] public struct RecordedMeasurement where T : struct { - public RecordedMeasurement(Instrument instrument, T value, ref ReadOnlySpan> tags, object state) + public RecordedMeasurement(Instrument instrument, T value, ref ReadOnlySpan> tags, + object state) { Instrument = instrument; Name = Instrument.Name; Value = value; - if (tags.Length > 0) - Tags = ImmutableDictionary.CreateRange(tags.ToArray()); - else - Tags = ImmutableDictionary.Empty; + Tags = tags.Length > 0 + ? ImmutableDictionary.CreateRange(tags.ToArray()) + : ImmutableDictionary.Empty; State = state; + Timestamp = DateTime.UtcNow; } public Instrument Instrument { get; } @@ -372,4 +399,5 @@ public RecordedMeasurement(Instrument instrument, T value, ref ReadOnlySpan Tags { get; } public object State { get; } + public DateTime Timestamp { get; } } diff --git a/src/Foundatio.TestHarness/Queue/QueueTestBase.cs b/src/Foundatio.TestHarness/Queue/QueueTestBase.cs index b88ad738..d0baf10c 100644 --- a/src/Foundatio.TestHarness/Queue/QueueTestBase.cs +++ b/src/Foundatio.TestHarness/Queue/QueueTestBase.cs @@ -98,9 +98,9 @@ await queue.EnqueueAsync(new SimpleWorkItem Assert.Equal(1, metricsCollector.GetSum("foundatio.simpleworkitem.dequeued")); Assert.Equal(1, metricsCollector.GetSum("foundatio.simpleworkitem.completed")); - Assert.Equal(0, metricsCollector.GetSum("foundatio.simpleworkitem.count")); - Assert.Equal(0, metricsCollector.GetSum("foundatio.simpleworkitem.working")); - Assert.Equal(0, metricsCollector.GetSum("foundatio.simpleworkitem.deadletter")); + Assert.Equal(0, metricsCollector.GetLast("foundatio.simpleworkitem.count")); + Assert.Equal(0, metricsCollector.GetLast("foundatio.simpleworkitem.working")); + Assert.Equal(0, metricsCollector.GetLast("foundatio.simpleworkitem.deadletter")); Assert.Equal(1, metricsCollector.GetSum("foundatio.simpleworkitem.myitem.enqueued")); Assert.Equal(1, metricsCollector.GetSum("foundatio.simpleworkitem.myitem.dequeued")); @@ -403,7 +403,7 @@ await queue.EnqueueAsync(new SimpleWorkItem Assert.Equal(0, metricsCollector.GetSum("foundatio.simpleworkitem.completed")); Assert.Equal(retryCount + 1, metricsCollector.GetSum("foundatio.simpleworkitem.abandoned")); - Assert.Equal(0, metricsCollector.GetSum("foundatio.simpleworkitem.count")); + Assert.Equal(0, metricsCollector.GetLast("foundatio.simpleworkitem.count")); } } finally