|
| 1 | +using ModFramework; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using Terraria; |
| 6 | +using Terraria.Utilities; |
| 7 | +using TerrariaApi.Server; |
| 8 | + |
| 9 | +namespace TerrariaServerAPI.Tests; |
| 10 | + |
| 11 | +public class TileBenchmarks : BaseTest |
| 12 | +{ |
| 13 | + [TestCase(10)] |
| 14 | + public void Clearing_Stock(int cycles) => ClearWorld(() => new DefaultCollection<ITile>(Main.maxTilesX, Main.maxTilesY), cycles); |
| 15 | + |
| 16 | + [TestCase(10)] |
| 17 | + public void Clearing_Heap(int cycles) => ClearWorld(() => new TileProvider(), cycles); |
| 18 | + |
| 19 | + [TestCase(10)] |
| 20 | + public void Clearing_Constileation(int cycles) => ClearWorld(() => new ConstileationProvider(), cycles); |
| 21 | + |
| 22 | +#if TILED_PLUGIN |
| 23 | + [TestCase(10)] |
| 24 | + public void Clearing_1d(int cycles) => ClearWorld(() => new Tiled.OneDimension.OneDimensionTileProvider(), cycles); |
| 25 | + |
| 26 | + [TestCase(10)] |
| 27 | + public void Clearing_2d(int cycles) => ClearWorld(() => new Tiled.TwoDimensions.TwoDimensionTileProvider(), cycles); |
| 28 | + |
| 29 | + [TestCase(10)] |
| 30 | + public void Clearing_Struct(int cycles) => ClearWorld(() => new Tiled.Struct.Structured1DTileProvider(), cycles); |
| 31 | +#endif |
| 32 | + |
| 33 | + public void ClearWorld<T>(Func<T> requestProvider, int cycles) |
| 34 | + where T : ICollection<ITile> |
| 35 | + { |
| 36 | + SetWorldSmall(); |
| 37 | + Main.tile = requestProvider(); |
| 38 | + Console.WriteLine($"{Main.tile.GetType().Name}: Clearing world with {cycles} cycle(s)..."); |
| 39 | + |
| 40 | + var sw = new Stopwatch(); |
| 41 | + sw.Start(); |
| 42 | + for (var i = 0; i < cycles; i++) |
| 43 | + WorldGen.clearWorld(); |
| 44 | + |
| 45 | + sw.Stop(); |
| 46 | + |
| 47 | + Console.WriteLine($"{Main.tile.GetType().Name}: Clear took: {sw.Elapsed.TotalSeconds:#.00}s ({sw.Elapsed.TotalMilliseconds / cycles:#.00}ms p/c)"); |
| 48 | + } |
| 49 | + |
| 50 | + void SetWorldSmall() |
| 51 | + { |
| 52 | + Main.maxTilesX = 4200; |
| 53 | + Main.maxTilesY = 1200; |
| 54 | + } |
| 55 | + |
| 56 | + public void Generate_Small<T>(Func<T> requestProvider) |
| 57 | + where T : ICollection<ITile> |
| 58 | + { |
| 59 | + SetWorldSmall(); |
| 60 | + Main.tile = requestProvider(); |
| 61 | + Console.WriteLine($"{Main.tile.GetType().Name}: Generate starting..."); |
| 62 | + WorldGen.clearWorld(); |
| 63 | + |
| 64 | + var sw = new Stopwatch(); |
| 65 | + sw.Start(); |
| 66 | + CreateNewWorld(); |
| 67 | + sw.Stop(); |
| 68 | + |
| 69 | + Console.WriteLine($"{Main.tile.GetType().Name}: Generate took: {sw.Elapsed.TotalSeconds:#.00}s"); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public void Generate_Small_Stock() |
| 74 | + => Generate_Small(() => new DefaultCollection<ITile>(Main.maxTilesX, Main.maxTilesY)); |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void Generate_Small_Heap() |
| 78 | + => Generate_Small(() => new TileProvider()); |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void Generate_Small_Constileation() |
| 82 | + => Generate_Small(() => new ConstileationProvider()); |
| 83 | + |
| 84 | +#if TILED_PLUGIN |
| 85 | + [Test] |
| 86 | + public void Generate_Small_1d() |
| 87 | + => Generate_Small(() => new Tiled.OneDimension.OneDimensionTileProvider()); |
| 88 | + |
| 89 | + [Test] |
| 90 | + public void Generate_Small_2d() |
| 91 | + => Generate_Small(() => new Tiled.TwoDimensions.TwoDimensionTileProvider()); |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void Generate_Small_Struct() |
| 95 | + => Generate_Small(() => new Tiled.Struct.Structured1DTileProvider()); |
| 96 | +#endif |
| 97 | + |
| 98 | + static void CreateNewWorld() |
| 99 | + { |
| 100 | + WorldGen.generatingWorld = true; |
| 101 | + Main.rand = new UnifiedRandom(9999); |
| 102 | + WorldGen.gen = true; |
| 103 | + Main.menuMode = 888; |
| 104 | + WorldGen.GenerateWorld(9999); |
| 105 | + } |
| 106 | +} |
0 commit comments