/ServiceStack.Text/tests/ServiceStack.Text.Tests/Support/BenchmarkTests.cs

https://github.com/ServiceStack/ServiceStack · C# · 281 lines · 276 code · 5 blank · 0 comment · 3 complexity · 86a23631202127e21ccd4c068353a03a MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework;
  5. using ServiceStack.Common.Tests;
  6. using ServiceStack.Text.Tests.JsonTests;
  7. namespace ServiceStack.Text.Tests.Support
  8. {
  9. [Ignore("Long-running benchmark")]
  10. [TestFixture]
  11. public class BenchmarkTests
  12. : PerfTestBase
  13. {
  14. [Test]
  15. public void Test_string_parsing()
  16. {
  17. const int stringSampleSize = 1024 * 10;
  18. var testString = CreateRandomString(stringSampleSize);
  19. var copyTo = new char[stringSampleSize];
  20. CompareMultipleRuns(
  21. "As char array",
  22. () =>
  23. {
  24. var asChars = testString.ToCharArray();
  25. for (var i = 0; i < stringSampleSize; i++)
  26. {
  27. copyTo[i] = asChars[i];
  28. }
  29. },
  30. "As string",
  31. () =>
  32. {
  33. for (var i = 0; i < stringSampleSize; i++)
  34. {
  35. copyTo[i] = testString[i];
  36. }
  37. });
  38. }
  39. public string CreateRandomString(int size)
  40. {
  41. var randString = new char[size];
  42. for (var i = 0; i < size; i++)
  43. {
  44. randString[i] = (char)((i % 10) + '0');
  45. }
  46. return new string(randString);
  47. }
  48. static readonly char[] EscapeChars = new char[]
  49. {
  50. '"', '\n', '\r', '\t', '"', '\\', '\f', '\b',
  51. };
  52. public static readonly char[] JsvEscapeChars = new[]
  53. {
  54. '"', ',', '{', '}', '[', ']',
  55. };
  56. private const int LengthFromLargestChar = '\\' + 1;
  57. private static readonly bool[] HasEscapeChars = new bool[LengthFromLargestChar];
  58. [Test]
  59. public void PrintEscapeChars()
  60. {
  61. JsvEscapeChars.ToList().OrderBy(x => (int)x).Each(x => Console.WriteLine(x + ": " + (int)x));
  62. }
  63. [Test]
  64. public void MeasureIndexOfEscapeChars()
  65. {
  66. foreach (var escapeChar in EscapeChars)
  67. {
  68. HasEscapeChars[escapeChar] = true;
  69. }
  70. var value = CreateRandomString(100);
  71. var len = value.Length;
  72. var hasEscapeChars = false;
  73. CompareMultipleRuns(
  74. "With bool flags",
  75. () =>
  76. {
  77. for (var i = 0; i < len; i++)
  78. {
  79. var c = value[i];
  80. if (c >= LengthFromLargestChar || !HasEscapeChars[c]) continue;
  81. hasEscapeChars = true;
  82. break;
  83. }
  84. },
  85. "With IndexOfAny",
  86. () =>
  87. {
  88. hasEscapeChars = value.IndexOfAny(EscapeChars) != -1;
  89. });
  90. Console.WriteLine(hasEscapeChars);
  91. }
  92. public class RuntimeType<T>
  93. {
  94. private static Type type = typeof(T);
  95. internal static bool TestVarType()
  96. {
  97. return type == typeof(byte) || type == typeof(byte?)
  98. || type == typeof(short) || type == typeof(short?)
  99. || type == typeof(ushort) || type == typeof(ushort?)
  100. || type == typeof(int) || type == typeof(int?)
  101. || type == typeof(uint) || type == typeof(uint?)
  102. || type == typeof(long) || type == typeof(long?)
  103. || type == typeof(ulong) || type == typeof(ulong?)
  104. || type == typeof(bool) || type == typeof(bool?)
  105. || type != typeof(DateTime)
  106. || type != typeof(DateTime?)
  107. || type != typeof(Guid)
  108. || type != typeof(Guid?)
  109. || type != typeof(float) || type != typeof(float?)
  110. || type != typeof(double) || type != typeof(double?)
  111. || type != typeof(decimal) || type != typeof(decimal?);
  112. }
  113. internal static bool TestGenericType()
  114. {
  115. return typeof(T) == typeof(byte) || typeof(T) == typeof(byte?)
  116. || typeof(T) == typeof(short) || typeof(T) == typeof(short?)
  117. || typeof(T) == typeof(ushort) || typeof(T) == typeof(ushort?)
  118. || typeof(T) == typeof(int) || typeof(T) == typeof(int?)
  119. || typeof(T) == typeof(uint) || typeof(T) == typeof(uint?)
  120. || typeof(T) == typeof(long) || typeof(T) == typeof(long?)
  121. || typeof(T) == typeof(ulong) || typeof(T) == typeof(ulong?)
  122. || typeof(T) == typeof(bool) || typeof(T) == typeof(bool?)
  123. || typeof(T) != typeof(DateTime)
  124. || typeof(T) != typeof(DateTime?)
  125. || typeof(T) != typeof(Guid)
  126. || typeof(T) != typeof(Guid?)
  127. || typeof(T) != typeof(float) || typeof(T) != typeof(float?)
  128. || typeof(T) != typeof(double) || typeof(T) != typeof(double?)
  129. || typeof(T) != typeof(decimal) || typeof(T) != typeof(decimal?);
  130. }
  131. internal static bool TestTypeCode()
  132. {
  133. var underlyingType = Nullable.GetUnderlyingType(type) ?? type;
  134. switch (underlyingType.GetTypeCode())
  135. {
  136. case TypeCode.SByte:
  137. case TypeCode.Byte:
  138. case TypeCode.Int16:
  139. case TypeCode.UInt16:
  140. case TypeCode.Int32:
  141. case TypeCode.UInt32:
  142. case TypeCode.Int64:
  143. case TypeCode.UInt64:
  144. case TypeCode.Single:
  145. case TypeCode.Double:
  146. case TypeCode.Decimal:
  147. case TypeCode.DateTime:
  148. return true;
  149. }
  150. return underlyingType == typeof(Guid);
  151. }
  152. }
  153. [Test]
  154. public void TestVarOrGenericType()
  155. {
  156. var matchingTypesCount = 0;
  157. CompareMultipleRuns(
  158. "With var type",
  159. () =>
  160. {
  161. if (RuntimeType<BenchmarkTests>.TestVarType())
  162. {
  163. matchingTypesCount++;
  164. }
  165. },
  166. "With generic type",
  167. () =>
  168. {
  169. if (RuntimeType<BenchmarkTests>.TestGenericType())
  170. {
  171. matchingTypesCount++;
  172. }
  173. });
  174. Console.WriteLine(matchingTypesCount);
  175. }
  176. [Test]
  177. public void TestGenericTypeOrTypeCode()
  178. {
  179. var matchingTypesCount = 0;
  180. CompareMultipleRuns(
  181. "With type code",
  182. () =>
  183. {
  184. if (RuntimeType<BenchmarkTests>.TestTypeCode())
  185. {
  186. matchingTypesCount++;
  187. }
  188. },
  189. "With generic type",
  190. () =>
  191. {
  192. if (RuntimeType<BenchmarkTests>.TestGenericType())
  193. {
  194. matchingTypesCount++;
  195. }
  196. });
  197. Console.WriteLine(matchingTypesCount);
  198. }
  199. [Test]
  200. public void Test_for_list_enumeration()
  201. {
  202. List<Cat> list = 20.Times(x => new Cat { Name = "Cat" });
  203. var results = 0;
  204. var listLength = list.Count;
  205. CompareMultipleRuns(
  206. "With foreach",
  207. () =>
  208. {
  209. foreach (var cat in list)
  210. {
  211. results++;
  212. }
  213. },
  214. "With for",
  215. () =>
  216. {
  217. for (var i = 0; i < listLength; i++)
  218. {
  219. var cat = list[i];
  220. results++;
  221. }
  222. });
  223. Console.WriteLine(results);
  224. }
  225. [Test]
  226. public void Test_for_Ilist_enumeration()
  227. {
  228. IList<Cat> list = 20.Times(x => new Cat { Name = "Cat" });
  229. var results = 0;
  230. var listLength = list.Count;
  231. CompareMultipleRuns(
  232. "With foreach",
  233. () =>
  234. {
  235. foreach (var cat in list)
  236. {
  237. results++;
  238. }
  239. },
  240. "With for",
  241. () =>
  242. {
  243. for (var i = 0; i < listLength; i++)
  244. {
  245. var cat = list[i];
  246. results++;
  247. }
  248. });
  249. Console.WriteLine(results);
  250. }
  251. }
  252. }