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.

RewindableAllocatorPerformanceTests.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using NUnit.Framework;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using Unity.PerformanceTesting;
  5. using Unity.PerformanceTesting.Benchmark;
  6. namespace Unity.Collections.PerformanceTests
  7. {
  8. struct RewindableAllocationInfo
  9. {
  10. public AllocatorHelper<RewindableAllocator> customAllocator;
  11. BenchmarkAllocatorUtil allocInfo;
  12. AllocatorManager.AllocatorHandle allocatorHandle;
  13. public void CreateAllocator(Allocator builtinOverride)
  14. {
  15. if (builtinOverride != Allocator.None)
  16. allocatorHandle = builtinOverride;
  17. else
  18. {
  19. customAllocator = new AllocatorHelper<RewindableAllocator>(Allocator.Persistent);
  20. customAllocator.Allocator.Initialize(128 * 1024, true);
  21. allocatorHandle = customAllocator.Allocator.Handle;
  22. }
  23. }
  24. public void DestroyAllocator()
  25. {
  26. if (allocatorHandle.IsCustomAllocator)
  27. {
  28. customAllocator.Allocator.Dispose();
  29. customAllocator.Dispose();
  30. }
  31. allocatorHandle = Allocator.Invalid;
  32. }
  33. public void Setup(int workers, int baseSize, int growthRate, int allocations)
  34. {
  35. allocInfo.Setup(workers, baseSize, growthRate, allocations);
  36. }
  37. public void Teardown()
  38. {
  39. allocInfo.Teardown(allocatorHandle);
  40. // Here we presume allocatorHandle == customAllocator.Handle, though there's currently no functionality
  41. // to check that in custom allocatorHandle API
  42. if (allocatorHandle.IsCustomAllocator)
  43. {
  44. customAllocator.Allocator.Rewind();
  45. // Rewinding invalidates the handle, so reassign
  46. allocatorHandle = customAllocator.Allocator.Handle;
  47. }
  48. }
  49. unsafe public void Allocate(int workerI)
  50. {
  51. var inner = allocInfo.AllocPtr[workerI];
  52. for (int i = 0; i < inner.Length; i++)
  53. inner[i] = (IntPtr)AllocatorManager.Allocate(allocatorHandle, allocInfo.AllocSize[i], 0);
  54. }
  55. }
  56. struct Rewindable_FixedSize : IBenchmarkAllocator
  57. {
  58. RewindableAllocationInfo allocInfo;
  59. public void CreateAllocator(Allocator builtinOverride) => allocInfo.CreateAllocator(builtinOverride);
  60. public void DestroyAllocator() => allocInfo.DestroyAllocator();
  61. public void Setup(int workers, int size, int allocations) =>
  62. allocInfo.Setup(workers, size, 0, allocations);
  63. public void Teardown() => allocInfo.Teardown();
  64. public void Measure(int workerI) => allocInfo.Allocate(workerI);
  65. }
  66. struct Rewindable_IncSize : IBenchmarkAllocator
  67. {
  68. RewindableAllocationInfo allocInfo;
  69. public void CreateAllocator(Allocator builtinOverride) => allocInfo.CreateAllocator(builtinOverride);
  70. public void DestroyAllocator() => allocInfo.DestroyAllocator();
  71. public void Setup(int workers, int size, int allocations) =>
  72. allocInfo.Setup(workers, size, size, allocations);
  73. public void Teardown() => allocInfo.Teardown();
  74. public void Measure(int workerI) => allocInfo.Allocate(workerI);
  75. }
  76. struct Rewindable_DecSize : IBenchmarkAllocator
  77. {
  78. RewindableAllocationInfo allocInfo;
  79. public void CreateAllocator(Allocator builtinOverride) => allocInfo.CreateAllocator(builtinOverride);
  80. public void DestroyAllocator() => allocInfo.DestroyAllocator();
  81. public void Setup(int workers, int size, int allocations) =>
  82. allocInfo.Setup(workers, size, -size, allocations);
  83. public void Teardown() => allocInfo.Teardown();
  84. public void Measure(int workerI) => allocInfo.Allocate(workerI);
  85. }
  86. [Benchmark(typeof(BenchmarkAllocatorType))]
  87. [BenchmarkNameOverride("RewindableAllocator")]
  88. class RewindableAllocatorBenchmark
  89. {
  90. [Test, Performance]
  91. [Category("Performance")]
  92. [BenchmarkTestFootnote]
  93. public void FixedSize(
  94. [Values(1, 2, 4, 8)] int workerThreads,
  95. [Values(1024, 1024 * 1024)] int allocSize,
  96. [Values] BenchmarkAllocatorType type)
  97. {
  98. BenchmarkAllocatorRunner<Rewindable_FixedSize>.Run(type, allocSize, workerThreads);
  99. }
  100. [Test, Performance]
  101. [Category("Performance")]
  102. [BenchmarkTestFootnote("Makes linearly increasing allocations [1⋅allocSize, 2⋅allocSize ... N⋅allocSize]")]
  103. public void IncSize(
  104. [Values(1, 2, 4, 8)] int workerThreads,
  105. [Values(4096, 65536)] int allocSize,
  106. [Values] BenchmarkAllocatorType type)
  107. {
  108. BenchmarkAllocatorRunner<Rewindable_IncSize>.Run(type, allocSize, workerThreads);
  109. }
  110. [Test, Performance]
  111. [Category("Performance")]
  112. [BenchmarkTestFootnote("Makes linearly decreasing allocations [N⋅allocSize ... 2⋅allocSize, 1⋅allocSize]")]
  113. public void DecSize(
  114. [Values(1, 2, 4, 8)] int workerThreads,
  115. [Values(4096, 65536)] int allocSize,
  116. [Values] BenchmarkAllocatorType type)
  117. {
  118. BenchmarkAllocatorRunner<Rewindable_DecSize>.Run(type, allocSize, workerThreads);
  119. }
  120. }
  121. }