Skip to content

Commit 7b4e55d

Browse files
committed
K Shortest Paths: benchamrk
1 parent 190e4be commit 7b4e55d

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*-
2+
* Copyright 2023 Barak Ugav
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.jgalgo.bench.impls;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.Random;
22+
import java.util.concurrent.TimeUnit;
23+
import java.util.concurrent.atomic.AtomicInteger;
24+
import org.openjdk.jmh.annotations.Benchmark;
25+
import org.openjdk.jmh.annotations.BenchmarkMode;
26+
import org.openjdk.jmh.annotations.Fork;
27+
import org.openjdk.jmh.annotations.Level;
28+
import org.openjdk.jmh.annotations.Measurement;
29+
import org.openjdk.jmh.annotations.Mode;
30+
import org.openjdk.jmh.annotations.OutputTimeUnit;
31+
import org.openjdk.jmh.annotations.Param;
32+
import org.openjdk.jmh.annotations.Scope;
33+
import org.openjdk.jmh.annotations.Setup;
34+
import org.openjdk.jmh.annotations.State;
35+
import org.openjdk.jmh.annotations.Warmup;
36+
import org.openjdk.jmh.infra.Blackhole;
37+
import com.jgalgo.alg.KShortestPathsST;
38+
import com.jgalgo.bench.util.BenchUtils;
39+
import com.jgalgo.bench.util.GraphsTestUtils;
40+
import com.jgalgo.bench.util.TestUtils.SeedGenerator;
41+
import com.jgalgo.graph.Graphs;
42+
import com.jgalgo.graph.IWeightFunction;
43+
import com.jgalgo.graph.IWeightFunctionInt;
44+
import com.jgalgo.graph.IntGraph;
45+
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
46+
47+
public class KShortestPathsSTBench {
48+
49+
private List<BenchArgs> graphs;
50+
private final int graphsNum = 31;
51+
private final AtomicInteger graphIdx = new AtomicInteger();
52+
53+
private void setup(String args, boolean directed) {
54+
Map<String, String> argsMap = BenchUtils.parseArgsStr(args);
55+
int n = Integer.parseInt(argsMap.get("|V|"));
56+
int m = Integer.parseInt(argsMap.get("|E|"));
57+
int k = Integer.parseInt(argsMap.get("k"));
58+
59+
final SeedGenerator seedGen = new SeedGenerator(0xb6b1a069899f9baaL);
60+
final Random rand = new Random(seedGen.nextSeed());
61+
graphs = new ObjectArrayList<>(graphsNum);
62+
for (int gIdx = 0; gIdx < graphsNum; gIdx++) {
63+
IntGraph g = GraphsTestUtils.randGraph(n, m, directed, seedGen.nextSeed());
64+
IWeightFunctionInt w = GraphsTestUtils.assignRandWeightsInt(g, 0, 64, seedGen.nextSeed());
65+
int source = Graphs.randVertex(g, rand);
66+
int target = Graphs.randVertex(g, rand);
67+
graphs.add(new BenchArgs(g, w, source, target, k));
68+
}
69+
}
70+
71+
@SuppressWarnings("boxing")
72+
private void benchAlgo(KShortestPathsST algo, Blackhole blackhole) {
73+
BenchArgs args = graphs.get(graphIdx.getAndUpdate(i -> (i + 1) % graphsNum));
74+
Object res = algo.computeKShortestPaths(args.g, args.w, args.source, args.target, args.k);
75+
blackhole.consume(res);
76+
}
77+
78+
@BenchmarkMode(Mode.AverageTime)
79+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
80+
@Warmup(iterations = 2, time = 3, timeUnit = TimeUnit.SECONDS)
81+
@Measurement(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS)
82+
@Fork(value = 1, warmups = 0)
83+
@State(Scope.Benchmark)
84+
public static class Directed extends KShortestPathsSTBench {
85+
86+
@Param({ "|V|=200 |E|=1500 k=5", "|V|=800 |E|=10000 k=15", "|V|=1500 |E|=3000 k=50" })
87+
public String args;
88+
89+
@Setup(Level.Trial)
90+
public void setup() {
91+
super.setup(args, true);
92+
}
93+
94+
@Benchmark
95+
public void Yen(Blackhole blackhole) {
96+
KShortestPathsST.Builder builder = KShortestPathsST.builder();
97+
builder.setOption("impl", "yen");
98+
super.benchAlgo(builder.build(), blackhole);
99+
}
100+
101+
@Benchmark
102+
public void HershbergerMaxelSuri(Blackhole blackhole) {
103+
KShortestPathsST.Builder builder = KShortestPathsST.builder();
104+
builder.setOption("impl", "hershberger-maxel-suri");
105+
builder.setOption("fast-replacement-threshold", 10);
106+
super.benchAlgo(builder.build(), blackhole);
107+
}
108+
}
109+
110+
@BenchmarkMode(Mode.AverageTime)
111+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
112+
@Warmup(iterations = 2, time = 3, timeUnit = TimeUnit.SECONDS)
113+
@Measurement(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS)
114+
@Fork(value = 1, warmups = 0)
115+
@State(Scope.Benchmark)
116+
public static class Undirected extends KShortestPathsSTBench {
117+
118+
@Param({ "|V|=200 |E|=1500 k=5", "|V|=800 |E|=10000 k=15", "|V|=1500 |E|=3000 k=50" })
119+
public String args;
120+
121+
@Setup(Level.Trial)
122+
public void setup() {
123+
super.setup(args, false);
124+
}
125+
126+
@Benchmark
127+
public void Yen(Blackhole blackhole) {
128+
KShortestPathsST.Builder builder = KShortestPathsST.builder();
129+
builder.setOption("impl", "yen");
130+
super.benchAlgo(builder.build(), blackhole);
131+
}
132+
133+
@Benchmark
134+
public void KShortestPathsSTKatohIbarakiMine(Blackhole blackhole) {
135+
KShortestPathsST.Builder builder = KShortestPathsST.builder();
136+
builder.setOption("impl", "katoh-ibaraki-mine");
137+
builder.setOption("fast-replacement-threshold", 10);
138+
super.benchAlgo(builder.build(), blackhole);
139+
}
140+
}
141+
142+
private static class BenchArgs {
143+
final IntGraph g;
144+
final IWeightFunction w;
145+
final int source;
146+
final int target;
147+
final int k;
148+
149+
BenchArgs(IntGraph g, IWeightFunction w, int source, int target, int k) {
150+
this.g = g;
151+
this.w = w;
152+
this.source = source;
153+
this.target = target;
154+
this.k = k;
155+
}
156+
157+
}
158+
}

0 commit comments

Comments
 (0)