Skip to content

Benchmarks for #124427 (artl93) #617

@EgorBot

Description

@EgorBot

Processing dotnet/runtime#124427 (comment) command:

Original command:

-amd -arm

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Text.Json;
using System.Text.Json.Serialization;

BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);

public enum Color { Red, Green, Blue, Yellow, Cyan, Magenta, White, Black }

public class SmallPoco
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public bool Active { get; set; }
    public double Score { get; set; }
}

[MemoryDiagnoser]
public class Bench
{
    private byte[] _smallJson = default!;
    private byte[] _nestedJson = default!;
    private byte[] _numberHeavyJson = default!;
    private JsonSerializerOptions _enumOptions = default!;

    [GlobalSetup]
    public void Setup()
    {
        _smallJson = JsonSerializer.SerializeToUtf8Bytes(
            new SmallPoco { Id = 42, Name = "test", Active = true, Score = 3.14 });
        _nestedJson = JsonSerializer.SerializeToUtf8Bytes(
            new { a = new { b = 1, c = "hello\nworld" }, d = new[] { 1, 2, 3 }, e = true, f = 0.0 });
        _numberHeavyJson = System.Text.Encoding.UTF8.GetBytes("[0, 0.1, 0.123, 1, 12, 123, 1234, 12345, 0.999, 1.0e10]");
        _enumOptions = new JsonSerializerOptions { Converters = { new JsonStringEnumConverter() } };
    }

    [Benchmark]
    public void Reader_SmallPoco()
    {
        var reader = new Utf8JsonReader(_smallJson);
        while (reader.Read()) { }
    }

    [Benchmark]
    public void Reader_NestedWithEscapes()
    {
        var reader = new Utf8JsonReader(_nestedJson);
        while (reader.Read()) { }
    }

    [Benchmark]
    public void Reader_NumberHeavy()
    {
        var reader = new Utf8JsonReader(_numberHeavyJson);
        while (reader.Read()) { }
    }

    [Benchmark]
    public byte[] Writer_SmallPoco()
    {
        return JsonSerializer.SerializeToUtf8Bytes(
            new SmallPoco { Id = 42, Name = "test", Active = true, Score = 3.14 });
    }

    [Benchmark]
    public Color Deserialize_Enum_String()
    {
        return JsonSerializer.Deserialize<Color>("\"Blue\"", _enumOptions);
    }

    [Benchmark]
    public SmallPoco Deserialize_SmallPoco()
    {
        return JsonSerializer.Deserialize<SmallPoco>(_smallJson)!;
    }
}

(EgorBot will reply in this issue)

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