Loading

DOTNET 平台代码分析工具 BenchmarkWatch.NET 及 IEnumerable<T> 和 IEnumerator<T> 性能测试

BenchmarkWatch.NET 介绍

BenchmarkWatch.NET 是为 DOTNET 平台快速定位代码运行耗时所设计, 优化代码执行效率, 快速分析代码质量的分析工具.

支持版本:

  • net45
  • netstandard2.0
  • netstandard2.1
  • net6.0
  • net7.0

安装

DOTNET CLI

dotnet add package BenchmarkWatch.NET

快速使用

  1. 创建 BenchmarkWatch 对象
var watch = new BenchmarkWatch();
  1. 使用 BenchmarkWatch.Mark() 方法添加检查标记点.
var watch = new BenchmarkWatch();
watch.Mark();
  1. 使用 BenchmarkWatch.Print() 获取记录字符串信息
var watch = new BenchmarkWatch();
watch.Mark();
var msgs = watch.Print();

使用示例2

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

namespace BenchmarkWatch.Test
{
    public class Program
    {
        public static NET.BenchmarkWatch BenchmarkWatch = new NET.BenchmarkWatch();
        public static void Main(params string[] args)
        {
            BenchmarkWatch.Mark();

            var msgs = GetNext();
            BenchmarkWatch.Mark();
            var msgsor = msgs.GetEnumerator();

            BenchmarkWatch.Mark();

            while (msgsor.MoveNext())
            {
                BenchmarkWatch.Mark();
                Console.WriteLine(msgsor.Current);
                BenchmarkWatch.Mark();
            }
            BenchmarkWatch.Mark();

            var msges = BenchmarkWatch.Print();
            foreach (var msg in msges)
            {
                Console.WriteLine(msg);
            }
        }
        

        public static IEnumerable<string> GetNext()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);

                yield return string.Format("run {0}", i);
            }
        }

    }
}

使用示例2

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using BenchmarkWatch.NET;

namespace BenchmarkWatch.Test
{
    public class Program
    {
        public static void Main(params string[] args)
        {
            var test = new Test();

            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            test.Method1();
            test.Method2();
            test.Method3();
            var lines = test.Put();
            foreach (var ln in lines)
            {
                Console.WriteLine(ln);
            }
            
        }


    }

    public class Test
    {
        private NET.BenchmarkWatch _watch = new NET.BenchmarkWatch();

        public void Method1()
        {
            Thread.Sleep(520);
            _watch.Mark();
        }


        public void Method2()
        {
            Thread.Sleep(620);
            _watch.Mark();
        }

        public void Method3()
        {
            Thread.Sleep(310);
            _watch.Mark();
        }

        public IEnumerable<string> Put()
        {
            return _watch.Print();
        }
    }
}

输出

_watch(at /root/works/BenchmarkWatch.NET/test/Program.cs:65) -> Method1(at /root/works/BenchmarkWatch.NET/test/Program.cs:70) during took a total of 525ms, and executed is 1 times, the avg a times is 525ms
Method1(at /root/works/BenchmarkWatch.NET/test/Program.cs:70) -> Method2(at /root/works/BenchmarkWatch.NET/test/Program.cs:77) during took a total of 7443ms, and executed is 12 times, the avg a times is 620ms
Method2(at /root/works/BenchmarkWatch.NET/test/Program.cs:77) -> Method3(at /root/works/BenchmarkWatch.NET/test/Program.cs:83) during took a total of 3720ms, and executed is 12 times, the avg a times is 310ms
Method3(at /root/works/BenchmarkWatch.NET/test/Program.cs:83) -> Method1(at /root/works/BenchmarkWatch.NET/test/Program.cs:70) during took a total of 5720ms, and executed is 11 times, the avg a times is 520ms
posted @ 2022-11-10 15:56  microestc  阅读(76)  评论(0编辑  收藏  举报