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.

QueueParallelPerformanceTests.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 QueueParallelUtil
  11. {
  12. static public void AllocInt(ref NativeQueue<int> container, int capacity, bool addValues)
  13. => QueueUtil.AllocInt(ref container, capacity, addValues);
  14. static public object AllocBclContainer(int capacity, bool addValues)
  15. {
  16. if (capacity < 0)
  17. return null;
  18. Random.InitState(0);
  19. var bclContainer = new System.Collections.Concurrent.ConcurrentQueue<int>();
  20. if (addValues)
  21. {
  22. for (int i = 0; i < capacity; i++)
  23. bclContainer.Enqueue(i);
  24. }
  25. return bclContainer;
  26. }
  27. }
  28. struct QueueParallelEnqueueGrow : IBenchmarkContainerParallel
  29. {
  30. int capacity;
  31. int workers;
  32. NativeQueue<int> nativeContainer;
  33. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  34. {
  35. this.capacity = capacity;
  36. workers = args[0];
  37. }
  38. public void AllocNativeContainer(int capacity) => QueueParallelUtil.AllocInt(ref nativeContainer, capacity >= 0 ? 0 : -1, false);
  39. public void AllocUnsafeContainer(int capacity) { }
  40. public object AllocBclContainer(int capacity) => QueueParallelUtil.AllocBclContainer(0, false);
  41. public void MeasureNativeContainer(int worker, int threadId)
  42. {
  43. var writer = nativeContainer.AsParallelWriter();
  44. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  45. for (int i = start; i < end; i++)
  46. writer.Enqueue(i, threadId);
  47. }
  48. public void MeasureUnsafeContainer(int worker, int threadId) { }
  49. public void MeasureBclContainer(object container, int worker)
  50. {
  51. var bclContainer = (System.Collections.Concurrent.ConcurrentQueue<int>)container;
  52. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  53. for (int i = start; i < end; i++)
  54. bclContainer.Enqueue(i);
  55. }
  56. }
  57. struct QueueParallelEnqueue : IBenchmarkContainerParallel
  58. {
  59. int capacity;
  60. int workers;
  61. NativeQueue<int> nativeContainer;
  62. void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
  63. {
  64. this.capacity = capacity;
  65. workers = args[0];
  66. }
  67. public void AllocNativeContainer(int capacity) => QueueParallelUtil.AllocInt(ref nativeContainer, capacity, false);
  68. public void AllocUnsafeContainer(int capacity) { }
  69. public object AllocBclContainer(int capacity) => QueueParallelUtil.AllocBclContainer(capacity, false);
  70. public void MeasureNativeContainer(int worker, int threadId)
  71. {
  72. var writer = nativeContainer.AsParallelWriter();
  73. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  74. for (int i = start; i < end; i++)
  75. writer.Enqueue(i, threadId);
  76. }
  77. public void MeasureUnsafeContainer(int worker, int threadId) { }
  78. public void MeasureBclContainer(object container, int worker)
  79. {
  80. var bclContainer = (System.Collections.Concurrent.ConcurrentQueue<int>)container;
  81. ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
  82. for (int i = start; i < end; i++)
  83. bclContainer.Enqueue(i);
  84. }
  85. }
  86. [Benchmark(typeof(BenchmarkContainerType))]
  87. [BenchmarkNameOverride(BenchmarkContainerConfig.BCL, "ConcurrentQueue")]
  88. class QueueParallelWriter
  89. {
  90. #if UNITY_EDITOR
  91. [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + "Queue.ParallelWriter")]
  92. static void RunIndividual()
  93. => BenchmarkContainerConfig.RunBenchmark(typeof(QueueParallelWriter));
  94. #endif
  95. [Test, Performance]
  96. [Category("Performance")]
  97. [BenchmarkTestFootnote]
  98. public unsafe void EnqueueGrow(
  99. [Values(1, 2, 4)] int workers,
  100. [Values(10000, 100000, 1000000)] int insertions,
  101. [Values(BenchmarkContainerType.Native, BenchmarkContainerType.NativeBurstSafety,
  102. BenchmarkContainerType.NativeBurstNoSafety)] BenchmarkContainerType type)
  103. {
  104. BenchmarkContainerRunnerParallel<QueueParallelEnqueueGrow>.Run(workers, insertions, type, workers);
  105. }
  106. [Test, Performance]
  107. [Category("Performance")]
  108. [BenchmarkTestFootnote]
  109. public unsafe void Enqueue(
  110. [Values(1, 2, 4)] int workers,
  111. [Values(10000, 100000, 1000000)] int insertions,
  112. [Values(BenchmarkContainerType.Native, BenchmarkContainerType.NativeBurstSafety,
  113. BenchmarkContainerType.NativeBurstNoSafety)] BenchmarkContainerType type)
  114. {
  115. BenchmarkContainerRunnerParallel<QueueParallelEnqueue>.Run(workers, insertions, type, workers);
  116. }
  117. }
  118. }