Skip to content

Instantly share code, notes, and snippets.

@ahsonkhan
Created January 28, 2020 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahsonkhan/8f86114245fd15b46d084ddfc4935d41 to your computer and use it in GitHub Desktop.
Save ahsonkhan/8f86114245fd15b46d084ddfc4935d41 to your computer and use it in GitHub Desktop.
Benchmark for System.Text.Json Deserialize with various casing approaches
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Collections.Generic;
namespace System.Text.Json.Serialization.Tests
{
public class CaseSensitive
{
private JsonSerializerOptions _options;
private JsonSerializerOptions _optionsNamingPolicy;
private JsonSerializerOptions _aspnetWebDefaults;
private const string _jsonStringPascalCase = "{\"MyString\" : \"abc\", \"MyInteger\" : 123, \"MyList\" : [\"abc\", \"123\"]}";
private const string _jsonStringCamelCase = "{\"myString\" : \"abc\", \"myInteger\" : 123, \"myList\" : [\"abc\", \"123\"]}";
private byte[] _jsonBytesPascalCase;
private byte[] _jsonBytesCamelCase;
[GlobalSetup]
public void Setup()
{
_options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
_optionsNamingPolicy = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
_aspnetWebDefaults = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true };
_jsonBytesPascalCase = Encoding.UTF8.GetBytes(_jsonStringPascalCase);
_jsonBytesCamelCase = Encoding.UTF8.GetBytes(_jsonStringCamelCase);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemTextCaseSensitive_Pascal()
{
return JsonSerializer.Deserialize<MyClass>(_jsonStringPascalCase);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemTextCaseInsensitive_Pascal()
{
return JsonSerializer.Deserialize<MyClass>(_jsonStringPascalCase, _options);
}
// Not sure if this benchmark makes sense
//[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
//[Benchmark]
//public MyClass SystemTextCaseSensitive_Camel()
//{
// return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase);
//}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass_Annotated SystemTextCaseSensitive_Annotated()
{
return JsonSerializer.Deserialize<MyClass_Annotated>(_jsonStringCamelCase);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemTextCaseInsensitive_Camel()
{
return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase, _options);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemTextCaseSensitive_Pascal_Utf8()
{
return JsonSerializer.Deserialize<MyClass>(_jsonBytesPascalCase);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemTextCaseInsensitive_Pascal_Utf8()
{
return JsonSerializer.Deserialize<MyClass>(_jsonBytesPascalCase, _options);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemTextCaseInsensitive_Camel_Utf8()
{
return JsonSerializer.Deserialize<MyClass>(_jsonBytesCamelCase, _options);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemText_CamelNamingPolicy()
{
return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase, _optionsNamingPolicy);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemText_CamelNamingPolicy_Utf8()
{
return JsonSerializer.Deserialize<MyClass>(_jsonBytesCamelCase, _optionsNamingPolicy);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass_Annotated SystemTextCaseSensitive_Annotated_Utf8()
{
return JsonSerializer.Deserialize<MyClass_Annotated>(_jsonBytesCamelCase);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemText_AspnetWebDefaults()
{
return JsonSerializer.Deserialize<MyClass>(_jsonStringCamelCase, _aspnetWebDefaults);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark]
public MyClass SystemText_AspnetWebDefaults_Utf8()
{
return JsonSerializer.Deserialize<MyClass>(_jsonBytesCamelCase, _aspnetWebDefaults);
}
[BenchmarkCategory(Categories.CoreFX, Categories.JSON)]
[Benchmark(Baseline = true)]
public MyClass NewtonSoftJson_Camel()
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(_jsonStringCamelCase);
}
public class MyClass
{
public int MyInteger { get; set; }
public string MyString { get; set; }
public List<string> MyList { get; set; }
}
public class MyClass_Annotated
{
[JsonPropertyName("myInteger")]
public int MyInteger { get; set; }
[JsonPropertyName("myString")]
public string MyString { get; set; }
[JsonPropertyName("myList")]
public List<string> MyList { get; set; }
}
}
}
@ahsonkhan
Copy link
Author

Here are the results:

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.19041
Intel Core i7-6700 CPU 3.40GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.100-alpha1-015914
  [Host]     : .NET Core 5.0.0 (CoreCLR 5.0.19.56303, CoreFX 5.0.19.56306), X64 RyuJIT
  Job-YIVRAB : .NET Core 5.0.0 (CoreCLR 5.0.19.56303, CoreFX 5.0.19.56306), X64 RyuJIT

PowerPlanMode=00000000-0000-0000-0000-000000000000  MaxIterationCount=10  MinIterationCount=5  
WarmupCount=3  
Method Mean Error StdDev Median Min Max Ratio RatioSD Gen 0 Gen 1 Gen 2 Allocated
SystemTextCaseSensitive_Pascal 908.4 ns 12.89 ns 4.60 ns 910.2 ns 900.3 ns 912.0 ns 0.51 0.01 0.0534 - - 224 B
SystemTextCaseInsensitive_Pascal 938.3 ns 17.03 ns 10.13 ns 935.0 ns 927.5 ns 954.3 ns 0.52 0.01 0.0534 - - 224 B
SystemTextCaseSensitive_Annotated 970.9 ns 91.72 ns 60.67 ns 961.2 ns 904.3 ns 1,104.7 ns 0.54 0.03 0.0534 - - 224 B
SystemTextCaseInsensitive_Camel 1,536.0 ns 23.81 ns 8.49 ns 1,536.5 ns 1,526.2 ns 1,547.9 ns 0.86 0.01 0.0725 - - 304 B
SystemTextCaseSensitive_Pascal_Utf8 886.4 ns 27.32 ns 18.07 ns 888.5 ns 844.6 ns 902.5 ns 0.49 0.02 0.0534 - - 224 B
SystemTextCaseInsensitive_Pascal_Utf8 874.8 ns 21.23 ns 12.63 ns 881.1 ns 851.7 ns 885.6 ns 0.49 0.01 0.0534 - - 224 B
SystemTextCaseInsensitive_Camel_Utf8 1,456.4 ns 27.86 ns 16.58 ns 1,456.6 ns 1,424.4 ns 1,481.4 ns 0.81 0.01 0.0725 - - 304 B
SystemText_CamelNamingPolicy 989.2 ns 30.39 ns 20.10 ns 982.4 ns 966.8 ns 1,033.1 ns 0.55 0.01 0.0534 - - 224 B
SystemText_CamelNamingPolicy_Utf8 888.7 ns 29.51 ns 17.56 ns 884.4 ns 871.2 ns 918.1 ns 0.50 0.01 0.0534 - - 224 B
SystemTextCaseSensitive_Annotated_Utf8 909.7 ns 24.72 ns 16.35 ns 907.1 ns 889.8 ns 933.3 ns 0.51 0.02 0.0534 - - 224 B
SystemText_AspnetWebDefaults 1,059.8 ns 23.28 ns 15.40 ns 1,062.7 ns 1,033.6 ns 1,080.3 ns 0.59 0.02 0.0534 - - 224 B
SystemText_AspnetWebDefaults_Utf8 833.0 ns 20.79 ns 12.37 ns 828.2 ns 815.8 ns 855.0 ns 0.47 0.01 0.0534 - - 224 B
NewtonSoftJson_Camel 1,799.2 ns 59.48 ns 39.34 ns 1,795.4 ns 1,759.9 ns 1,882.4 ns 1.00 0.00 0.7401 - - 3104 B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment