123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using NUnit.Framework;
- using UnityEngine;
- using Unity.Collections.LowLevel.Unsafe;
- using Unity.PerformanceTesting;
- using Unity.PerformanceTesting.Benchmark;
- using System.Runtime.CompilerServices;
- using System.Threading;
-
- namespace Unity.Collections.PerformanceTests
- {
- static class QueueParallelUtil
- {
- static public void AllocInt(ref NativeQueue<int> container, int capacity, bool addValues)
- => QueueUtil.AllocInt(ref container, capacity, addValues);
-
- static public object AllocBclContainer(int capacity, bool addValues)
- {
- if (capacity < 0)
- return null;
-
- Random.InitState(0);
- var bclContainer = new System.Collections.Concurrent.ConcurrentQueue<int>();
- if (addValues)
- {
- for (int i = 0; i < capacity; i++)
- bclContainer.Enqueue(i);
- }
- return bclContainer;
- }
- }
-
- struct QueueParallelEnqueueGrow : IBenchmarkContainerParallel
- {
- int capacity;
- int workers;
- NativeQueue<int> nativeContainer;
-
- void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
- {
- this.capacity = capacity;
- workers = args[0];
- }
-
- public void AllocNativeContainer(int capacity) => QueueParallelUtil.AllocInt(ref nativeContainer, capacity >= 0 ? 0 : -1, false);
- public void AllocUnsafeContainer(int capacity) { }
- public object AllocBclContainer(int capacity) => QueueParallelUtil.AllocBclContainer(0, false);
-
- public void MeasureNativeContainer(int worker, int threadId)
- {
- var writer = nativeContainer.AsParallelWriter();
- ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
- for (int i = start; i < end; i++)
- writer.Enqueue(i, threadId);
- }
- public void MeasureUnsafeContainer(int worker, int threadId) { }
- public void MeasureBclContainer(object container, int worker)
- {
- var bclContainer = (System.Collections.Concurrent.ConcurrentQueue<int>)container;
- ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
- for (int i = start; i < end; i++)
- bclContainer.Enqueue(i);
- }
- }
-
- struct QueueParallelEnqueue : IBenchmarkContainerParallel
- {
- int capacity;
- int workers;
- NativeQueue<int> nativeContainer;
-
- void IBenchmarkContainerParallel.SetParams(int capacity, params int[] args)
- {
- this.capacity = capacity;
- workers = args[0];
- }
-
- public void AllocNativeContainer(int capacity) => QueueParallelUtil.AllocInt(ref nativeContainer, capacity, false);
- public void AllocUnsafeContainer(int capacity) { }
- public object AllocBclContainer(int capacity) => QueueParallelUtil.AllocBclContainer(capacity, false);
-
- public void MeasureNativeContainer(int worker, int threadId)
- {
- var writer = nativeContainer.AsParallelWriter();
- ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
- for (int i = start; i < end; i++)
- writer.Enqueue(i, threadId);
- }
- public void MeasureUnsafeContainer(int worker, int threadId) { }
- public void MeasureBclContainer(object container, int worker)
- {
- var bclContainer = (System.Collections.Concurrent.ConcurrentQueue<int>)container;
- ParallelHashMapUtil.SplitForWorkers(capacity, worker, workers, out int start, out int end);
- for (int i = start; i < end; i++)
- bclContainer.Enqueue(i);
- }
- }
-
-
-
- [Benchmark(typeof(BenchmarkContainerType))]
- [BenchmarkNameOverride(BenchmarkContainerConfig.BCL, "ConcurrentQueue")]
- class QueueParallelWriter
- {
- #if UNITY_EDITOR
- [UnityEditor.MenuItem(BenchmarkContainerConfig.kMenuItemIndividual + "Queue.ParallelWriter")]
- static void RunIndividual()
- => BenchmarkContainerConfig.RunBenchmark(typeof(QueueParallelWriter));
- #endif
-
- [Test, Performance]
- [Category("Performance")]
- [BenchmarkTestFootnote]
- public unsafe void EnqueueGrow(
- [Values(1, 2, 4)] int workers,
- [Values(10000, 100000, 1000000)] int insertions,
- [Values(BenchmarkContainerType.Native, BenchmarkContainerType.NativeBurstSafety,
- BenchmarkContainerType.NativeBurstNoSafety)] BenchmarkContainerType type)
- {
- BenchmarkContainerRunnerParallel<QueueParallelEnqueueGrow>.Run(workers, insertions, type, workers);
- }
-
- [Test, Performance]
- [Category("Performance")]
- [BenchmarkTestFootnote]
- public unsafe void Enqueue(
- [Values(1, 2, 4)] int workers,
- [Values(10000, 100000, 1000000)] int insertions,
- [Values(BenchmarkContainerType.Native, BenchmarkContainerType.NativeBurstSafety,
- BenchmarkContainerType.NativeBurstNoSafety)] BenchmarkContainerType type)
- {
- BenchmarkContainerRunnerParallel<QueueParallelEnqueue>.Run(workers, insertions, type, workers);
- }
- }
- }
|