A_05 性能调优:采用BenchmarkDotNet对c#代码进行基准测试,

1、BenchmarkDotNet

BenchmarkDotNet可帮助将方法转换为基准、跟踪其性能并共享可重复的测量实验。简而言之,采用BenchmarkDotNet可以对方法或者代码块进行基准测试,可以对代码进行调优,并且对代码进行侵入性很低。可以在同一环境下测试不同的方法性能;可以在不同平台(.net core、.net framework)测试相同的代码,达到测试不同平台的性能。

官方文档资料

2、Demo操作

(1)测试结果重要参数说明:

Method:测试的方法或代码;

Mean:测试方法平均用时;

Allocated:测试时需要分配的内存

A_05 性能调优:采用BenchmarkDotNet对c#代码进行基准测试,

 

 (2)代码

对lua脚本解释器库NLua和MoonSharp进行基准测试,用于比较那个性能更加优秀

using BenchmarkDotNet.Attributes;
using MoonSharp.Interpreter;
using NLua;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenchmarkTestDemo
{
    /// <summary>
    /// Lua脚本性能基准测试
    /// </summary>
    [MemoryDiagnoser]
    public class LuaScriptTestContext
    {
        [Benchmark]
        public void NLua()
        {
            Lua state = new Lua();
            string hex1 = "001100220044555";
            string scriptCode = @"
                    function operate(hexArr,executeCode)
                        if(executeCode==""read"")
                        then
                            local index=1
                            local string=string
                            temp='""temp""'
                            humi='""humi""'
                            local tmp_valu=string.sub(hexArr,3,4)
                            local humi_valu=string.sub(hexArr,7,8)
                            local result = string.format(""{%s:%d,%s:%d}"",temp,tonumber(tmp_valu,16),humi,tonumber(humi_valu,16))
                            return result
                        else
                            return """"
                        end
                    end";
            state.DoString(scriptCode);
            var scriptFunc = state["operate"] as LuaFunction;
            var res = scriptFunc.Call(hex1, "read").First();
        }

        [Benchmark]
        public void MoonSharp()
        {
            string hex1 = "001100220044555";
            string scriptCode = @"
                    function operate(hexArr,executeCode)
                        if(executeCode==""read"")
                        then
                            local index=1
                            local string=string
                            temp='""temp""'
                            humi='""humi""'
                            local tmp_valu=string.sub(hexArr,3,4)
                            local humi_valu=string.sub(hexArr,7,8)
                            local result = string.format(""{%s:%d,%s:%d}"",temp,tonumber(tmp_valu,16),humi,tonumber(humi_valu,16))
                            return result
                        else
                            return """"
                        end
                    end";

            Script script = new Script();
            script.DoString(scriptCode);
            var res = script.Call(script.Globals["operate"], hex1, "read").String;
        }
    }
}
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using System;

namespace BenchmarkTestDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Summary summary = BenchmarkRunner.Run<LuaScriptTestContext>();
            Console.Read();
        }
    }
}

(3)测试报告

A_05 性能调优:采用BenchmarkDotNet对c#代码进行基准测试,

 

 从测试报告可以看出,NLua解释器库性能更加优秀

 

上一篇:Blocking Brute Force Attacks


下一篇:spring boot metrics信息推送开发