1
0

DynamicTreeBenchmark.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using BenchmarkDotNet.Attributes;
  2. using Robust.Shared.Analyzers;
  3. using Robust.Shared.Maths;
  4. using Robust.Shared.Physics;
  5. namespace Content.Benchmarks
  6. {
  7. [SimpleJob, MemoryDiagnoser]
  8. [Virtual]
  9. public class DynamicTreeBenchmark
  10. {
  11. private static readonly Box2[] Aabbs1 =
  12. {
  13. ((Box2) default).Enlarged(1), //2x2 square
  14. ((Box2) default).Enlarged(2), //4x4 square
  15. new(-3, 3, -3, 3), // point off to the bottom left
  16. new(-3, -3, -3, -3), // point off to the top left
  17. new(3, 3, 3, 3), // point off to the bottom right
  18. new(3, -3, 3, -3), // point off to the top right
  19. ((Box2) default).Enlarged(1), //2x2 square
  20. ((Box2) default).Enlarged(2), //4x4 square
  21. ((Box2) default).Enlarged(1), //2x2 square
  22. ((Box2) default).Enlarged(2), //4x4 square
  23. ((Box2) default).Enlarged(1), //2x2 square
  24. ((Box2) default).Enlarged(2), //4x4 square
  25. ((Box2) default).Enlarged(1), //2x2 square
  26. ((Box2) default).Enlarged(2), //4x4 square
  27. ((Box2) default).Enlarged(3), //6x6 square
  28. new(-3, 3, -3, 3), // point off to the bottom left
  29. new(-3, -3, -3, -3), // point off to the top left
  30. new(3, 3, 3, 3), // point off to the bottom right
  31. new(3, -3, 3, -3), // point off to the top right
  32. };
  33. private B2DynamicTree<int> _b2Tree;
  34. private DynamicTree<int> _tree;
  35. [GlobalSetup]
  36. public void Setup()
  37. {
  38. _b2Tree = new B2DynamicTree<int>();
  39. _tree = new DynamicTree<int>((in int value) => Aabbs1[value], capacity: 16);
  40. for (var i = 0; i < Aabbs1.Length; i++)
  41. {
  42. var aabb = Aabbs1[i];
  43. _b2Tree.CreateProxy(aabb, uint.MaxValue, i);
  44. _tree.Add(i);
  45. }
  46. }
  47. [Benchmark]
  48. public void BenchB2()
  49. {
  50. object state = null;
  51. _b2Tree.Query(ref state, (ref object _, DynamicTree.Proxy __) => true, new Box2(-1, -1, 1, 1));
  52. }
  53. [Benchmark]
  54. public void BenchQ()
  55. {
  56. foreach (var _ in _tree.QueryAabb(new Box2(-1, -1, 1, 1), true))
  57. {
  58. }
  59. }
  60. }
  61. }