-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from chris-peterson/timers-api
Introduce API for timers
- Loading branch information
Showing
6 changed files
with
173 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Spiffy.Monitoring | ||
{ | ||
public class TimerCollection | ||
{ | ||
readonly ConcurrentDictionary<string, AutoTimer> _timers = new ConcurrentDictionary<string, AutoTimer>(); | ||
|
||
public ITimedContext TimeOnce(string key) | ||
{ | ||
var timer = GetTimer(key); | ||
timer.StartOver(); | ||
return timer; | ||
} | ||
|
||
public ITimedContext Accumulate(string key) | ||
{ | ||
var timer = GetTimer(key); | ||
timer.Resume(); | ||
return timer; | ||
} | ||
internal Dictionary<string, AutoTimer> ShallowClone() => new Dictionary<string, AutoTimer>(_timers); | ||
|
||
AutoTimer GetTimer(string key) | ||
{ | ||
return _timers.GetOrAdd(key, _ => new AutoTimer()); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Spiffy.Monitoring; | ||
using Xunit; | ||
|
||
namespace UnitTests | ||
{ | ||
public class ConcurrencyTests | ||
{ | ||
[Fact] | ||
public void TestTimers() | ||
{ | ||
var eventContext = new EventContext(); | ||
var tasks = new List<Task>(); | ||
const int numTasks = 100; | ||
for (int i = 0; i < numTasks; i++) | ||
{ | ||
var task = new Task(StaggeredMeasurements, new object [] {i, eventContext}); | ||
task.Start(); | ||
tasks.Add(task); | ||
} | ||
|
||
Task.WaitAll(tasks.ToArray()); | ||
|
||
LogEvent logEvent = null; | ||
Configuration.Initialize(c => c.Providers.Add(GetType().Name, le => logEvent = le )); | ||
eventContext.Dispose(); | ||
|
||
Assert.InRange(int.Parse(logEvent.Properties["Count_accum"]), numTasks*.65, numTasks*.99); | ||
Assert.DoesNotContain("Count_once", logEvent.Properties); | ||
Assert.True(double.Parse(logEvent.Properties["TimeElapsed_accum"]) > double.Parse(logEvent.Properties["TimeElapsed_once"])); | ||
} | ||
|
||
static void StaggeredMeasurements(object state) | ||
{ | ||
var param = (object[]) state; | ||
var delay = (int) param[0]; | ||
var eventContext = (EventContext) param[1]; | ||
using (eventContext.Timers.Accumulate("accum")) | ||
using (eventContext.Timers.TimeOnce("once")) | ||
{ | ||
Thread.Sleep(delay); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.Threading; | ||
using Spiffy.Monitoring; | ||
using Kekiri.Xunit; | ||
using Xunit; | ||
|
||
namespace UnitTests | ||
{ | ||
public class TimerCollectionTests : Scenarios | ||
{ | ||
[Scenario] | ||
public void TimeOnceScenario() | ||
{ | ||
Given(A_code_block_timed_once); | ||
When(Event_is_logged); | ||
Then(The_time_elapsed_field_should_be_near, 10) | ||
.And(There_should_be_no_count_field); | ||
} | ||
|
||
[Scenario] | ||
public void AccumulateScenario() | ||
{ | ||
Given(A_code_block_timed_multiple_times); | ||
When(Event_is_logged); | ||
Then(The_time_elapsed_field_should_be_near, 20) | ||
.And(There_should_be_a_count_field); | ||
} | ||
|
||
void A_code_block_timed_once() | ||
{ | ||
using (EventContext.Timers.TimeOnce(TimerKey)) | ||
{ | ||
Thread.Sleep(10); | ||
} | ||
} | ||
|
||
void A_code_block_timed_multiple_times() | ||
{ | ||
for (int i = 0; i < 2; i++) | ||
{ | ||
using (EventContext.Timers.Accumulate(TimerKey)) | ||
{ | ||
Thread.Sleep(10); | ||
} | ||
} | ||
} | ||
|
||
void Event_is_logged() | ||
{ | ||
Configuration.Initialize(c => c.Providers.Add(GetType().Name, logEvent => LoggedEvent = logEvent)); | ||
EventContext.Dispose(); | ||
} | ||
|
||
void The_time_elapsed_field_should_be_near(int target) | ||
{ | ||
Assert.InRange(double.Parse(LoggedEvent.Properties[$"TimeElapsed_{TimerKey}"]), target-5, target+5); | ||
} | ||
|
||
void There_should_be_no_count_field() | ||
{ | ||
Assert.DoesNotContain($"Count_{TimerKey}", LoggedEvent.Properties); | ||
} | ||
|
||
void There_should_be_a_count_field() | ||
{ | ||
var keyName = $"Count_{TimerKey}"; | ||
Assert.Contains(keyName, LoggedEvent.Properties); | ||
Assert.Equal(2, int.Parse(LoggedEvent.Properties[keyName])); | ||
} | ||
|
||
EventContext EventContext { get; } = new EventContext(); | ||
LogEvent LoggedEvent { get; set; } | ||
const string TimerKey = "abc"; | ||
} | ||
} |