XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text;
using BenchmarkDotNet.Attributes;
using XFEExtension.NetCore.XFETransform.Json;

namespace XFEExtension.NetCore.Benchmarks;

[MemoryDiagnoser]
public class JsonBenchmarks
{
    private string _json = null!;
    private XFEJsonNode _node = null!;

    [Params(1_000, 40_000)]
    public int ElementCount { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        var builder = new StringBuilder(ElementCount * 32);
        builder.Append("{\"first\":1,\"items\":[");
        for (var index = 0; index < ElementCount; index++)
        {
            if (index > 0) builder.Append(',');
            builder.Append("{\"id\":").Append(index).Append(",\"text\":\"item-").Append(index).Append("\"}");
        }
        builder.Append("],\"last\":2}");
        _json = builder.ToString();
        _node = XFEJson.Parse(_json);
    }

    [Benchmark(Baseline = true)]
    public XFEJsonNode ParseOnly() => XFEJson.Parse(_json);

    [Benchmark]
    public int EarlyProperty() => XFEJson.Parse(_json)["first"]!.GetInt32();

    [Benchmark]
    public int DeepArrayElement() => XFEJson.Parse(_json)["items"]![ElementCount - 1]!["id"]!.GetInt32();

    [Benchmark]
    public int CachedDeepArrayElement() => _node["items"]![ElementCount - 1]!["id"]!.GetInt32();

    [Benchmark]
    public void ValidateDocument() => XFEJson.Validate(_json);
}