|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Microsoft.PowerShell.EditorServices.Utility |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Simple timer to be used with `using` to time executions. |
| 10 | + /// </summary> |
| 11 | + /// <example> |
| 12 | + /// An example showing how ExecutionTimer is intended to be used |
| 13 | + /// <code> |
| 14 | + /// using (ExecutionTimer.Start(logger, "Execution of MyMethod completed.")) |
| 15 | + /// { |
| 16 | + /// MyMethod(various, arguments); |
| 17 | + /// } |
| 18 | + /// </code> |
| 19 | + /// This will print a message like "Execution of MyMethod completed. [50ms]" to the logs. |
| 20 | + /// </example> |
| 21 | + public struct ExecutionTimer : IDisposable |
| 22 | + { |
| 23 | + [ThreadStatic] |
| 24 | + private static Stopwatch t_stopwatch; |
| 25 | + |
| 26 | + private readonly ILogger _logger; |
| 27 | + |
| 28 | + private readonly string _message; |
| 29 | + |
| 30 | + private readonly string _callerMemberName; |
| 31 | + |
| 32 | + private readonly string _callerFilePath; |
| 33 | + |
| 34 | + private readonly int _callerLineNumber; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Create a new execution timer and start it. |
| 38 | + /// </summary> |
| 39 | + /// <param name="logger">The logger to log the execution timer message in.</param> |
| 40 | + /// <param name="message">The message to prefix the execution time with.</param> |
| 41 | + /// <param name="callerMemberName">The name of the calling method or property.</param> |
| 42 | + /// <param name="callerFilePath">The path to the source file of the caller.</param> |
| 43 | + /// <param name="callerLineNumber">The line where the timer is called.</param> |
| 44 | + /// <returns>A new, started execution timer.</returns> |
| 45 | + public static ExecutionTimer Start( |
| 46 | + ILogger logger, |
| 47 | + string message, |
| 48 | + [CallerMemberName] string callerMemberName = null, |
| 49 | + [CallerFilePath] string callerFilePath = null, |
| 50 | + [CallerLineNumber] int callerLineNumber = -1) |
| 51 | + { |
| 52 | + var timer = new ExecutionTimer(logger, message, callerMemberName, callerFilePath, callerLineNumber); |
| 53 | + t_stopwatch.Start(); |
| 54 | + return timer; |
| 55 | + } |
| 56 | + |
| 57 | + internal ExecutionTimer( |
| 58 | + ILogger logger, |
| 59 | + string message, |
| 60 | + string callerMemberName, |
| 61 | + string callerFilePath, |
| 62 | + int callerLineNumber) |
| 63 | + { |
| 64 | + _logger = logger; |
| 65 | + _message = message; |
| 66 | + _callerMemberName = callerMemberName; |
| 67 | + _callerFilePath = callerFilePath; |
| 68 | + _callerLineNumber = callerLineNumber; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Dispose of the execution timer by stopping the stopwatch and then printing |
| 73 | + /// the elapsed time in the logs. |
| 74 | + /// </summary> |
| 75 | + public void Dispose() |
| 76 | + { |
| 77 | + t_stopwatch.Stop(); |
| 78 | + |
| 79 | + string logMessage = new StringBuilder() |
| 80 | + .Append(_message) |
| 81 | + .Append(" [") |
| 82 | + .Append(t_stopwatch.ElapsedMilliseconds) |
| 83 | + .Append("ms]") |
| 84 | + .ToString(); |
| 85 | + |
| 86 | + t_stopwatch.Reset(); |
| 87 | + |
| 88 | + _logger.Write( |
| 89 | + LogLevel.Verbose, |
| 90 | + logMessage, |
| 91 | + callerName: _callerMemberName, |
| 92 | + callerSourceFile: _callerFilePath, |
| 93 | + callerLineNumber: _callerLineNumber); |
| 94 | + } |
| 95 | + |
| 96 | + private Stopwatch Stopwatch => t_stopwatch ?? (t_stopwatch = new Stopwatch()); |
| 97 | + } |
| 98 | +} |
0 commit comments