No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QueuePerformanceTests.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 QueueUtil
  11. {
  12. static public void AllocInt(ref NativeQueue<int> container, int capacity, bool addValues)
  13. {
  14. if (capacity >= 0)
  15. {
  16. Random.InitState(0);
  17. container = new NativeQueue<int>(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 UnsafeQueue<int> container, int capacity, bool addValues)
  28. {
  29. if (capacity >= 0)
  30. {
  31. Random.InitState(0);
  32. container = new UnsafeQueue<int>(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>();
  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 QueueIsEmpty100k : IBenchmarkContainer
  72. {
  73. const int kIterations = 100_000;
  74. NativeQueue<int> nativeContainer;
  75. UnsafeQueue<int> unsafeContainer;
  76. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity, true);
  77. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  78. public object AllocBclContainer(int capacity) => QueueUtil.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. public void MeasureUnsafeContainer()
  86. {
  87. for (int i = 0; i < kIterations; i++)
  88. _ = unsafeContainer.IsEmpty();
  89. }
  90. [MethodImpl(MethodImplOptions.NoOptimization)]
  91. public void MeasureBclContainer(object container)
  92. {
  93. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  94. for (int i = 0; i < kIterations; i++)
  95. _ = bclContainer.Count == 0;
  96. }
  97. }
  98. struct QueueCount100k : IBenchmarkContainer
  99. {
  100. const int kIterations = 100_000;
  101. NativeQueue<int> nativeContainer;
  102. UnsafeQueue<int> unsafeContainer;
  103. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity, true);
  104. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  105. public object AllocBclContainer(int capacity) => QueueUtil.AllocBclContainer(capacity, true);
  106. [MethodImpl(MethodImplOptions.NoOptimization)]
  107. public void MeasureNativeContainer()
  108. {
  109. for (int i = 0; i < kIterations; i++)
  110. _ = nativeContainer.Count;
  111. }
  112. public void MeasureUnsafeContainer()
  113. {
  114. for (int i = 0; i < kIterations; i++)
  115. _ = unsafeContainer.Count;
  116. }
  117. [MethodImpl(MethodImplOptions.NoOptimization)]
  118. public void MeasureBclContainer(object container)
  119. {
  120. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  121. for (int i = 0; i < kIterations; i++)
  122. _ = bclContainer.Count;
  123. }
  124. }
  125. struct QueueToNativeArray : IBenchmarkContainer
  126. {
  127. NativeQueue<int> nativeContainer;
  128. UnsafeQueue<int> unsafeContainer;
  129. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity, true);
  130. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  131. public object AllocBclContainer(int capacity) => QueueUtil.AllocBclContainer(capacity, true);
  132. public void MeasureNativeContainer()
  133. {
  134. var asArray = nativeContainer.ToArray(Allocator.Temp);
  135. asArray.Dispose();
  136. }
  137. public void MeasureUnsafeContainer()
  138. {
  139. var asArray = unsafeContainer.ToArray(Allocator.Temp);
  140. asArray.Dispose();
  141. }
  142. public void MeasureBclContainer(object container)
  143. {
  144. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  145. int[] asArray = new int[bclContainer.Count];
  146. bclContainer.CopyTo(asArray, 0);
  147. }
  148. }
  149. struct QueueEnqueueGrow : IBenchmarkContainer
  150. {
  151. int capacity;
  152. int workers;
  153. NativeQueue<int> nativeContainer;
  154. UnsafeQueue<int> unsafeContainer;
  155. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  156. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity >= 0 ? 0 : -1, false);
  157. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity >= 0 ? 0 : -1, false);
  158. public object AllocBclContainer(int capacity) => QueueUtil.AllocBclContainer(capacity >= 0 ? 0 : -1, false);
  159. public void MeasureNativeContainer()
  160. {
  161. for (int i = 0; i < capacity; i++)
  162. nativeContainer.Enqueue(i);
  163. }
  164. public void MeasureUnsafeContainer()
  165. {
  166. for (int i = 0; i < capacity; i++)
  167. unsafeContainer.Enqueue(i);
  168. }
  169. public void MeasureBclContainer(object container)
  170. {
  171. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  172. for (int i = 0; i < capacity; i++)
  173. bclContainer.Enqueue(i);
  174. }
  175. }
  176. struct QueueEnqueue : IBenchmarkContainer
  177. {
  178. int capacity;
  179. int workers;
  180. NativeQueue<int> nativeContainer;
  181. UnsafeQueue<int> unsafeContainer;
  182. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  183. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity, false);
  184. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity, false);
  185. public object AllocBclContainer(int capacity) => QueueUtil.AllocBclContainer(capacity, false);
  186. public void MeasureNativeContainer()
  187. {
  188. for (int i = 0; i < capacity; i++)
  189. nativeContainer.Enqueue(i);
  190. }
  191. public void MeasureUnsafeContainer()
  192. {
  193. for (int i = 0; i < capacity; i++)
  194. unsafeContainer.Enqueue(i);
  195. }
  196. public void MeasureBclContainer(object container)
  197. {
  198. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  199. for (int i = 0; i < capacity; i++)
  200. bclContainer.Enqueue(i);
  201. }
  202. }
  203. struct QueueDequeue : IBenchmarkContainer
  204. {
  205. int capacity;
  206. NativeQueue<int> nativeContainer;
  207. UnsafeQueue<int> unsafeContainer;
  208. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  209. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity, true);
  210. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  211. public object AllocBclContainer(int capacity) => QueueUtil.AllocBclContainer(capacity, true);
  212. public void MeasureNativeContainer()
  213. {
  214. int keep = 0;
  215. for (int i = 0; i < capacity; i++)
  216. Volatile.Write(ref keep, nativeContainer.Dequeue());
  217. }
  218. public void MeasureUnsafeContainer()
  219. {
  220. int keep = 0;
  221. for (int i = 0; i < capacity; i++)
  222. Volatile.Write(ref keep, unsafeContainer.Dequeue());
  223. }
  224. public void MeasureBclContainer(object container)
  225. {
  226. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  227. for (int i = 0; i < capacity; i++)
  228. {
  229. bclContainer.TryDequeue(out int value);
  230. Volatile.Read(ref value);
  231. }
  232. }
  233. }
  234. struct QueuePeek: IBenchmarkContainer
  235. {
  236. int capacity;
  237. NativeQueue<int> nativeContainer;
  238. UnsafeQueue<int> unsafeContainer;
  239. void IBenchmarkContainer.SetParams(int capacity, params int[] args) => this.capacity = capacity;
  240. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity, true);
  241. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  242. public object AllocBclContainer(int capacity) => QueueUtil.AllocBclContainer(capacity, true);
  243. [MethodImpl(MethodImplOptions.NoOptimization)]
  244. public void MeasureNativeContainer()
  245. {
  246. for (int i = 0; i < capacity; i++)
  247. _ = nativeContainer.Peek();
  248. }
  249. public void MeasureUnsafeContainer()
  250. {
  251. for (int i = 0; i < capacity; i++)
  252. _ = unsafeContainer.Peek();
  253. }
  254. [MethodImpl(MethodImplOptions.NoOptimization)]
  255. public void MeasureBclContainer(object container)
  256. {
  257. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  258. for (int i = 0; i < capacity; i++)
  259. {
  260. bclContainer.TryPeek(out int value);
  261. Volatile.Read(ref value);
  262. }
  263. }
  264. }
  265. struct QueueForEach : IBenchmarkContainer
  266. {
  267. NativeQueue<int> nativeContainer;
  268. UnsafeQueue<int> unsafeContainer;
  269. public int total;
  270. public void AllocNativeContainer(int capacity) => QueueUtil.AllocInt(ref nativeContainer, capacity, true);
  271. public void AllocUnsafeContainer(int capacity) => QueueUtil.AllocInt(ref unsafeContainer, capacity, true);
  272. public object AllocBclContainer(int capacity) => QueueUtil.AllocBclContainer(capacity, true);
  273. public void MeasureNativeContainer()
  274. {
  275. int value = 0;
  276. var ro = nativeContainer.AsReadOnly();
  277. foreach (var element in ro)
  278. Volatile.Write(ref value, element);
  279. }
  280. public void MeasureUnsafeContainer()
  281. {
  282. int value = 0;
  283. var ro = unsafeContainer.AsReadOnly();
  284. foreach (var element in ro)
  285. Volatile.Write(ref value, element);
  286. }
  287. public void MeasureBclContainer(object container)
  288. {
  289. int value = 0;
  290. var bclContainer = (System.Collections.Generic.Queue<int>)container;
  291. foreach (var element in bclContainer)
  292. Volatile.Write(ref value, element);
  293. }
  294. }
  295. [Benchmark(typeof(BenchmarkContainerType))]
  296. class Queue
  297. {
  298. #if UNITY_EDITOR
  299. [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + nameof(Queue))]
  300. static void RunIndividual()
  301. => BenchmarkContainerConfig.RunBenchmark(typeof(Queue));
  302. #endif
  303. [Test, Performance]
  304. [Category("Performance")]
  305. public unsafe void IsEmpty_x_100k(
  306. [Values(0, 100)] int capacity,
  307. [Values] BenchmarkContainerType type)
  308. {
  309. BenchmarkContainerRunner<QueueIsEmpty100k>.Run(capacity, type);
  310. }
  311. [Test, Performance]
  312. [Category("Performance")]
  313. public unsafe void Count_x_100k(
  314. [Values(0, 100)] int capacity,
  315. [Values] BenchmarkContainerType type)
  316. {
  317. BenchmarkContainerRunner<QueueCount100k>.Run(capacity, type);
  318. }
  319. [Test, Performance]
  320. [Category("Performance")]
  321. public unsafe void ToNativeArray(
  322. [Values(10000, 100000, 1000000)] int capacity,
  323. [Values] BenchmarkContainerType type)
  324. {
  325. BenchmarkContainerRunner<QueueToNativeArray>.Run(capacity, type);
  326. }
  327. [Test, Performance]
  328. [Category("Performance")]
  329. [BenchmarkTestFootnote]
  330. public unsafe void EnqueueGrow(
  331. [Values(10000, 100000, 1000000)] int insertions,
  332. [Values] BenchmarkContainerType type)
  333. {
  334. BenchmarkContainerRunner<QueueEnqueueGrow>.Run(insertions, type);
  335. }
  336. [Test, Performance]
  337. [Category("Performance")]
  338. [BenchmarkTestFootnote]
  339. public unsafe void Enqueue(
  340. [Values(10000, 100000, 1000000)] int insertions,
  341. [Values] BenchmarkContainerType type)
  342. {
  343. BenchmarkContainerRunner<QueueEnqueue>.Run(insertions, type);
  344. }
  345. [Test, Performance]
  346. [Category("Performance")]
  347. public unsafe void Dequeue(
  348. [Values(10000, 100000, 1000000)] int insertions,
  349. [Values] BenchmarkContainerType type)
  350. {
  351. BenchmarkContainerRunner<QueueDequeue>.Run(insertions, type);
  352. }
  353. [Test, Performance]
  354. [Category("Performance")]
  355. public unsafe void Peek(
  356. [Values(10000, 100000, 1000000)] int insertions,
  357. [Values] BenchmarkContainerType type)
  358. {
  359. BenchmarkContainerRunner<QueuePeek>.Run(insertions, type);
  360. }
  361. [Test, Performance]
  362. [Category("Performance")]
  363. public unsafe void Foreach(
  364. [Values(10000, 100000, 1000000)] int insertions,
  365. [Values] BenchmarkContainerType type)
  366. {
  367. BenchmarkContainerRunner<QueueForEach>.Run(insertions, type);
  368. }
  369. }
  370. }