I want to create a tool for my app performance measurement, something that I can put in sort of section and it will take a timestamp at the beginning and at the end, and then it will write to log the time delta.
However I don't want this functionality in my Production version (I want those measurements to be excluded from the compilation on Production) - I want them only on Debug mode...
I have looked at several AOT libraries, such as Postsharp , but they cost money and also most of them (including PostSharp) don't supports ARM architecture.
Any help will be appreciated.
Eventually I came with something like this, which is a singletone when you are not in debug and normal instantse when you are: Usage:
using(PerfMesureHandler.Get("Something to meassure")))
{
//Code to measure
}
Code:
public class PerfMesureHandler: IDisposable
{
private PerfMesureHandler(string topic, string methodName , int lineNumber )
{
#if DEBUG
_methodName = methodName;
_lineNumber = lineNumber;
_topic = topic;
_stopwatch = new Stopwatch();
_stopwatch.Start();
#endif
}
private string _topic;
private static PerfMesureHandler_singleton;
private Stopwatch _stopwatch;
private int _lineNumber;
private string _methodName;
public static PerfMesureHandler Start(string topic, [CallerMemberName] string methodName = "", [CallerLineNumber] int lineNumber = 0)
{
PerfMesureHandler= null;
#if DEBUG
result = new PerfMesureHandler(topic, methodName, lineNumber);
#else
if (_singleton == null)
{
_singleton = new PerfMesureHandler(topic, methodName, lineNumber);
}
result = _singleton;
#endif
return result;
}
public void Dispose()
{
#if DEBUG
_stopwatch.Stop();
//Write to log...
string msg = $"topic: {_topic}, time: {_stopwatch.ElapsedMilliseconds}, method: {_methodName}, line: {_lineNumber}";
Console.WriteLine(msg)
#endif
}
}