|
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; } |
|
} |
|
} |