Skip to content

Rewrite the benchmark "json-serde/1.cs" #457

Open
@iamkisly

Description

@iamkisly

Hi guys. I think the json-serde/1.cs example should be replaced with one that uses System.Text.Json. This is because Netonsoft.Json is becoming obsolete.. and given equivalent code with the same result, it loses a lot in performance.

using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

static class Program
{
    public static async Task Main(string[] args)
    {
        var fileName = args.Length > 0 ? args[0] : "sample";
        int n;
        if (args.Length < 2 || !int.TryParse(args[1], out n))
        {
            n = 10;
        }

        var jsonStr = await File.ReadAllTextAsync($"{fileName}.json").ConfigureAwait(false);
        var jti = JsonTypeInfo.CreateJsonTypeInfo<Object>(JsonSerializerOptions.Default);
        var data = JsonSerializer.Deserialize(jsonStr, jti);

        PrintHash(JsonSerializer.Serialize(data));
        var list = new List<object>(n);
        for (var i = 0; i < n; i++)
        {
            list.Add(JsonSerializer.Deserialize(jsonStr, jti));
        }
        PrintHash(JsonSerializer.Serialize(list));
    }

The result is different enough to be significant.

BenchmarkDotNet v0.14.0, Windows 10 (10.0.19045.5011/22H2/2022Update)
Intel Core i3-8100 CPU 3.60GHz (Coffee Lake), 1 CPU, 4 logical and 4 physical cores
.NET SDK 9.0.100
  [Host]   : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

"Simple" json

Method Job Runtime Mean Error StdDev Ratio RatioSD Gen0 Allocated Alloc Ratio
MainNSJ .NET 9.0 .NET 9.0 142.5 μs 0.70 μs 0.66 μs 1.00 0.01 24.4141 75.2 KB 1.00
MainSTJ .NET 9.0 .NET 9.0 106.0 μs 1.58 μs 1.23 μs 1.00 0.02 9.2773 28.71 KB 1.00

"Canada" json file

Method Mean Error StdDev Median Ratio RatioSD Gen0 Gen1 Gen2 Allocated Alloc Ratio
MainNSJ 192.05 ms 3.782 ms 7.553 ms 188.58 ms 1.00 0.05 9000.0000 6000.0000 2000.0000 54.51 MB 1.00
MainSTJ 37.85 ms 0.568 ms 0.503 ms 37.81 ms 1.00 0.02 1214.2857 1000.0000 428.5714 23.98 MB 1.00

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions