-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels