Skip to content

Instantly share code, notes, and snippets.

@millar
Last active December 20, 2015 23:08
Show Gist options
  • Save millar/6209784 to your computer and use it in GitHub Desktop.
Save millar/6209784 to your computer and use it in GitHub Desktop.
Test used to compare performance of py2neo 1.6 vs 1.5
# coding: utf8
import logging
import unittest
import random
from py2neo import cypher, neo4j
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
# Uncomment for py2neo debug
# neologger = logging.getLogger('py2neo')
# neologger.setLevel(logging.DEBUG)
# neologger.addHandler(logging.StreamHandler())
class SpeedTest(unittest.TestCase):
NUM_ITERATIONS = 20
BATCH_ITERATIONS = 30
NUM_RELATIVES = 3
def test_speed(self):
graph_db = neo4j.GraphDatabaseService()
for _ in range(0, self.NUM_ITERATIONS):
logger.info("Performed %i of %i iterations" % (_, self.NUM_ITERATIONS))
batch = neo4j.WriteBatch(graph_db)
for i in range(0, self.BATCH_ITERATIONS):
# Write
batch.create(neo4j.Node.abstract(**{'x': 'x', 'y': random.randint(1, 5)}))
for j in range(0, self.NUM_RELATIVES):
batch.create(neo4j.Node.abstract(**{'x': 'x', 'y': random.randint(1, 5)}))
batch.create(neo4j.Relationship.abstract(i * (1 + self.NUM_RELATIVES * 2), "test_rel1", (i * (1 + self.NUM_RELATIVES * 2)) + 1 + (j * 2)))
results = batch.submit()
for i in range(0, self.BATCH_ITERATIONS):
# Read
returned, metadata = cypher.execute(graph_db, "START o=node({origin}) MATCH o-[]-(p) WITH p RETURN p", {'origin': results[i * (1 + self.NUM_RELATIVES * 2)]._id})
for i in range(0, self.BATCH_ITERATIONS):
# Delete
cypher.execute(graph_db, "START o=node({origin}) MATCH o-[r]-(p) DELETE r, p", {'origin': results[i * (1 + self.NUM_RELATIVES * 2)]._id})
cypher.execute(graph_db, "START o=node({origin}) DELETE o", {'origin': results[i * (1 + self.NUM_RELATIVES * 2)]._id})
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment