Advertisement
Guest User

Untitled

a guest
May 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using Newtonsoft.Json;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using NUnit.Framework;
  7.  
  8. public class JsonPerformanceTest
  9. {
  10. [Serializable]
  11. class SampleEntity
  12. {
  13. public int id;
  14. public string name;
  15. }
  16.  
  17. [Test]
  18. public void EditorTest()
  19. {
  20. int n = 10000;
  21. string json = "{\"name\":\"jojo\",\"id\":1}";
  22. {
  23. Assert.AreEqual(PerformUnityJsonConvert(json).name, "jojo");
  24. Assert.AreEqual(PerformUnityJsonConvert(json).id, 1);
  25.  
  26. StartWatch();
  27. for (int i = 0; i < n; i++) { PerformUnityJsonConvert(json); }
  28. Debug.Log("UnityJson: " + StopWatch());
  29. }
  30. {
  31. Assert.AreEqual(PerformJsonConvert(json).name, "jojo");
  32. Assert.AreEqual(PerformJsonConvert(json).id, 1);
  33.  
  34. StartWatch();
  35. for (int i = 0; i < n; i++) { PerformJsonConvert(json); }
  36. Debug.Log("JsonConvert: " + StopWatch());
  37. }
  38.  
  39. {
  40. Assert.AreEqual(PerformJsonRawConvert(json).name, "jojo");
  41. Assert.AreEqual(PerformJsonRawConvert(json).id, 1);
  42.  
  43. StartWatch();
  44. for (int i = 0; i < n; i++) { PerformJsonRawConvert(json); }
  45. Debug.Log("JsonConvertRaw: " + StopWatch());
  46. }
  47. }
  48.  
  49. private static long currentTime = 0;
  50.  
  51. private static void StartWatch() {
  52. currentTime = CurrentTimeMillis();
  53. }
  54.  
  55. private static long StopWatch() {
  56. long diff = CurrentTimeMillis() - currentTime;
  57. return diff;
  58. }
  59.  
  60. private static long CurrentTimeMillis()
  61. {
  62. System.DateTime epochStart = new System.DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
  63. return (long)(System.DateTime.UtcNow - epochStart).TotalMilliseconds;
  64. }
  65.  
  66. private SampleEntity PerformUnityJsonConvert(string text) {
  67. return JsonUtility.FromJson<SampleEntity>(text);
  68. }
  69.  
  70. private SampleEntity PerformJsonConvert(string text)
  71. {
  72. return JsonConvert.DeserializeObject<SampleEntity>(text);
  73. }
  74.  
  75. private SampleEntity PerformJsonRawConvert(string json)
  76. {
  77. SampleEntity entity = new SampleEntity();
  78. string property = "";
  79.  
  80. using (var reader = new JsonTextReader(new StringReader(json)))
  81. {
  82. while (reader.Read())
  83. {
  84. if (reader.Value != null)
  85. {
  86. if (reader.TokenType == JsonToken.PropertyName)
  87. {
  88. property = reader.Value as string;
  89. }
  90. else if (reader.TokenType == JsonToken.String)
  91. {
  92. if (property.Equals("name"))
  93. {
  94. entity.name = reader.Value as string;
  95. }
  96. }
  97. else if (reader.TokenType == JsonToken.Integer)
  98. {
  99. if (property.Equals("id"))
  100. {
  101. entity.id = int.Parse(reader.Value.ToString());
  102. }
  103. }
  104. }
  105. }
  106. }
  107.  
  108. return entity;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement