This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0b7c69
commit 1d7f34f
Showing
10 changed files
with
177 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Manatee.Json.Serialization; | ||
|
||
namespace Manatee.Json.Tests.Benchmark | ||
{ | ||
public class EmojiResponse | ||
{ | ||
public List<LocalEmoji> Trello { get; set; } | ||
} | ||
|
||
public class LocalEmoji | ||
{ | ||
// functions as ID | ||
public string Unified { get; set; } | ||
public string Native { get; set; } | ||
public string Name { get; set; } | ||
public string ShortName { get; set; } | ||
public List<string> ShortNames { get; set; } | ||
public string Text { get; set; } | ||
public List<string> Texts { get; set; } | ||
public string Category { get; set; } | ||
public int SheetX { get; set; } | ||
public int SheetY { get; set; } | ||
public string Tts { get; set; } | ||
public List<string> Keywords { get; set; } | ||
public Dictionary<string, LocalEmoji> SkinVariations { get; set; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,144 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using JsonSerializer = Manatee.Json.Serialization.JsonSerializer; | ||
|
||
namespace Manatee.Json.Tests.Benchmark | ||
{ | ||
public class PerformanceTests | ||
public static class PerformanceTests | ||
{ | ||
public class MyClass | ||
private static readonly JsonSerializer _serializer = new JsonSerializer | ||
{ | ||
Options = | ||
{ | ||
CaseSensitiveDeserialization = false | ||
} | ||
}; | ||
private static readonly Stopwatch _manateeWatch = new Stopwatch(); | ||
private static long _parseTime; | ||
private static long _serializeTime; | ||
private static long _deserializeTime; | ||
private static long _toStringTime; | ||
private static bool _shouldOutput; | ||
|
||
public static async Task Run() | ||
{ | ||
public string Value { get; set; } | ||
public int OtherValue { get; set; } | ||
public MyClass NestedValue { get; set; } | ||
await _RunBulk(); | ||
} | ||
|
||
private static readonly JsonSerializer _serializer = new JsonSerializer(); | ||
|
||
public static void Run() | ||
private static async Task _RunBulk() | ||
{ | ||
// This initializes whatever caches might be inside the serializer | ||
//_RunSingle(false); | ||
|
||
//_RunSingle(); | ||
_RunBulk(); | ||
var data = await GetEmojiData(); | ||
Console.WriteLine($"Data length: {data.Length}"); | ||
Console.WriteLine($"Object count: 1528"); | ||
|
||
var runCount = 100; | ||
var page = 10; | ||
for (int i = 0; i < runCount; i++) | ||
{ | ||
_shouldOutput = i % page == 0 || i == runCount - 1; | ||
Log($"Run {i}"); | ||
_ExecuteTest(data); | ||
Log(); | ||
Log(); | ||
} | ||
} | ||
|
||
private static void _RunSingle(bool output = true) | ||
private static void _ExecuteTest(string data) | ||
{ | ||
var subjects = new[] {_GenerateSubject()}; | ||
|
||
if (output) | ||
Console.WriteLine("\nNewtonsoft @1"); | ||
//_Run(subjects, JsonConvert.SerializeObject, JsonConvert.DeserializeObject<MyClass>, output); | ||
|
||
if (output) | ||
Console.WriteLine("\nManatee @1"); | ||
_Run(subjects, _ManateeSerialize, _ManateeDeserialize, output); | ||
} | ||
_parseTime = 0; | ||
_deserializeTime = 0; | ||
_serializeTime = 0; | ||
_toStringTime = 0; | ||
|
||
private static void _RunBulk() | ||
{ | ||
var count = 100000; | ||
var subjects = Enumerable.Range(0, count).Select(i => _GenerateSubject()).ToList(); | ||
Log($"\nNewtonsoft"); | ||
_Run(data, JsonConvert.SerializeObject, JsonConvert.DeserializeObject<EmojiResponse>); | ||
|
||
Console.WriteLine($"\nNewtonsoft @{count}"); | ||
_Run(subjects, JsonConvert.SerializeObject, JsonConvert.DeserializeObject<MyClass>); | ||
Log($"\nManatee"); | ||
_Run(data, _ManateeSerialize, _ManateeDeserialize, true); | ||
|
||
Console.WriteLine($"\nManatee @{count}"); | ||
_Run(subjects, _ManateeSerialize, _ManateeDeserialize); | ||
} | ||
|
||
private static void _Run(IEnumerable<MyClass> subjects, Func<MyClass, string> serialize, Func<string, MyClass> deserialize, bool output = true) | ||
private static void _Run(string data, Func<EmojiResponse, string> serialize, Func<string, EmojiResponse> deserialize, bool details = false) | ||
{ | ||
Thread.Sleep(1); | ||
|
||
var watch = new Stopwatch(); | ||
|
||
watch.Start(); | ||
var json = subjects.Select(serialize).ToArray(); | ||
var objects = deserialize(data); | ||
watch.Stop(); | ||
|
||
if (output) | ||
Console.WriteLine($" Serialize: {watch.Elapsed}"); | ||
|
||
Log($" Deserialize: {watch.Elapsed}"); | ||
if (details) | ||
{ | ||
Log($" Parse: {TimeSpan.FromTicks(_parseTime)}"); | ||
Log($" Deserialize: {TimeSpan.FromTicks(_deserializeTime)}"); | ||
} | ||
|
||
watch.Reset(); | ||
watch.Start(); | ||
var back = json.Select(deserialize).ToArray(); | ||
var json = serialize(objects); | ||
watch.Stop(); | ||
|
||
if (output) | ||
Console.WriteLine($" Deserialize: {watch.Elapsed}"); | ||
Log($" Serialize: {watch.Elapsed}"); | ||
if (details) | ||
{ | ||
Log($" Serialize: {TimeSpan.FromTicks(_serializeTime)}"); | ||
Log($" ToString: {TimeSpan.FromTicks(_toStringTime)}"); | ||
} | ||
} | ||
|
||
private static string _ManateeSerialize(MyClass obj) | ||
private static string _ManateeSerialize(EmojiResponse obj) | ||
{ | ||
_manateeWatch.Reset(); | ||
_manateeWatch.Start(); | ||
var json = _serializer.Serialize(obj); | ||
return json.ToString(); | ||
_manateeWatch.Stop(); | ||
_serializeTime += _manateeWatch.ElapsedTicks; | ||
|
||
_manateeWatch.Reset(); | ||
_manateeWatch.Start(); | ||
var str = json.ToString(); | ||
_manateeWatch.Stop(); | ||
_toStringTime += _manateeWatch.ElapsedTicks; | ||
|
||
return str; | ||
} | ||
|
||
private static MyClass _ManateeDeserialize(string jsonString) | ||
private static EmojiResponse _ManateeDeserialize(string jsonString) | ||
{ | ||
_manateeWatch.Reset(); | ||
_manateeWatch.Start(); | ||
var json = JsonValue.Parse(jsonString); | ||
return _serializer.Deserialize<MyClass>(json); | ||
_manateeWatch.Stop(); | ||
_parseTime += _manateeWatch.ElapsedTicks; | ||
|
||
_manateeWatch.Reset(); | ||
_manateeWatch.Start(); | ||
var obj = _serializer.Deserialize<EmojiResponse>(json); | ||
_manateeWatch.Stop(); | ||
_deserializeTime += _manateeWatch.ElapsedTicks; | ||
|
||
return obj; | ||
} | ||
|
||
private static async Task<string> GetEmojiData() | ||
{ | ||
using (var client = new HttpClient()) | ||
using (var response = await client.GetAsync("https://api.trello.com/1/emoji?spritesheets=false&key=062109670e7f56b88783721892f8f66f")) | ||
{ | ||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
} | ||
|
||
private static MyClass _GenerateSubject(int nest = 0) | ||
private static void Log(string message = null) | ||
{ | ||
return new MyClass | ||
{ | ||
Value = Guid.NewGuid().ToString(), | ||
OtherValue = new Random().Next(), | ||
NestedValue = nest < 3 && new Random().Next(10)%2 == 0 ? _GenerateSubject(nest + 1) : null | ||
}; | ||
if (_shouldOutput) | ||
Console.WriteLine(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Manatee.Json.Tests.Benchmark | ||
{ | ||
/// <summary> | ||
/// Enumerates the various skin tone variations for <see cref="Emoji"/>s. | ||
/// </summary> | ||
public enum SkinVariationType | ||
{ | ||
/// <summary> | ||
/// No variation (yellow). | ||
/// </summary> | ||
None, | ||
/// <summary> | ||
/// Light skin. | ||
/// </summary> | ||
Light, | ||
/// <summary> | ||
/// Medium light skin. | ||
/// </summary> | ||
MediumLight, | ||
/// <summary> | ||
/// Medium skin. | ||
/// </summary> | ||
Medium, | ||
/// <summary> | ||
/// Medium dark skin. | ||
/// </summary> | ||
MediumDark, | ||
/// <summary> | ||
/// Dark skin. | ||
/// </summary> | ||
Dark | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.