Skip to content

Commit 6ba8daf

Browse files
committed
implementation of benchmarks
1 parent 689fd7d commit 6ba8daf

File tree

6 files changed

+132
-30
lines changed

6 files changed

+132
-30
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Dijkstra.NET.Benchmark
2+
{
3+
using Contract;
4+
using Model;
5+
using ShortestPath;
6+
7+
public class BfsParallelBenchmark: DijkstraBenchmarkBase
8+
{
9+
public BfsParallelBenchmark()
10+
{
11+
}
12+
13+
public BfsParallelBenchmark(Graph<int, string> graph, uint from, uint to): base(graph, from, to)
14+
{
15+
}
16+
17+
public override IShortestPathResult GetPath()
18+
{
19+
var bfs = new BfsParallel<int, string>(_graph);
20+
return bfs.Process(_from, _to);
21+
}
22+
}
23+
}

src/Dijkstra.NET/Dijkstra.NET.Benchmark/Dijkstra.NET.Benchmark.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Reference Include="System.Xml" />
6565
</ItemGroup>
6666
<ItemGroup>
67+
<Compile Include="BfsParallelBenchmark.cs" />
6768
<Compile Include="DijkstraBenchmark.cs" />
6869
<Compile Include="DijkstraBenchmarkBase.cs" />
6970
<Compile Include="Program.cs" />

src/Dijkstra.NET/Dijkstra.NET.Benchmark/DijkstraBenchmark.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
namespace Dijkstra.NET.Benchmark
22
{
33
using Contract;
4+
using Model;
45
using ShortestPath;
56

67
public class DijkstraBenchmark: DijkstraBenchmarkBase
78
{
8-
public IShortestPathResult GetPath()
9+
10+
public DijkstraBenchmark()
11+
{
12+
}
13+
14+
public DijkstraBenchmark(Graph<int, string> graph, uint from, uint to) : base(graph, from, to)
15+
{
16+
}
17+
18+
public override IShortestPathResult GetPath()
919
{
1020
var dijkstra = new Dijkstra<int, string>(_graph);
11-
return dijkstra.Process(@from, to);
21+
return dijkstra.Process(_from, _to);
1222
}
1323

1424
}
Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
11
namespace Dijkstra.NET.Benchmark
22
{
33
using System;
4+
using Contract;
45
using Model;
56

6-
public class DijkstraBenchmarkBase
7+
public abstract class DijkstraBenchmarkBase
78
{
8-
protected readonly Graph<int, string> _graph = new Graph<int, string>();
9+
protected readonly Graph<int, string> _graph;
910

10-
private const int Nodes = 1000000;
11+
private const int Nodes = 10000000;
1112
private const int Connections = 10000000;
1213

13-
protected uint from;
14-
protected uint to;
14+
private const int ConnectionLevel = 300000;
15+
16+
protected uint _from;
17+
protected uint _to;
18+
19+
private bool _graphFromCtor;
20+
21+
protected DijkstraBenchmarkBase()
22+
{
23+
_graph = new Graph<int, string>();
24+
}
25+
26+
protected DijkstraBenchmarkBase(Graph<int, string> graph, uint from, uint to)
27+
{
28+
_graph = graph;
29+
_from = from;
30+
_to = to;
31+
_graphFromCtor = true;
32+
}
33+
34+
public Graph<int, string> Graph => _graph;
35+
public uint From => _from;
36+
public uint To => _to;
37+
1538

1639
public void Setup()
1740
{
41+
if (_graphFromCtor)
42+
{
43+
_graph.Reset();
44+
return;
45+
}
46+
1847
for (int i = 0; i < Nodes; i++)
1948
_graph.AddNode(i);
2049

@@ -24,24 +53,50 @@ public void Setup()
2453
{
2554
int node1 = random.Next(1, Nodes);
2655
int node2 = random.Next(1, Nodes);
27-
int cost = random.Next(1, 50);
56+
int cost = random.Next(15, 50);
2857

29-
_graph.Connect((uint) node1, (uint) node2, cost, null);
58+
_graph.Connect((uint) node1, (uint) node2, cost, null);
3059
}
3160

32-
_graph.Connect(0, 5, 10, null);
33-
_graph.Connect(5, 121, 2, null);
34-
_graph.Connect(121, 115, 1, null);
35-
_graph.Connect(115, 300, 4, null);
36-
_graph.Connect(300, 855, 2, null);
37-
_graph.Connect(855, 1600, 1, null);
38-
_graph.Connect(1600, 5000, 4, null);
39-
_graph.Connect(5000, 50, 1, null);
40-
_graph.Connect(50, 21, 1, null);
41-
_graph.Connect(21, Nodes-1, 1, null);
42-
43-
@from = 0;
44-
to = Nodes-1;
61+
for (uint i = 0; i < ConnectionLevel; i++)
62+
{
63+
int cost = random.Next(1, 5);
64+
_graph.Connect(0, i + 1, cost, null);
65+
}
66+
67+
for (uint i = 0; i < ConnectionLevel; i++)
68+
{
69+
int node1 = random.Next(1, ConnectionLevel);
70+
int node2 = random.Next(ConnectionLevel, ConnectionLevel*2);
71+
int cost = random.Next(1, 5);
72+
73+
_graph.Connect((uint) node1, (uint) node2, cost, null);
74+
}
75+
76+
for (uint i = 0; i < ConnectionLevel; i++)
77+
{
78+
int node1 = random.Next(ConnectionLevel, 2*ConnectionLevel);
79+
int node2 = random.Next(2*ConnectionLevel, 3*ConnectionLevel);
80+
int cost = random.Next(1, 5);
81+
82+
_graph.Connect((uint) node1, (uint) node2, cost, null);
83+
}
84+
85+
_graph.Connect(0, 5, 10000000, null);
86+
_graph.Connect(5, 121, 10000000, null);
87+
_graph.Connect(121, 115, 10000000, null);
88+
_graph.Connect(115, 300, 10000000, null);
89+
_graph.Connect(300, 855, 10000000, null);
90+
_graph.Connect(855, 1600, 10000000, null);
91+
_graph.Connect(1600, 5000, 10000000, null);
92+
_graph.Connect(5000, 50, 10000000, null);
93+
_graph.Connect(50, 21, 10000000, null);
94+
_graph.Connect(21, Nodes-1, 10000000, null);
95+
96+
_from = 0;
97+
_to = Nodes-1;
4598
}
99+
100+
public abstract IShortestPathResult GetPath();
46101
}
47102
}

src/Dijkstra.NET/Dijkstra.NET.Benchmark/Program.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,31 @@ class Program
99
{
1010
static void Main()
1111
{
12-
var normal = new DijkstraBenchmark();
13-
normal.Setup();
12+
var bfs = new BfsParallelBenchmark();
13+
Measure(bfs);
1414

15-
var stopWatch = Stopwatch.StartNew();
16-
IShortestPathResult result = normal.GetPath();
15+
GC.Collect();
16+
17+
Measure(new DijkstraBenchmark(bfs.Graph, bfs.From, bfs.To));
18+
Console.ReadKey();
19+
}
20+
21+
static void Measure(DijkstraBenchmarkBase benchmark)
22+
{
23+
string benchmarkName = benchmark.GetType().Name;
24+
25+
Console.WriteLine($"--- Start {benchmarkName} benchmark. --- \r\n");
26+
benchmark.Setup();
27+
28+
var stopWatch = Stopwatch.StartNew();
29+
IShortestPathResult result = benchmark.GetPath();
1730
stopWatch.Stop();
1831

1932
uint[] path = result.GetPath().ToArray();
2033

21-
Console.WriteLine($"Dijkstra takes {stopWatch.ElapsedMilliseconds} ms. Length of path is {path.Length}.");
34+
Console.WriteLine($"{benchmarkName} takes {stopWatch.ElapsedMilliseconds} ms. Length of path is {path.Length}.");
2235
Console.WriteLine($"Path: {path.Select(x => x.ToString()).Aggregate((a, b) => a + " -> " + b)}");
23-
24-
Console.ReadKey();
36+
Console.WriteLine($"--- End {benchmarkName} benchmark. --- \r\n");
2537
}
2638
}
2739
}

src/Dijkstra.NET/Dijkstra.NET/ShortestPath/Utility/ProducerConsumer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
namespace Dijkstra.NET.ShortestPath.Utility
22
{
33
using System;
4+
using System.Collections.Concurrent;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using Contract;
78
using Model;
89

910
internal sealed class ProducerConsumer<T, TEdgeCustom> where TEdgeCustom : IEquatable<TEdgeCustom>
1011
{
11-
private readonly Collection<Emitter> _table = new Collection<Emitter>();
12+
private readonly BlockingCollection<Emitter> _table = new BlockingCollection<Emitter>(50);
1213

1314
private int _currentJobs;
1415

0 commit comments

Comments
 (0)