Skip to content

Commit abd0a38

Browse files
committed
Fixed benchmarks
1 parent 22458db commit abd0a38

3 files changed

Lines changed: 63 additions & 40 deletions

File tree

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
44
<TargetFramework>net10.0</TargetFramework>
@@ -7,9 +7,16 @@
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>
99
<ItemGroup>
10-
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
10+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
11+
<PackageReference Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" Version="18.3.36812.1" />
1112
</ItemGroup>
1213
<ItemGroup>
1314
<ProjectReference Include="..\..\src\Earcut\Earcut.csproj" />
1415
</ItemGroup>
15-
</Project>
16+
<ItemGroup>
17+
<None Include="..\..\test\Earcut.Tests\fixtures\**\*" Link="fixtures\%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
18+
</ItemGroup>
19+
<ItemGroup>
20+
<Folder Include="BenchmarkDotNet.Artifacts\" />
21+
</ItemGroup>
22+
</Project>

bench/Earcut.Benchmarks/Program.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ namespace EarcutDotNet.Benchmarks;
99

1010
public class Program
1111
{
12-
public static void Main()
12+
public static void Main(string[] args)
1313
{
14-
BenchmarkRunner.Run<TriangulationBenchmarks>();
14+
var m = new TriangulationBenchmarks();
15+
m.Setup();
16+
m.TriangulateSquare();
17+
m.TriangulateWater();
18+
m.TriangulateWaterHuge();
19+
m.TriangulateDude();
20+
m.TriangulateComplexPolygon();
21+
22+
BenchmarkSwitcher.FromAssembly(typeof(TriangulationBenchmarks).Assembly).Run(args);
1523
}
1624
}

bench/Earcut.Benchmarks/TriangulationBenchmarks.cs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,65 +48,73 @@ public void Setup()
4848

4949
private static void LoadFixture(string name, out double[] vertices, out int[] holes)
5050
{
51-
try
52-
{
53-
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "..", "..", "test", "Earcut.Tests", "fixtures", $"{name}.json");
54-
if (!File.Exists(path))
55-
{
56-
// Fallback to simple polygon
57-
vertices = [0, 0, 10, 0, 10, 10, 0, 10];
58-
holes = [];
59-
return;
60-
}
61-
62-
var json = File.ReadAllText(path);
63-
var coords = JsonSerializer.Deserialize<double[][][]>(json);
64-
if (coords != null)
65-
{
66-
var (Vertices, Holes, Dimensions) = Earcut.Flatten(coords);
67-
vertices = Vertices;
68-
holes = Holes;
69-
}
70-
else
71-
{
72-
vertices = [0, 0, 10, 0, 10, 10, 0, 10];
73-
holes = [];
74-
}
75-
}
76-
catch
77-
{
78-
vertices = [0, 0, 10, 0, 10, 10, 0, 10];
79-
holes = [];
80-
}
51+
string path = Path.Combine(
52+
AppDomain.CurrentDomain.BaseDirectory, "fixtures", $"{name}.json");
53+
54+
var json = File.ReadAllText(path);
55+
var coords = JsonSerializer.Deserialize<double[][][]>(json)
56+
?? throw new InvalidDataException($"Fixture '{name}' deserialized to null.");
57+
58+
(vertices, holes, _) = Earcut.Flatten(coords);
8159
}
8260

8361
[Benchmark]
8462
public IReadOnlyList<int> TriangulateSquare()
8563
{
86-
return Earcut.Triangulate(_square);
64+
var r = Earcut.Triangulate(_square);
65+
if (r.Length == 0)
66+
{
67+
throw new Exception();
68+
}
69+
70+
return r;
8771
}
8872

8973
[Benchmark]
9074
public IReadOnlyList<int> TriangulateComplexPolygon()
9175
{
92-
return Earcut.Triangulate(_complexPolygon);
76+
var r = Earcut.Triangulate(_complexPolygon);
77+
if (r.Length == 0)
78+
{
79+
throw new Exception();
80+
}
81+
82+
return r;
9383
}
9484

9585
[Benchmark]
9686
public IReadOnlyList<int> TriangulateDude()
9787
{
98-
return Earcut.Triangulate(_dudeVertices, _dudeHoles);
88+
var r = Earcut.Triangulate(_dudeVertices, _dudeHoles);
89+
if (r.Length == 0)
90+
{
91+
throw new Exception();
92+
}
93+
94+
return r;
9995
}
10096

10197
[Benchmark]
10298
public IReadOnlyList<int> TriangulateWater()
10399
{
104-
return Earcut.Triangulate(_waterVertices, _waterHoles);
100+
var r = Earcut.Triangulate(_waterVertices, _waterHoles);
101+
if (r.Length == 0)
102+
{
103+
throw new Exception();
104+
}
105+
106+
return r;
105107
}
106108

107109
[Benchmark]
108110
public IReadOnlyList<int> TriangulateWaterHuge()
109111
{
110-
return Earcut.Triangulate(_waterHugeVertices, _waterHugeHoles);
112+
var r = Earcut.Triangulate(_waterHugeVertices, _waterHugeHoles);
113+
if (r.Length == 0)
114+
{
115+
throw new Exception();
116+
}
117+
118+
return r;
111119
}
112120
}

0 commit comments

Comments
 (0)