Windows Phone 应用 (ARM) 的性能测量

Performance messurement on Windows Phone app (ARM)

我想为我的应用程序性能测量创建一个工具,我可以把它放在某个部分,它会在开始和结束时使用时间戳,然后它会写入记录时间增量.

但是我不希望在我的生产版本中使用此功能(我希望这些测量从生产编译中排除)- 我只希望它们在调试模式下...

我看过几个 AOT 库,比如 Postsharp ,但是它们要花钱,而且大多数(包括 PostSharp)都不支持 ARM 架构。

任何帮助将不胜感激。

我不确定你到底在问什么。如果您可以为此任务构建自己的 class,那就去做吧。如果您需要排除发布版本的日志记录 - 以此为例:

这是class性能测量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class PerfLibTest
    {
#if DEBUG
        private DateTime dtStarted;
#endif

        public void StartIt()
        {
#if DEBUG
            dtStarted = DateTime.Now;
#endif
        }

        public void StopAndLogIt()
        {
#if DEBUG
            //write somewhere (DateTime.Now-dtStarted).TotalMilliseconds
#endif
        }

    }
}

这是class我要测量的功能:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class CLassToTest
    {
        public void DoSomeJob()
        {
            PerfLibTest pf = new PerfLibTest();
            pf.StartIt();

            // do some job

            pf.StopAndLogIt();
        }
    }
}

有条件的

#if DEBUG

将帮助您从 RELEASE 版本中排除测量值。

最终我得到了这样的东西,当你不在调试时它是一个单例,当你处于调试状态时它是一个普通实例: 用法:

using(PerfMesureHandler.Get("Something to meassure")))
{
    //Code to measure
}

代码:

    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
        }        

}