55import apoc .export .csv .ImportCsv ;
66import apoc .help .Help ;
77import apoc .periodic .Periodic ;
8- import org .iguana .grammar .Grammar ;
8+ import iguana .utils .input .GraphInput ;
9+ import iguana .utils .input .Neo4jBenchmarkInput ;
910import org .neo4j .configuration .GraphDatabaseSettings ;
1011import org .neo4j .configuration .connectors .BoltConnector ;
1112import org .neo4j .dbms .api .DatabaseManagementService ;
1920import org .neo4j .kernel .api .procedure .GlobalProcedures ;
2021import org .neo4j .kernel .internal .GraphDatabaseAPI ;
2122
22- import java .io .File ;
23- import java .io . FileNotFoundException ;
24- import java .io . IOException ;
23+ import java .io .* ;
24+ import java .nio . file . Files ;
25+ import java .nio . file . Paths ;
2526import java .util .List ;
2627import java .util .function .BiFunction ;
28+ import java .util .stream .Stream ;
2729
2830import static java .util .Arrays .asList ;
2931import static org .neo4j .configuration .GraphDatabaseSettings .DEFAULT_DATABASE_NAME ;
3032
31- public abstract class Neo4jBenchmark {
32-
33- protected static String RESULTS_DIR = "results" ;
34-
35- // args[0] - benchmarkType
36- // args[1] - nodeNumber
37- // args[2] - number of warm up iteration
38- // args[3] - total number of iterations
39- // args[4] - path to dataset (graph)
40- // args[5] - path to grammar
41- // args[6] - dataset name = name of file with results
42- public static void main (String [] args ) throws IOException {
43- Neo4jBenchmark benchmark = benchmarkByType (args [0 ], Integer .parseInt (args [1 ]));
44- benchmark .loadGraph (args [4 ]);
45- benchmark .loadGrammar (args [5 ]);
46- benchmark .benchmark (parseSettings (new String []{args [2 ], args [3 ], args [6 ]}));
47- benchmark .removeData ();
48- benchmark .managementService .shutdown ();
49- }
50- private static Neo4jBenchmark benchmarkByType (String type , int nodesCount ) {
51- return switch (type ) {
52- // AP - All Pairs; MS - Multiple Sources
53- case "REACHABILITY_AP" -> new ReachabilityBenchmark (nodesCount );
54- // case "REACHABILITY_MS" -> new ReachabilityBenchmark();
55- case "PATH_AP" -> new PathBenchmark (nodesCount );
56- // case "PATH_MS" -> new PathBenchmark();
57- default -> throw new IllegalArgumentException ("Illegal benchmark type" );
58- };
59- }
33+ public class BenchmarkGraphNeo4j extends BenchmarkGraphStorage {
6034
61- /**
62- * @param args array contains string params:
63- * args[0] - warmUpIterations
64- * args[1] - measurementsIterations
65- * args[2] - datasetName
66- * @return {@link BenchmarkSettings} instance
67- */
68- private static BenchmarkSettings parseSettings (String [] args ) {
69- return new BenchmarkSettings (
70- Integer .parseInt (args [0 ]),
71- Integer .parseInt (args [1 ]),
72- args [2 ]);
73- }
35+ private int nodesCount ;
36+
37+ BenchmarkGraphNeo4j () {
7438
75- Neo4jBenchmark (int nodesCount ) {
76- this .nodesCount = nodesCount ;
7739 }
7840
7941 private final BiFunction <Relationship , Direction , String > relationship2Label =
@@ -86,9 +48,8 @@ private static BenchmarkSettings parseSettings(String[] args) {
8648 private final File databaseDirectory = new File ("target/neo4j-hello-db" );
8749 private GraphDatabaseService graphDb ;
8850 private DatabaseManagementService managementService ;
89- private Grammar grammar ;
90- private final int nodesCount ;
91- public void registerProcedure (GraphDatabaseService graphDb , List <Class <?>> procedures ) {
51+
52+ private void registerProcedure (GraphDatabaseService graphDb , List <Class <?>> procedures ) {
9253 GlobalProcedures globalProcedures = ((GraphDatabaseAPI ) graphDb ).getDependencyResolver ().resolveDependency (GlobalProcedures .class );
9354 for (Class <?> procedure : procedures ) {
9455 try {
@@ -101,7 +62,15 @@ public void registerProcedure(GraphDatabaseService graphDb, List<Class<?>> proce
10162 }
10263 }
10364
104- private void loadGraph (String pathToDataset ) throws IOException {
65+ private Neo4jBenchmarkInput graphInput = null ;
66+
67+ @ Override
68+ public void loadGraph (String path ) throws IOException {
69+
70+ try (var lines = Files .lines (Paths .get (path + File .separatorChar + "nodes.csv" ))) {
71+ nodesCount = (int ) lines .count ();
72+ }
73+
10574 FileUtils .deleteRecursively (databaseDirectory );
10675 managementService =
10776 new DatabaseManagementServiceBuilder (databaseDirectory )
@@ -124,50 +93,45 @@ private void loadGraph(String pathToDataset) throws IOException {
12493 Periodic .class
12594 ));
12695
96+ String neo4jPath = new File (path ).getAbsolutePath ();
12797 try (Transaction tx = graphDb .beginTx ()) {
12898 tx .execute ("CALL apoc.import.csv(" +
129- "[{fileName: 'FILE:///" + pathToDataset + "nodes.csv', labels: ['Node']}], " +
130- "[{fileName: 'FILE:///" + pathToDataset + "edges.csv'}], " +
99+ "[{fileName: 'FILE:///" + neo4jPath + File . separator + "nodes.csv', labels: ['Node']}], " +
100+ "[{fileName: 'FILE:///" + neo4jPath + File . separator + "edges.csv'}], " +
131101 "{delimiter: ' ', stringIds: false}" +
132102 ")" );
133103 tx .commit ();
134104 }
135105 System .out .println ("Graph loaded" );
136106 }
137107
138- private void loadGrammar (String pathToGrammar ) {
139- try {
140- grammar = Grammar .load (pathToGrammar , "json" );
141- } catch (FileNotFoundException e ) {
142- throw new RuntimeException ("No grammar file is present" );
143- }
108+ @ Override
109+ public GraphInput getGraphInput (Stream <Integer > startVertices ) {
110+ graphInput = new Neo4jBenchmarkInput (graphDb , relationship2Label , startVertices , nodesCount );
111+ return graphInput ;
144112 }
145113
146- protected abstract void benchmark (BenchmarkSettings settings ) throws IOException ;
114+ @ Override
115+ protected void onIterationFinish () {
116+ graphInput .close ();
117+ graphInput = null ;
118+ }
147119
148- private void removeData () {
120+ @ Override
121+ protected void close () {
149122 try (Transaction tx = graphDb .beginTx ()) {
150123 tx .getAllNodes ().forEach (node -> {
151124 node .getRelationships ().forEach (Relationship ::delete );
152125 node .delete ();
153126 });
154127 tx .commit ();
155128 }
129+ managementService .shutdown ();
156130 }
157131
158- protected BiFunction <Relationship , Direction , String > getRelationship2Label () {
159- return relationship2Label ;
160- }
161-
162- protected GraphDatabaseService getGraphDb () {
163- return graphDb ;
164- }
165-
166- protected Grammar getGrammar () {
167- return grammar ;
132+ @ Override
133+ public String toString () {
134+ return "NEO4J" ;
168135 }
169136
170- protected int getNodesCount () {
171- return nodesCount ;
172- }
173137}
0 commit comments