|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Configs; |
| 3 | +using BenchmarkDotNet.Running; |
| 4 | +using ModFramework; |
| 5 | +using NUnit.Framework; |
| 6 | +using Terraria; |
| 7 | + |
| 8 | +namespace TerrariaServerAPI.Tests.Benchmarks; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// This tests how well providers can run the clear world logic from vanilla. |
| 12 | +/// </summary> |
| 13 | +[MarkdownExporter] |
| 14 | +public class TileClearWorldBenchmarks : TileBenchmarks |
| 15 | +{ |
| 16 | + [Test] |
| 17 | + public void RunBenchmarks() |
| 18 | + { |
| 19 | + var res = BenchmarkRunner.Run<TileClearWorldBenchmarks>( |
| 20 | + ManualConfig |
| 21 | + .Create(DefaultConfig.Instance) |
| 22 | + .WithOption(ConfigOptions.DisableOptimizationsValidator, true) |
| 23 | + ); |
| 24 | + Assert.That(res.HasCriticalValidationErrors, Is.False); |
| 25 | + } |
| 26 | + |
| 27 | + [Benchmark(Baseline = true), Test] |
| 28 | + public void ClearWorld_Stock() => ClearWorld(_stock); |
| 29 | + |
| 30 | + [Benchmark, Test] |
| 31 | + public void ClearWorld_Heap() => ClearWorld(_heap); |
| 32 | + |
| 33 | + [Benchmark, Test] |
| 34 | + public void ClearWorld_Constileation() => ClearWorld(_const); |
| 35 | + |
| 36 | +#if TILED_PLUGIN |
| 37 | + [Benchmark, Test] |
| 38 | + public void ClearWorld_1d() => ClearWorld(_1d); |
| 39 | + |
| 40 | + [Benchmark, Test] |
| 41 | + public void ClearWorld_2d() => ClearWorld(_2d); |
| 42 | + |
| 43 | + [Benchmark, Test] |
| 44 | + public void ClearWorld_Struct() => ClearWorld(_struct); |
| 45 | +#endif |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// This replicates the clear logic that terraria itself calls. It intentially replicates the tile hooks, as it will |
| 49 | + /// create Terraria.Tile references for the providers to translate. |
| 50 | + /// </summary> |
| 51 | + /// <typeparam name="T"></typeparam> |
| 52 | + /// <param name="requestProvider"></param> |
| 53 | + public void ClearWorld(ICollection<ITile> provider) |
| 54 | + { |
| 55 | + for (int x = 0; x < Main.maxTilesX; x++) |
| 56 | + for (int y = 0; y < Main.maxTilesY; y++) |
| 57 | + { |
| 58 | + if (provider[x, y] is null) |
| 59 | + provider[x, y] = OTAPI.Hooks.Tile.InvokeCreate(); |
| 60 | + else |
| 61 | + provider[x, y].ClearEverything(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments