User Tools

Site Tools


technology:databases:neo4j:experiment_2

Experiment #2

Now that I got my feet wet with Neo4J, I wanted to test it out performance-wise. I know Neo4J has a bulk load utility, but I need to know the limits of doing things myself. Plus, its a learning exercise when you do things yourself. :-)

Here is the code:

  1: /*Neo4J Performance Exercise
  2:  * Chris Freyer
  3:  * Chris@TheFreyers.net
  4:  * July 18,2010
  5:  */
  6: package com.freyer.neo4j.test;
  7:
  8: import java.util.Calendar;
  9: import org.neo4j.graphdb.Direction;
 10: import org.neo4j.graphdb.GraphDatabaseService;
 11: import org.neo4j.graphdb.Node;
 12: import org.neo4j.graphdb.Relationship;
 13: import org.neo4j.graphdb.RelationshipType;
 14: import org.neo4j.graphdb.Transaction;
 15: import org.neo4j.index.IndexService;
 16: import org.neo4j.index.lucene.LuceneIndexService;
 17: import org.neo4j.kernel.EmbeddedGraphDatabase;
 18:
 19: public class Neo4JPerformanceExercise {
 20:
 21:     //static constants
 22:     private static final String DB_PATH = "neo4j-store";
 23:     private static final String USERNAME_KEY = "username";
 24:     private static final String ADDRESS_KEY = "address";
 25:     private static final String CITY_KEY = "city";
 26:     private static final String STATE_KEY = "state";
 27:     private static final String ZIP_KEY = "zip";
 28:
 29:     //class variables
 30:     private static GraphDatabaseService graphDb;
 31:     private static IndexService indexService;
 32:     private static Node usersReferenceNode;
 33:     private static long numUsers = 50000L;
 34:     private static long numQueries = 100000L;
 35:     private static Transaction tx;
 36:
 37:     private static void addUsers() {
 38:         // ADD USERS & INDEX THEM
 39:         try {
 40:             tx = graphDb.beginTx();
 41:             for (long id = 0; id < numUsers; id++) {
 42:                 Node userNode = createAndIndexUser(idToUserName(id));
 43:                 usersReferenceNode.createRelationshipTo(userNode, RelTypes.USER);
 44:                 //use periodic commits to conserve RAM
 45:                 if (id % 5000 == 0) {
 46:                     output("Created " + id + " users so far...");
 47:                     tx.success();
 48:                     tx.finish();
 49:                     tx = graphDb.beginTx();
 50:                 }
 51:             }
 52:             tx.success();
 53:         } catch (Exception e) {
 54:             output("Exception creating users:\n" + e.getMessage());
 55:             tx.failure();
 56:             System.exit(2);
 57:         } finally {
 58:             tx.finish();
 59:         }
 60:         output("" + numUsers + " Users created");
 61:     }
 62:
 63:     private static void deleteUsers() {
 64:         // DELETE USERS & REMOVE FROM INDEX
 65:         output("Removing users...");
 66:         try {
 67:             tx = graphDb.beginTx();
 68:             int i = 0;
 69:             for (Relationship relationship : usersReferenceNode.getRelationships(RelTypes.USER, Direction.OUTGOING)) {
 70:                 Node user = relationship.getEndNode();
 71:                 indexService.removeIndex(user, USERNAME_KEY, user.getProperty(USERNAME_KEY));
 72:                 user.delete();
 73:                 relationship.delete();
 74:                 //use periodic commits to conserve RAM
 75:                 if (++i % 5000 == 0) {
 76:                     output("Deleted " + i + "...");
 77:                     tx.success();
 78:                     tx.finish();
 79:                     tx = graphDb.beginTx();
 80:                 }
 81:             }
 82:             tx.success();
 83:         } catch (Exception e) {
 84:             output("Exception deleting users:\n" + e.getMessage());
 85:             tx.failure();
 86:             System.exit(4);
 87:         } finally {
 88:             tx.finish();
 89:         }
 90:         output("done.");
 91:     }
 92:
 93:     private static void runQueries() {
 94:         // Query many times...
 95:         output("Querying users...");
 96:         try {
 97:             tx = graphDb.beginTx();
 98:             for (int i = 0; i < numQueries; i++) {
 99:                 long idToFind = (long) (Math.random() * (numUsers - 1));
100:                 Node foundUser = indexService.getSingleNode(USERNAME_KEY, idToUserName(idToFind));
101:                 if (i % 10000 == 0) {
102:                     output("Queried " + i + " times...");
103: //                    tx.success();
104:   //                  tx.finish();
105:     //                tx = graphDb.beginTx();
106:                 }
107:             }
108:             tx.success();
109:         } catch (Exception e) {
110:             output("Exception querying users:\n" + e.getMessage());
111:             tx.failure();
112:             System.exit(3);
113:         } finally {
114:             tx.finish();
115:         }
116:         output("done.");
117:     }
118:
119:     private static void setup() {
120:         // Startup with a reference node...
121:
122:         graphDb = new EmbeddedGraphDatabase(DB_PATH);
123:         indexService = new LuceneIndexService(graphDb);
124:         registerShutdownHook();
125:
126:         try {
127:             tx = graphDb.beginTx();
128:             usersReferenceNode = graphDb.createNode();
129:             graphDb.getReferenceNode().createRelationshipTo(usersReferenceNode, RelTypes.USERS_REFERENCE);
130:         } catch (Exception e) {
131:             output("Exception setting up the program...:\n" + e.getMessage() + e.getLocalizedMessage());
132:             tx.failure();
133:             System.exit(1);
134:         } finally {
135:             tx.success();
136:             tx.finish();
137:         }
138:     }
139:
140:     private static void teardown() {
141:         //Delete the main user reference node
142:         tx = graphDb.beginTx();
143:         try {
144:             usersReferenceNode.getSingleRelationship(RelTypes.USERS_REFERENCE, Direction.INCOMING).delete();
145:             usersReferenceNode.delete();
146:             tx.success();
147:         } catch (Exception e) {
148:             output("Exception deleting reference node:\n" + e.getMessage());
149:             tx.failure();
150:             System.exit(5);
151:         } finally {
152:             tx.finish();
153:         }
154:         output("Shutting down database ...");
155:         shutdown();
156:     }
157:
158:     private static void output(String value) {
159:         System.out.println(value);
160:     }
161:
162:     private static enum RelTypes implements RelationshipType {
163:
164:         USERS_REFERENCE, USER,
165:     }
166:
167:     public static void main(final String[] args) {
168:         long starttime = Calendar.getInstance().getTimeInMillis();
169:         setup();
170:         addUsers();
171:         runQueries();
172:         deleteUsers();
173:         teardown();
174:         long endtime = Calendar.getInstance().getTimeInMillis();
175:         output("Runtime:  " + (endtime - starttime) + "ms.");
176:     }
177:
178:     private static void shutdown() {
179:         indexService.shutdown();
180:         graphDb.shutdown();
181:     }
182:
183:     private static String idToUserName(final long id) {
184:         return "user_" + id;
185:     }
186:
187:     private static Node createAndIndexUser(final String username) {
188:         Node node = graphDb.createNode();
189:         node.setProperty(USERNAME_KEY, username);
190:         node.setProperty(ADDRESS_KEY, "1234 South Central Avenue");
191:         node.setProperty(CITY_KEY, "Atlantaville");
192:         node.setProperty(STATE_KEY, "Texiana");
193:         node.setProperty(ZIP_KEY, "12345");
194:
195:         indexService.index(node, USERNAME_KEY, username);
196:         return node;
197:     }
198:
199:     private static void registerShutdownHook() {
200:         Runtime.getRuntime().addShutdownHook(new Thread() {
201:             @Override
202:             public void run() {
203:                 shutdown();
204:             }
205:         });
206:     }
207: }

If you look at the main() method, you'll see I calculate my own runtime. This is because the IDE (Eclipse, Netbeans) normally includes the JVM startup in its runtime total.

Here is the output of running the program:

run:
Created 0 users so far...
Created 5000 users so far...
Created 10000 users so far...
Created 15000 users so far...
Created 20000 users so far...
Created 25000 users so far...
Created 30000 users so far...
Created 35000 users so far...
Created 40000 users so far...
Created 45000 users so far...
50000 Users created
Querying users...
Queried 0 times...
Queried 10000 times...
Queried 20000 times...
Queried 30000 times...
Queried 40000 times...
Queried 50000 times...
Queried 60000 times...
Queried 70000 times...
Queried 80000 times...
Queried 90000 times...
done.
Removing users...
Deleted 5000...
Deleted 10000...
Deleted 15000...
Deleted 20000...
Deleted 25000...
Deleted 30000...
Deleted 35000...
Deleted 40000...
Deleted 45000...
Deleted 50000...
done.
Shutting down database ...
Runtime:  218234ms.

So in 3 minutes 38 seconds, I'm able to:

  • start the database
  • add 50,000 nodes, index them, and tie them to the reference node
  • perform 100,000 random queries against an indexed property
  • remove the same 50,000 nodes and relationships
  • shutdown the database

Overall, I'd say this is pretty good performance, especially considering its running on my laptop (overworked, underpowered).

Points of Interest

  1. The concept of a reference node can be important. If each kind of node (i.e. domain object) in a graph is associated with a unique reference node, a form of data segmentation is created. The performance benefits can be huge.
  2. Relationships have a type and a direction. A type can be a structural relationship as mentioned above, or a domain-related one.
  3. Neo4J does not require a database schema to be prepared in advance. Any object persisted in code is accepted. This has several implications:
    • Prototyping applications is much faster than with relational databases.
    • Responsibility for database consistency is moved from the DBA's to the programmers.
    • Its a very real possibility that old and new versions of objects will exist in the database simultaneously. Your code must be able to handle it.
  4. The IndexService is not a native part of the database. Developers must be certain to add and remove objects from the index during CRUD operations.
/home/cfreyer/public_html/data/pages/technology/databases/neo4j/experiment_2.txt · Last modified: 2010/07/31 21:00 by Chris Freyer