Created
November 12, 2014 16:30
-
-
Save Eibwen/f587373a926ec7689654 to your computer and use it in GitHub Desktop.
DebugTimerRepository - Ability to sum time taken on tasks
This file contains hidden or 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
/// <summary> | |
/// How to use this: | |
/// 1. Get the reference | |
/// a. Inject IDebugTimerRepository | |
/// b. call DebugTimerRepository.Instance | |
/// 2. Log values | |
/// a. Call _debugTimerRepository.AddValue(string label, long value) | |
/// Probably using value == Stopwatch.TotalMiliseconds | |
/// | |
/// Option 2: | |
/// Use DebugTimerSection | |
/// 1. In a using() block | |
/// 2. Create a new instance, and .Dispose() when want it logged | |
/// </summary> | |
public interface IDebugTimerRepository : IRepository<DebugCounter, string> | |
{ | |
void AddValue(string label, long miliseconds); | |
} | |
/// <summary> | |
/// How to use this: | |
/// 1. Get the reference | |
/// a. Inject IDebugTimerRepository | |
/// b. call DebugTimerRepository.Instance | |
/// 2. Log values | |
/// a. Call _debugTimerRepository.AddValue(string label, long value) | |
/// Probably using value == Stopwatch.TotalMiliseconds | |
/// | |
/// Option 2: | |
/// Use DebugTimerSection | |
/// 1. In a using() block | |
/// 2. Create a new instance, and .Dispose() when want it logged | |
/// </summary> | |
public class DebugTimerRepository : InMemoryRepository<DebugCounter, string>, IDebugTimerRepository | |
{ | |
private static readonly Lazy<IDebugTimerRepository> LazyInstance = new Lazy<IDebugTimerRepository>(() => new DebugTimerRepository()); | |
public static IDebugTimerRepository Instance | |
{ | |
get { return LazyInstance.Value; } | |
} | |
private DebugTimerRepository() | |
{ | |
} | |
private static bool IsNotDevEnvironment | |
{ | |
get { return !ModuleConfig.GetSettings().IsDevelopmentEnvironment; } | |
} | |
public void AddValue(string label, long value) | |
{ | |
if (IsNotDevEnvironment) return; | |
DebugCounter item; | |
if (base.Contains(label)) | |
{ | |
item = base.Get(label); | |
} | |
else | |
{ | |
item = new DebugCounter(label); | |
base.Add(item); | |
} | |
item.AddValue(value); | |
} | |
} | |
public class DebugTimerSection : IDisposable | |
{ | |
private readonly string _label; | |
private readonly Stopwatch _stopwatch; | |
public DebugTimerSection(string label) | |
{ | |
_label = label; | |
_stopwatch = new Stopwatch(); | |
_stopwatch.Start(); | |
} | |
public void Dispose() | |
{ | |
_stopwatch.Stop(); | |
DebugTimerRepository.Instance.AddValue(_label, _stopwatch.ElapsedMilliseconds); | |
} | |
} | |
public class DebugCounter : InMemoryEntity<string> | |
{ | |
private readonly DateTime _dateCreated = DateTime.Now; | |
public DebugCounter() | |
{ | |
Id = "null"; | |
} | |
public DebugCounter(string label) | |
{ | |
Id = label; | |
} | |
public DateTime DateCreated | |
{ | |
get { return _dateCreated; } | |
} | |
public long TotalValueSummation { get; set; } | |
public long TotalCount { get; set; } | |
public void AddValue(long value) | |
{ | |
TotalValueSummation += value; | |
TotalCount++; | |
} | |
} |
This file contains hidden or 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
public class TestDebugTimerLogsController : ApiControllerBase | |
{ | |
private readonly IDebugTimerRepository _repository; | |
public TestDebugTimerLogsController( | |
IApiContext context, | |
IApiResponseFactoryFactory responseFactoryFactory, | |
IApiModelFactory modelFactory, | |
IDebugTimerRepository debugTimerRepository) | |
: base(context, responseFactoryFactory, modelFactory) | |
{ | |
_repository = debugTimerRepository; | |
} | |
public ICollectionResult<DebugCounter> GetAll() | |
{ | |
//return _repository.GetAll().Select(x => x.Id).ToArray(); | |
return ResponseFactory.CreateCollection<DebugCounter>(_repository.GetAll()); | |
} | |
public ICollectionResult<DebugCounter> Get(string label) | |
{ | |
var stuffs = _repository.GetAll().Where(di => di.Id == label).ToList(); | |
return ResponseFactory.CreateCollection<DebugCounter>(stuffs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment