Skip to content

Commit 0cd44f2

Browse files
authored
Merge pull request #27 from boraaros/feature/benchmarkdotnet
Feature/benchmarkdotnet
2 parents 26f9a84 + f229c77 commit 0cd44f2

4 files changed

Lines changed: 138 additions & 0 deletions

File tree

Alligator.Benchmark/Alligator.Benchmark.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
<ProjectReference Include="..\Alligator.Solver\Alligator.Solver.csproj" />
1313
</ItemGroup>
1414

15+
<ItemGroup>
16+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
17+
</ItemGroup>
18+
1519
</Project>

Alligator.Benchmark/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Alligator.SixMaking.Model;
33
using Alligator.Solver;
44
using Alligator.Solver.Algorithms;
5+
using BenchmarkDotNet.Running;
56
using System.Diagnostics;
67

78
namespace Alligator.Benchmark
@@ -10,6 +11,12 @@ class Program
1011
{
1112
static void Main(string[] args)
1213
{
14+
if (args.Contains("--benchmark"))
15+
{
16+
BenchmarkRunner.Run<SolverBenchmarks>();
17+
return;
18+
}
19+
1320
Console.ForegroundColor = ConsoleColor.White;
1421
Console.WriteLine("Hello Six Making Benchmark!");
1522

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"profiles": {
3+
"Diagnostic": {
4+
"commandName": "Project",
5+
"commandLineArgs": ""
6+
},
7+
"BenchmarkDotNet": {
8+
"commandName": "Project",
9+
"commandLineArgs": "--benchmark"
10+
}
11+
}
12+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using Alligator.SixMaking.Logics;
2+
using Alligator.SixMaking.Model;
3+
using Alligator.Solver;
4+
using BenchmarkDotNet.Attributes;
5+
using BenchmarkDotNet.Engines;
6+
7+
namespace Alligator.Benchmark
8+
{
9+
[MemoryDiagnoser]
10+
[SimpleJob(RunStrategy.Monitoring, warmupCount: 1, iterationCount: 5)]
11+
public class SolverBenchmarks
12+
{
13+
private SolverProvider<IPosition, Step> solverFactory = null!;
14+
private ISolver<Step> solver = null!;
15+
private IList<Step> example1History = null!;
16+
private IList<Step> example2History = null!;
17+
private IList<Step> example3History = null!;
18+
private IList<Step> example5History = null!;
19+
20+
[GlobalSetup]
21+
public void GlobalSetup()
22+
{
23+
var rules = new Rules(new StepPool(), new MovingRules());
24+
var solverConfiguration = new Configuration();
25+
solverFactory = new SolverProvider<IPosition, Step>(rules, solverConfiguration);
26+
27+
example1History = BuildHistory(Example1());
28+
example2History = BuildHistory(Example2());
29+
example3History = BuildHistory(Example3());
30+
example5History = BuildHistory(Example5());
31+
}
32+
33+
[IterationSetup]
34+
public void IterationSetup()
35+
{
36+
solver = solverFactory.Create();
37+
}
38+
39+
[Benchmark(Description = "Example1 - Empty board")]
40+
public Step Example1_EmptyBoard() => solver.OptimizeNextStep(example1History);
41+
42+
[Benchmark(Description = "Example2 - Four pieces")]
43+
public Step Example2_FourPieces() => solver.OptimizeNextStep(example2History);
44+
45+
[Benchmark(Description = "Example3 - Twelve pieces")]
46+
public Step Example3_TwelvePieces() => solver.OptimizeNextStep(example3History);
47+
48+
[Benchmark(Description = "Example5 - Complex midgame")]
49+
public Step Example5_ComplexMidgame() => solver.OptimizeNextStep(example5History);
50+
51+
private static IList<Step> BuildHistory(IPosition position)
52+
{
53+
return position.History.ToList();
54+
}
55+
56+
private static IPosition Example1()
57+
{
58+
return new Position();
59+
}
60+
61+
private static IPosition Example2()
62+
{
63+
IPosition position = new Position();
64+
position.Take(new Placement(12));
65+
position.Take(new Placement(13));
66+
position.Take(new Placement(11));
67+
position.Take(new Placement(17));
68+
return position;
69+
}
70+
71+
private static IPosition Example3()
72+
{
73+
IPosition position = new Position();
74+
for (int i = 6; i < 18; i++)
75+
{
76+
position.Take(new Placement(i));
77+
}
78+
return position;
79+
}
80+
81+
private static IPosition Example5()
82+
{
83+
IPosition position = new Position();
84+
position.Take(new Placement(0));
85+
position.Take(new Placement(12));
86+
position.Take(new Placement(1));
87+
position.Take(new Placement(11));
88+
position.Take(new Placement(2));
89+
position.Take(new Placement(17));
90+
position.Take(new Placement(3));
91+
position.Take(new Placement(13));
92+
position.Take(new Placement(4));
93+
position.Take(new Placement(10));
94+
position.Take(new Placement(5));
95+
position.Take(new Placement(15));
96+
position.Take(new Placement(20));
97+
position.Take(new Placement(16));
98+
position.Take(new Movement(5, 10, 1));
99+
position.Take(new Movement(15, 10, 1));
100+
position.Take(new Placement(5));
101+
position.Take(new Placement(6));
102+
position.Take(new Movement(1, 6, 1));
103+
position.Take(new Movement(12, 11, 1));
104+
position.Take(new Movement(10, 17, 1));
105+
position.Take(new Placement(12));
106+
position.Take(new Placement(7));
107+
position.Take(new Placement(22));
108+
position.Take(new Placement(1));
109+
position.Take(new Movement(11, 16, 2));
110+
position.Take(new Placement(21));
111+
position.Take(new Placement(18));
112+
return position;
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)