Skip to content

Instantly share code, notes, and snippets.

@joncloud
Created April 29, 2020 22:29
Show Gist options
  • Save joncloud/a44639af907ad4068e2ab7e5d23319be to your computer and use it in GitHub Desktop.
Save joncloud/a44639af907ad4068e2ab7e5d23319be to your computer and use it in GitHub Desktop.
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.778 (1909/November2018Update/19H2)
Intel Core i7-8850H CPU 2.60GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=3.1.300-preview-015048
  [Host]     : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT
  DefaultJob : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT

Method Mean Error StdDev Median Gen 0 Gen 1 Gen 2 Allocated
FromString 315.142 μs 5.8171 μs 10.6369 μs 315.401 μs 4.3945 - - 17.92 KB
FromBytes 261.120 μs 4.9478 μs 6.7726 μs 260.883 μs 0.9766 - - 5.63 KB
FromStream 261.504 μs 6.6877 μs 18.8627 μs 253.325 μs 0.9766 - - 4.58 KB
ToEncodedString 6.436 μs 0.1246 μs 0.1576 μs 6.428 μs 0.7782 - - 3.59 KB
ToMemoryStream 5.925 μs 0.1172 μs 0.2692 μs 5.846 μs 0.3738 - - 1.73 KB
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>
<ItemGroup>
<None Update="Test.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ConsoleApp87
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<Tests>();
}
}
[MemoryDiagnoser]
public class Tests
{
static readonly string _asString = @"
[
{
""_id"": ""5ea9fa48a46f46a9c2b2338e"",
""index"": 0,
""guid"": ""2628b8ef-aaf4-4559-95c4-72d0abe7ab16"",
""isActive"": false,
""balance"": ""$3,061.25"",
""picture"": ""http://placehold.it/32x32"",
""age"": 28,
""eyeColor"": ""green"",
""name"": {
""first"": ""Stefanie"",
""last"": ""Bryan""
},
""company"": ""LYRICHORD"",
""email"": ""stefanie.bryan@lyrichord.org"",
""phone"": ""+1 (953) 448-2960"",
""address"": ""140 Bleecker Street, Connerton, American Samoa, 1242"",
""about"": ""Qui mollit officia velit dolore aute irure minim mollit. Elit consequat cupidatat adipisicing aliquip tempor anim officia aliquip eu non duis dolor. Aliquip do fugiat veniam labore esse. Non ipsum est reprehenderit ad dolor sunt cupidatat quis et."",
""registered"": ""Monday, December 25, 2017 1:22 PM"",
""latitude"": ""-24.037376"",
""longitude"": ""58.896979"",
""tags"": [
""amet"",
""ea"",
""quis"",
""ea"",
""in""
],
""range"": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
""friends"": [
{
""id"": 0,
""name"": ""Camille Langley""
},
{
""id"": 1,
""name"": ""Rhodes Combs""
},
{
""id"": 2,
""name"": ""Reilly Bryant""
}
],
""greeting"": ""Hello, Stefanie! You have 6 unread messages."",
""favoriteFruit"": ""apple""
}
]
";
static readonly Example[] _asType = JsonSerializer.Deserialize<Example[]>(_asString);
[Benchmark]
public async Task<Example[]> FromString()
{
var json = await File.ReadAllTextAsync("Test.json");
return JsonSerializer.Deserialize<Example[]>(json);
}
[Benchmark]
public async Task<Example[]> FromBytes()
{
var json = await File.ReadAllBytesAsync("Test.json");
return JsonSerializer.Deserialize<Example[]>(json);
}
[Benchmark]
public async Task<Example[]> FromStream()
{
using var json = File.OpenRead("Test.json");
return await JsonSerializer.DeserializeAsync<Example[]>(json);
}
[Benchmark]
public byte[] ToEncodedString()
{
var json = JsonSerializer.Serialize(_asType);
return Encoding.UTF8.GetBytes(json);
}
[Benchmark]
public async Task<MemoryStream> ToMemoryStream()
{
var memory = new MemoryStream();
await JsonSerializer.SerializeAsync(memory, _asType);
memory.Position = 0;
return memory;
}
}
public class Name
{
public string first { get; set; }
public string last { get; set; }
}
public class Friend
{
public int id { get; set; }
public string name { get; set; }
}
public class Example
{
public string _id { get; set; }
public int index { get; set; }
public string guid { get; set; }
public bool isActive { get; set; }
public string balance { get; set; }
public string picture { get; set; }
public int age { get; set; }
public string eyeColor { get; set; }
public Name name { get; set; }
public string company { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string address { get; set; }
public string about { get; set; }
public string registered { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public List<string> tags { get; set; }
public List<int> range { get; set; }
public List<Friend> friends { get; set; }
public string greeting { get; set; }
public string favoriteFruit { get; set; }
}
}
[
{
"_id": "5ea9fa48a46f46a9c2b2338e",
"index": 0,
"guid": "2628b8ef-aaf4-4559-95c4-72d0abe7ab16",
"isActive": false,
"balance": "$3,061.25",
"picture": "http://placehold.it/32x32",
"age": 28,
"eyeColor": "green",
"name": {
"first": "Stefanie",
"last": "Bryan"
},
"company": "LYRICHORD",
"email": "stefanie.bryan@lyrichord.org",
"phone": "+1 (953) 448-2960",
"address": "140 Bleecker Street, Connerton, American Samoa, 1242",
"about": "Qui mollit officia velit dolore aute irure minim mollit. Elit consequat cupidatat adipisicing aliquip tempor anim officia aliquip eu non duis dolor. Aliquip do fugiat veniam labore esse. Non ipsum est reprehenderit ad dolor sunt cupidatat quis et.",
"registered": "Monday, December 25, 2017 1:22 PM",
"latitude": "-24.037376",
"longitude": "58.896979",
"tags": [
"amet",
"ea",
"quis",
"ea",
"in"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Camille Langley"
},
{
"id": 1,
"name": "Rhodes Combs"
},
{
"id": 2,
"name": "Reilly Bryant"
}
],
"greeting": "Hello, Stefanie! You have 6 unread messages.",
"favoriteFruit": "apple"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment