Brak opisu
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.

JobStressTests.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Collections;
  4. using Unity.Jobs;
  5. namespace Unity.Jobs.Tests.ManagedJobs
  6. {
  7. internal class JobStressTests : JobTestsFixture
  8. {
  9. struct JobSetIndexValue : IJobParallelFor
  10. {
  11. public NativeArray<int> value;
  12. public void Execute(int index)
  13. {
  14. value[index] = index;
  15. }
  16. }
  17. [Test]
  18. public void StressTestParallelFor()
  19. {
  20. StressTestParallelForIterations(1, 5000);
  21. }
  22. public void StressTestParallelForIterations(int amount, int amountOfData)
  23. {
  24. for (var k = 0; k != amount; k++)
  25. {
  26. var len = UnityEngine.Random.Range(1, amountOfData);
  27. JobSetIndexValue job1;
  28. job1.value = CollectionHelper.CreateNativeArray<int>(len, RwdAllocator.ToAllocator);
  29. JobSetIndexValue job2;
  30. job2.value = CollectionHelper.CreateNativeArray<int>(len, RwdAllocator.ToAllocator);
  31. var job1Handle = job1.Schedule(len, UnityEngine.Random.Range(1, 1024));
  32. var job2Handle = job2.Schedule(len, UnityEngine.Random.Range(1, 1024));
  33. job2Handle.Complete();
  34. job1Handle.Complete();
  35. for (var i = 0; i < len; i++)
  36. {
  37. Assert.AreEqual(i, job1.value[i]);
  38. Assert.AreEqual(i, job2.value[i]);
  39. }
  40. }
  41. }
  42. struct JobSetValue : IJob
  43. {
  44. public int expected;
  45. public NativeArray<int> value;
  46. public void Execute()
  47. {
  48. value[0] = value[0] + 1;
  49. }
  50. }
  51. [Test]
  52. public void DeepDependencyChain()
  53. {
  54. var array = new NativeArray<int>(1, Allocator.Persistent);
  55. var jobHandle = new JobHandle();
  56. const int depth = 10000;
  57. for (var i = 0; i < depth; i++)
  58. {
  59. var job = new JobSetValue
  60. {
  61. value = array,
  62. expected = i
  63. };
  64. jobHandle = job.Schedule(jobHandle);
  65. }
  66. jobHandle.Complete();
  67. Assert.AreEqual(depth, array[0]);
  68. array.Dispose();
  69. }
  70. }
  71. }