diff --git a/arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/AirbnbBenchmark.java b/arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/AirbnbBenchmark.java index 5b11c17..933c8f5 100644 --- a/arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/AirbnbBenchmark.java +++ b/arebac-neo4j/src/test/java/io/github/danthe1st/arebac/neo4j/tests/airbnb/AirbnbBenchmark.java @@ -2,10 +2,12 @@ import java.io.IOException; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern; import io.github.danthe1st.arebac.gpeval.GPEval; @@ -27,10 +29,19 @@ public class AirbnbBenchmark { @Benchmark - public void scenario1GetReviewsFromHostGPEval(AirbnbState state, Blackhole bh) { + public void scenario1GetReviewsFromHostGPEvalWithWeaving(AirbnbState state, Blackhole bh) { try(Transaction tx = state.database.beginTx()){ Neo4jDB db = new Neo4jDB(tx); - Set> result = GPEval.evaluate(db, state.nextHostPattern()); + Set> result = GPEval.evaluate(db, state.hostPatternInfo.computeNextPattern()); + result.forEach(bh::consume); + } + } + + @Benchmark + public void scenario1GetReviewsFromHostGPEvalWithoutWeaving(AirbnbState state, Blackhole bh) { + try(Transaction tx = state.database.beginTx()){ + Neo4jDB db = new Neo4jDB(tx); + Set> result = GPEval.evaluate(db, state.hostPatternInfo.nextLoadedPattern()); result.forEach(bh::consume); } } @@ -42,17 +53,26 @@ public void scenario1GetReviewsFromHostNeo4J(AirbnbState state, Blackhole bh) { MATCH (r1:Reviewer)-[: WROTE]->(r2:Review) -[: REVIEWS]->(l: Listing)<-[HOSTS]-(h: Host) WHERE h.host_id="$hostId" RETURN r1,r2 - """, Map.of("hostId", state.nextHostId())); + """, Map.of("hostId", state.hostPatternInfo.nextId())); result.forEachRemaining(bh::consume); } } @Benchmark - public void scenario1GetReviewsFromReviewerGPEval(AirbnbState state, Blackhole bh) { + public void scenario1GetReviewsFromReviewerGPEvalWithWeaving(AirbnbState state, Blackhole bh) { try(Transaction tx = state.database.beginTx()){ Neo4jDB db = new Neo4jDB(tx); - Set> result = GPEval.evaluate(db, state.nextReviewerPattern()); + Set> result = GPEval.evaluate(db, state.reviewerPatternInfo.computeNextPattern()); + result.forEach(bh::consume); + } + } + + @Benchmark + public void scenario1GetReviewsFromReviewerGPEvalWithoutWeaving(AirbnbState state, Blackhole bh) { + try(Transaction tx = state.database.beginTx()){ + Neo4jDB db = new Neo4jDB(tx); + Set> result = GPEval.evaluate(db, state.reviewerPatternInfo.nextLoadedPattern()); result.forEach(bh::consume); } } @@ -64,7 +84,7 @@ public void scenario1GetReviewsFromReviewerNeo4J(AirbnbState state, Blackhole bh MATCH (r:Reviewer) -[: WROTE]-> (r2: Review) WHERE r.reviewer_id="REVIEWER_ID" RETURN r - """, Map.of("hostId", state.nextReviewerId())); + """, Map.of("hostId", state.reviewerPatternInfo.nextId())); result.forEachRemaining(bh::consume); } @@ -73,38 +93,61 @@ public void scenario1GetReviewsFromReviewerNeo4J(AirbnbState state, Blackhole bh @State(Scope.Thread) public static class AirbnbState { private final GraphDatabaseService database; - private final List hostIds = List.of("131304391", "155715332", "404944621"); - private int currentHostIndex = 0; - private final List reviewerIds = List.of("272671293", "227163707", "268281268", "31292360"); - private int currentReviewerIndex = 0; + private final PatternInfo hostPatternInfo; + private final PatternInfo reviewerPatternInfo; public AirbnbState() { try{ database = AirbnbSetup.getDatabase(); + hostPatternInfo = new PatternInfo(List.of("131304391", "155715332", "404944621"), Scenario1Test::createAuthorizedCetAllReviewsFromHostGraphPattern); + reviewerPatternInfo = new PatternInfo(List.of("272671293", "227163707", "268281268", "31292360"), Scenario1Test::createAuthorizedGetAllReviewsFromReviewerGraphPattern); }catch(IOException | IncorrectFormat | InterruptedException | URISyntaxException e){ throw new RuntimeException("cannot initialize benchmark state", e); } } - public GraphPattern nextHostPattern() { - return Scenario1Test.createAuthorizedCetAllReviewsFromHostGraphPattern(nextHostId()); + public PatternInfo getHostPatternInfo() { + return hostPatternInfo; } - public String nextHostId() { - int index = currentHostIndex; - currentHostIndex = (currentHostIndex + 1) % hostIds.size(); - return hostIds.get(index); + public PatternInfo getReviewerPatternInfo() { + return reviewerPatternInfo; } - public GraphPattern nextReviewerPattern() { - return Scenario1Test.createAuthorizedGetAllReviewsFromReviewerGraphPattern(nextReviewerId()); + } + + private static class PatternInfo { + private final List ids; + private final Function patternGenerator; + private final List patterns; + private int currentIndex = 0; + + public PatternInfo(List ids, Function patternGenerator) { + this.ids = ids; + this.patternGenerator = patternGenerator; + List patterns = new ArrayList<>(); + for(String id : ids){ + patterns.add(patternGenerator.apply(id)); + } + this.patterns = List.copyOf(patterns); } - public String nextReviewerId() { - int index = currentReviewerIndex; - currentReviewerIndex = (currentReviewerIndex + 1) % reviewerIds.size(); - return reviewerIds.get(index); + public GraphPattern nextLoadedPattern() { + return patterns.get(nextIndex()); } + public GraphPattern computeNextPattern() { + return patternGenerator.apply(nextId()); + } + + public String nextId() { + return ids.get(nextIndex()); + } + + private int nextIndex() { + int index = currentIndex; + currentIndex = (currentIndex + 1) % ids.size(); + return index; + } } }