Skip to content

Commit c6b0fd8

Browse files
authored
Merge pull request #231 from Pryaxis/constileation-updates
Constileation updates and add in benchmarks to show comparisons between current providers
2 parents 7133dc7 + 4b5004a commit c6b0fd8

16 files changed

Lines changed: 680 additions & 118 deletions

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
run: dotnet build TSAPI.sln -c Release
3232

3333
- name: Run tests
34-
run: dotnet test --filter "FullyQualifiedName!~TerrariaServerAPI.Tests.TileBenchmarks"
34+
run: dotnet test --filter "FullyQualifiedName!~TerrariaServerAPI.Tests.Benchmarks"
3535

3636
# example task for the release CI
3737
# - name: "Releasing to NuGet: TSAPI"

.github/workflows/nuget.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Build
2222
run: dotnet build TSAPI.sln --configuration Release --no-restore
2323
- name: Test
24-
run: dotnet test --no-build --verbosity normal --configuration Release --filter "FullyQualifiedName!~TerrariaServerAPI.Tests.TileBenchmarks"
24+
run: dotnet test --no-build --verbosity normal --configuration Release --filter "FullyQualifiedName!~TerrariaServerAPI.Tests.Benchmarks"
2525

2626
# Publish
2727
- name: Push TSAPI
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using BenchmarkDotNet.Configs;
2+
using BenchmarkDotNet.Running;
3+
using NUnit.Framework;
4+
using System.Linq;
5+
6+
namespace TerrariaServerAPI.Tests.Benchmarks;
7+
8+
/// <summary>
9+
/// Allows all benchmarks to be run in one go (as an alternate to test explorer in VS)
10+
/// </summary>
11+
public class Benchmarks
12+
{
13+
[Test]
14+
public void RunAllBenchmarks()
15+
{
16+
var res = BenchmarkRunner.Run(this.GetType().Assembly,
17+
ManualConfig
18+
.Create(DefaultConfig.Instance)
19+
.WithOption(ConfigOptions.DisableOptimizationsValidator, true)
20+
);
21+
Assert.That(res.Any(x => x.HasCriticalValidationErrors), Is.False);
22+
}
23+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 will test how fast each provider can mark all of it's tiles to active
12+
/// </summary>
13+
[MarkdownExporter]
14+
public class TileActiveBenchmarks : TileBenchmarks
15+
{
16+
[Test]
17+
public void RunBenchmarks()
18+
{
19+
var res = BenchmarkRunner.Run<TileActiveBenchmarks>(
20+
ManualConfig
21+
.Create(DefaultConfig.Instance)
22+
.WithOption(ConfigOptions.DisableOptimizationsValidator, true)
23+
);
24+
Assert.That(res.HasCriticalValidationErrors, Is.False);
25+
}
26+
27+
/// <summary>
28+
/// Ensure the providers all have some data, avoiding NRE while testing individually
29+
/// </summary>
30+
protected override void OnSetup()
31+
{
32+
foreach (var prov in _all)
33+
for (int x = 0; x < Main.maxTilesX; x++)
34+
for (int y = 0; y < Main.maxTilesY; y++)
35+
prov[x, y] = new Tile();
36+
}
37+
38+
[Benchmark(Baseline = true), Test]
39+
public void Active_Stock() => Active(_stock);
40+
41+
[Benchmark, Test]
42+
public void Active_Heap() => Active(_heap);
43+
44+
[Benchmark, Test]
45+
public void Active_Constileation() => Active(_const);
46+
47+
#if TILED_PLUGIN
48+
[Benchmark, Test]
49+
public void Active_1d() => Active(_1d);
50+
51+
[Benchmark, Test]
52+
public void Active_2d() => Active(_2d);
53+
54+
[Benchmark, Test]
55+
public void Active_Struct() => Active(_struct);
56+
#endif
57+
58+
public void Active(ICollection<ITile> provider)
59+
{
60+
for (int x = 0; x < Main.maxTilesX; x++)
61+
for (int y = 0; y < Main.maxTilesY; y++)
62+
{
63+
provider[x, y].active(true);
64+
}
65+
}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 to see how well the providers can store data, issued from itself, since some providers can copy memory instead of using ITile
12+
/// </summary>
13+
[MarkdownExporter]
14+
public class TileAssignsFromSelfBenchmarks : TileBenchmarks
15+
{
16+
[Test]
17+
public void RunBenchmarks()
18+
{
19+
var res = BenchmarkRunner.Run<TileAssignsFromSelfBenchmarks>(
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 AssignFromSelf_Stock() => AssignFromSelf(_stock);
29+
30+
[Benchmark, Test]
31+
public void AssignFromSelf_Heap() => AssignFromSelf(_heap);
32+
33+
[Benchmark, Test]
34+
public void AssignFromSelf_Constileation() => AssignFromSelf(_const);
35+
36+
#if TILED_PLUGIN
37+
[Benchmark, Test]
38+
public void AssignFromSelf_1d() => AssignFromSelf(_1d);
39+
40+
[Benchmark, Test]
41+
public void AssignFromSelf_2d() => AssignFromSelf(_2d);
42+
43+
[Benchmark, Test]
44+
public void AssignFromSelf_Struct() => AssignFromSelf(_struct);
45+
#endif
46+
47+
public void AssignFromSelf(ICollection<ITile> provider)
48+
{
49+
for (int x = 0; x < Main.maxTilesX; x++)
50+
for (int y = 0; y < Main.maxTilesY; y++)
51+
{
52+
provider[x, y] = provider[x, y];
53+
}
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 the providers can assign data from the vanilla Tile instance, as it will need to translate the ITile structure manually.
12+
/// </summary>
13+
[MarkdownExporter]
14+
public class TileAssignsFromTileBenchmarks : TileBenchmarks
15+
{
16+
[Test]
17+
public void RunBenchmarks()
18+
{
19+
var res = BenchmarkRunner.Run<TileAssignsFromTileBenchmarks>(
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 AssignFromTile_Stock() => AssignFromTile(_stock);
29+
30+
[Benchmark, Test]
31+
public void AssignFromTile_Heap() => AssignFromTile(_heap);
32+
33+
[Benchmark, Test]
34+
public void AssignFromTile_Constileation() => AssignFromTile(_const);
35+
36+
#if TILED_PLUGIN
37+
[Benchmark, Test]
38+
public void AssignFromTile_1d() => AssignFromTile(_1d);
39+
40+
[Benchmark, Test]
41+
public void AssignFromTile_2d() => AssignFromTile(_2d);
42+
43+
[Benchmark, Test]
44+
public void AssignFromTile_Struct() => AssignFromTile(_struct);
45+
#endif
46+
47+
public void AssignFromTile(ICollection<ITile> provider)
48+
{
49+
for (int x = 0; x < Main.maxTilesX; x++)
50+
for (int y = 0; y < Main.maxTilesY; y++)
51+
{
52+
provider[x, y] = new Terraria.Tile();
53+
}
54+
}
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 will test how fast all providers will be able to clear every tile they own.
12+
/// </summary>
13+
[MarkdownExporter]
14+
public class TileClearBenchmarks : TileBenchmarks
15+
{
16+
[Test]
17+
public void RunBenchmarks()
18+
{
19+
var res = BenchmarkRunner.Run<TileClearBenchmarks>(
20+
ManualConfig
21+
.Create(DefaultConfig.Instance)
22+
.WithOption(ConfigOptions.DisableOptimizationsValidator, true)
23+
);
24+
Assert.That(res.HasCriticalValidationErrors, Is.False);
25+
}
26+
27+
/// <summary>
28+
/// Ensure the providers all have some data, avoiding NRE while testing individually
29+
/// </summary>
30+
protected override void OnSetup()
31+
{
32+
foreach (var prov in _all)
33+
for (int x = 0; x < Main.maxTilesX; x++)
34+
for (int y = 0; y < Main.maxTilesY; y++)
35+
prov[x, y] = new Tile();
36+
}
37+
38+
[Benchmark(Baseline = true), Test]
39+
public void Clear_Stock() => Clear(_stock);
40+
41+
[Benchmark, Test]
42+
public void Clear_Heap() => Clear(_heap);
43+
44+
[Benchmark, Test]
45+
public void Clear_Constileation() => Clear(_const);
46+
47+
#if TILED_PLUGIN
48+
[Benchmark, Test]
49+
public void Clear_1d() => Clear(_1d);
50+
51+
[Benchmark, Test]
52+
public void Clear_2d() => Clear(_2d);
53+
54+
[Benchmark, Test]
55+
public void Clear_Struct() => Clear(_struct);
56+
#endif
57+
58+
public void Clear(ICollection<ITile> provider)
59+
{
60+
for (int x = 0; x < Main.maxTilesX; x++)
61+
for (int y = 0; y < Main.maxTilesY; y++)
62+
{
63+
provider[x, y].Clear(Terraria.DataStructures.TileDataType.All);
64+
}
65+
}
66+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)