Program.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7. using System.Diagnostics;
  8. namespace ABTest
  9. {
  10. #if DO_ABTEST
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. var random = new Random();
  16. int iterations = 10;
  17. int voxels = 65000;
  18. int voxelTypes = 10;
  19. long totalManaged = 0;
  20. long totalNative = 0;
  21. long totalNaive = 0;
  22. var managed = new SVT.Level_TOP(0);
  23. var native = new SparseVoxelTreePerformanceTest.Chunk(0);
  24. var naive = new int[16 * 64 * 16];
  25. Console.WriteLine("Testing performance of Managed / Native / Naive");
  26. for (var i = 0; i < iterations; ++i)
  27. {
  28. var managedResults = TestLevel(16, 64, 16, (c, v) => managed.SetVoxel(c, v), (c) => managed.GetVoxel(c));
  29. var nativeResults = TestLevel(16, 64, 16, (c, v) => native.SetVoxel(c.X, c.Y, c.Z, v), (c) => native.GetVoxel(c.X, c.Y, c.Z));
  30. var naiveResults = TestLevel(16, 64, 16, (c, v) =>
  31. naive[(c.Y * 16 * 16) + (c.Z * 16) + c.X] = v, (c) =>
  32. naive[(c.Y * 16 * 16) + (c.Z * 16) + c.X]);
  33. Console.WriteLine(String.Format("MC:{0} MT:{1} NC:{2} NT:{3} DIFF:{4:0.000} NAC:{5} NAT:{6} NADIFF:{7:0.000}",
  34. managedResults.Item1,
  35. managedResults.Item3,
  36. nativeResults.Item1,
  37. nativeResults.Item3,
  38. (float)nativeResults.Item3 / (float)managedResults.Item3,
  39. naiveResults.Item1,
  40. naiveResults.Item3,
  41. (float)nativeResults.Item3 / (float)naiveResults.Item3));
  42. totalManaged += managedResults.Item3;
  43. totalNative += nativeResults.Item3;
  44. totalNaive += naiveResults.Item3;
  45. }
  46. Console.WriteLine(String.Format("Overall Diff: {0} Naive Diff: {1}",
  47. (float)totalNative / (float)totalManaged,
  48. (float)totalNative / (float)totalNaive));
  49. Console.WriteLine(String.Format("Worst case memory comparison - Native: {0} Naive: {1}",
  50. native.GetMemoryUsage(), 4 * 16 * 64 * 16));
  51. long memoryUsed = 0;
  52. Console.WriteLine("Comparing memory usage using {0} voxels from pool of {1} voxel types", voxels, voxelTypes);
  53. for (var i = 0; i < iterations; ++i)
  54. {
  55. var nativeMemSizeTest = new SparseVoxelTreePerformanceTest.Chunk(0);
  56. for (var v = 0; v < voxels; ++v)
  57. nativeMemSizeTest.SetVoxel((byte)random.Next(16), (byte)random.Next(45), (byte)random.Next(16), random.Next(voxelTypes) + 1);
  58. nativeMemSizeTest.Compact();
  59. var mem = nativeMemSizeTest.GetMemoryUsage();
  60. Console.WriteLine("M:{0} %:{1}",
  61. mem,
  62. (float)mem / (float)(16 * 64 * 16 * 4));
  63. memoryUsed += mem;
  64. }
  65. Console.WriteLine("Overall M:{0} %:{1}",
  66. memoryUsed,
  67. (float)memoryUsed / (float)(16 * 64 * 16 * 4 * 10));
  68. Console.ReadLine();
  69. }
  70. private static Tuple<int, int, long> TestLevel(int X, int Y, int Z, Action<SVT.MiniPoint3, int> Set, Func<SVT.MiniPoint3, int> Get)
  71. {
  72. var watch = Stopwatch.StartNew();
  73. var good = 0;
  74. var bad = 0;
  75. for (byte x = 0; x < X; ++x)
  76. for (byte y = 0; y < Y; ++y)
  77. for (byte z = 0; z < Z; ++z)
  78. Set(new SVT.MiniPoint3(x, y, z), CalcTestValue(x, y, z));
  79. for (byte x = 0; x < X; ++x)
  80. for (byte z = 0; z < Z; ++z) // Order here intentionally 'wrong'.
  81. for (byte y = 0; y < Y; ++y)
  82. if (Get(new SVT.MiniPoint3(x, y, z)) == CalcTestValue(x, y, z))
  83. good += 1;
  84. else
  85. {
  86. Console.WriteLine("Fail at " + x + ", " + y + ", " + z);
  87. bad += 1;
  88. }
  89. watch.Stop();
  90. return Tuple.Create(good, bad, watch.ElapsedTicks);
  91. }
  92. private static int CalcTestValue(byte x, byte y, byte z)
  93. {
  94. return (x << 16) + (y << 8) + z;
  95. }
  96. }
  97. #else
  98. class Program
  99. {
  100. static void Main(string[] args)
  101. {
  102. }
  103. }
  104. #endif
  105. }