Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RingQueuePerformanceTests.cs 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. using Unity.PerformanceTesting;
  5. using Unity.PerformanceTesting.Benchmark;
  6. using System.Runtime.CompilerServices;
  7. using System.Threading;
  8. namespace Unity.Collections.PerformanceTests
  9. {
  10. static class RingQueueUtil
  11. {
  12. static public void AllocInt(ref NativeRingQueue<int> container, int capacity, bool addValues)
  13. {
  14. if (capacity >= 0)
  15. {
  16. Random.InitState(0);
  17. container = new NativeRingQueue<int>(capacity, Allocator.Persistent);
  18. if (addValues)
  19. {
  20. for (int i = 0; i < capacity; i++)
  21. container.Enqueue(i);
  22. }
  23. }
  24. else
  25. container.Dispose();
  26. }
  27. static public void AllocInt(ref UnsafeRingQueue<int> container, int capacity, bool addValues)
  28. {
  29. if (capacity >= 0)
  30. {
  31. Random.InitState(0);
  32. container = new UnsafeRingQueue<int>(capacity, Allocator.Persistent);
  33. if (addValues)
  34. {
  35. for (int i = 0; i < capacity; i++)
  36. container.Enqueue(i);
  37. }
  38. }
  39. else
  40. container.Dispose();
  41. }
  42. static public object AllocBclContainer(int capacity, bool addValues)
  43. {
  44. if (capacity < 0)
  45. return null;
  46. Random.InitState(0);
  47. var bclContainer = new System.Collections.Generic.Queue<int>(capacity);
  48. if (addValues)
  49. {
  50. for (int i = 0; i < capacity; i++)
  51. bclContainer.Enqueue(i);
  52. }
  53. return bclContainer;
  54. }
  55. static public void CreateRandomValues(int capacity, ref UnsafeList<int> values)
  56. {
  57. if (capacity >= 0)
  58. {
  59. values = new UnsafeList<int>(capacity, Allocator.Persistent);
  60. Random.InitState(0);
  61. for (int i = 0; i < capacity; i++)
  62. {
  63. int randKey = Random.Range(0, capacity);
  64. values.Add(randKey);
  65. }
  66. }
  67. else
  68. values.Dispose();
  69. }
  70. }
  71. struct RingQueueIsEmpty100k : IBenchmarkContainer
  72. {
  73. const int kIterations = 100_000;
  74. NativeRingQueue<int> nativeContainer;
  75. UnsafeRingQueue<int> unsafeContainer;
  76. public void AllocNativeContainer(int capacity) => RingQueueUtil.AllocInt(ref nativeContainer, capacity, true);
  77. public void AllocUnsafeContainer(int capacity) => RingQueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  78. public object AllocBclContainer(int capacity) => RingQueueUtil.AllocBclContainer(capacity, true);
  79. [MethodImpl(MethodImplOptions.NoOptimization)]
  80. public void MeasureNativeContainer()
  81. {
  82. for (int i = 0; i < kIterations; i++)
  83. _ = nativeContainer.IsEmpty;
  84. }
  85. [MethodImpl(MethodImplOptions.NoOptimization)]
  86. public void MeasureUnsafeContainer()
  87. {
  88. for (int i = 0; i < kIterations; i++)
  89. _ = unsafeContainer.IsEmpty;
  90. }
  91. [MethodImpl(MethodImplOptions.NoOptimization)]
  92. public void MeasureBclContainer(object container)
  93. {
  94. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  95. for (int i = 0; i < kIterations; i++)
  96. _ = bclContainer.Count == 0;
  97. }
  98. }
  99. struct RingQueueCount100k : IBenchmarkContainer
  100. {
  101. const int kIterations = 100_000;
  102. NativeRingQueue<int> nativeContainer;
  103. UnsafeRingQueue<int> unsafeContainer;
  104. public void AllocNativeContainer(int capacity) => RingQueueUtil.AllocInt(ref nativeContainer, capacity, true);
  105. public void AllocUnsafeContainer(int capacity) => RingQueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  106. public object AllocBclContainer(int capacity) => RingQueueUtil.AllocBclContainer(capacity, true);
  107. [MethodImpl(MethodImplOptions.NoOptimization)]
  108. public void MeasureNativeContainer()
  109. {
  110. for (int i = 0; i < kIterations; i++)
  111. _ = nativeContainer.Length;
  112. }
  113. [MethodImpl(MethodImplOptions.NoOptimization)]
  114. public void MeasureUnsafeContainer()
  115. {
  116. for (int i = 0; i < kIterations; i++)
  117. _ = unsafeContainer.Length;
  118. }
  119. [MethodImpl(MethodImplOptions.NoOptimization)]
  120. public void MeasureBclContainer(object container)
  121. {
  122. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  123. for (int i = 0; i < kIterations; i++)
  124. _ = bclContainer.Count;
  125. }
  126. }
  127. struct RingQueueEnqueue : IBenchmarkContainer
  128. {
  129. int capacity;
  130. NativeRingQueue<int> nativeContainer;
  131. UnsafeRingQueue<int> unsafeContainer;
  132. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  133. public void AllocNativeContainer(int capacity) => RingQueueUtil.AllocInt(ref nativeContainer, capacity, false);
  134. public void AllocUnsafeContainer(int capacity) => RingQueueUtil.AllocInt(ref unsafeContainer, capacity, false);
  135. public object AllocBclContainer(int capacity) => RingQueueUtil.AllocBclContainer(capacity, false);
  136. public void MeasureNativeContainer()
  137. {
  138. for (int i = 0; i < capacity; i++)
  139. nativeContainer.Enqueue(i);
  140. }
  141. public void MeasureUnsafeContainer()
  142. {
  143. for (int i = 0; i < capacity; i++)
  144. unsafeContainer.Enqueue(i);
  145. }
  146. public void MeasureBclContainer(object container)
  147. {
  148. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  149. for (int i = 0; i < capacity; i++)
  150. bclContainer.Enqueue(i);
  151. }
  152. }
  153. struct RingQueueDequeue : IBenchmarkContainer
  154. {
  155. int capacity;
  156. NativeRingQueue<int> nativeContainer;
  157. UnsafeRingQueue<int> unsafeContainer;
  158. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  159. public void AllocNativeContainer(int capacity) => RingQueueUtil.AllocInt(ref nativeContainer, capacity, true);
  160. public void AllocUnsafeContainer(int capacity) => RingQueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  161. public object AllocBclContainer(int capacity) => RingQueueUtil.AllocBclContainer(capacity, true);
  162. public void MeasureNativeContainer()
  163. {
  164. int keep = 0;
  165. for (int i = 0; i < capacity; i++)
  166. Volatile.Write(ref keep, nativeContainer.Dequeue());
  167. }
  168. public void MeasureUnsafeContainer()
  169. {
  170. int keep = 0;
  171. for (int i = 0; i < capacity; i++)
  172. Volatile.Write(ref keep, unsafeContainer.Dequeue());
  173. }
  174. public void MeasureBclContainer(object container)
  175. {
  176. int keep = 0;
  177. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  178. for (int i = 0; i < capacity; i++)
  179. Volatile.Write(ref keep, bclContainer.Dequeue());
  180. }
  181. }
  182. [Benchmark(typeof(BenchmarkContainerType))]
  183. [BenchmarkNameOverride(BenchmarkContainerConfig.BCL, "Queue")]
  184. class RingQueue
  185. {
  186. #if UNITY_EDITOR
  187. [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + nameof(RingQueue))]
  188. static void RunIndividual()
  189. => BenchmarkContainerConfig.RunBenchmark(typeof(RingQueue));
  190. #endif
  191. [Test, Performance]
  192. [Category("Performance")]
  193. public unsafe void IsEmpty_x_100k(
  194. [Values(0, 100)] int capacity,
  195. [Values] BenchmarkContainerType type)
  196. {
  197. BenchmarkContainerRunner<RingQueueIsEmpty100k>.Run(capacity, type);
  198. }
  199. [Test, Performance]
  200. [Category("Performance")]
  201. public unsafe void Count_x_100k(
  202. [Values(0, 100)] int capacity,
  203. [Values] BenchmarkContainerType type)
  204. {
  205. BenchmarkContainerRunner<RingQueueCount100k>.Run(capacity, type);
  206. }
  207. [Test, Performance]
  208. [Category("Performance")]
  209. public unsafe void Enqueue(
  210. [Values(10000, 100000, 1000000)] int insertions,
  211. [Values] BenchmarkContainerType type)
  212. {
  213. BenchmarkContainerRunner<RingQueueEnqueue>.Run(insertions, type);
  214. }
  215. [Test, Performance]
  216. [Category("Performance")]
  217. public unsafe void Dequeue(
  218. [Values(10000, 100000, 1000000)] int insertions,
  219. [Values] BenchmarkContainerType type)
  220. {
  221. BenchmarkContainerRunner<RingQueueDequeue>.Run(insertions, type);
  222. }
  223. }
  224. }