Performance.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using WingServerCommon.Log;
  2. namespace WingServerCommon.Utilities
  3. {
  4. public class Performance : IDisposable
  5. {
  6. private readonly int _performanceThreshold;
  7. private readonly string _message;
  8. private readonly int _startTime;
  9. public Performance(string message)
  10. {
  11. _performanceThreshold = 100;
  12. _startTime = Environment.TickCount;
  13. _message = message;
  14. }
  15. public Performance(string format, params object[] args)
  16. {
  17. _performanceThreshold = 100;
  18. _startTime = Environment.TickCount;
  19. _message = string.Format(format, args);
  20. }
  21. public Performance(int performanceThreshold, string format, params object[] args)
  22. {
  23. _performanceThreshold = performanceThreshold;
  24. _startTime = Environment.TickCount;
  25. _message = string.Format(format, args);
  26. }
  27. public void Dispose()
  28. {
  29. var endTime = Environment.TickCount;
  30. var intervals = endTime - _startTime;
  31. if (intervals > _performanceThreshold)
  32. {
  33. Logger.WriteLineWarn($"Perf: {_message} end, spend {intervals}ms");
  34. }
  35. }
  36. }
  37. }