4

I am trying to compare get the execution time of a Cypher query from python, i.e. the time required to compute on the neo4j-server (not including the time required output the result). Right now I am using the following code:

from neo4j.v1 import 
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', '1234'))

n_repeats = 3
cypher = "MATCH (a) -[:{}*]- (b) WHERE ID(a) < ID(b) RETURN DISTINCT a, b".format(graphname + '_edges')

with driver.session() as session:
    total_time = 0
    for _ in range(n_repeats):
        with session.begin_transaction() as tx:
            start = time.time()
            tx.run(cypher)
            total_time += time.time() - start

avg_time = total_time*1000 / n_repeats
print('Average execution time:', avg_time, 'ms')

Is there a better way to time the execution time of a cypher query? In postgresql for instance there is the EXPLAIN ANALYZE statement, which also provides the time required to execute a SQL query. In Cypher there are EXPLAIN and PROFILE statements, but both seemingly don't return specific times.

I am using neo4j-driver to connect to neo4j right now, but I would be willing to switch to another library.

2 Answers2

6

In fact, the time taken is available in all results without profiling. They are in the summary of the result, and the execution time is split into the time until any of the results stream is available and the time until the entire results stream has been consumed by the server.

They can be added together to get the total execution time of the query, expressed in milliseconds:

result = tx.run(query, params)
avail = result.summary().result_available_after
cons = result.summary().result_consumed_after
total_time = avail + cons
Rebecca Nelson
  • 1,286
  • 2
  • 9
  • 20
  • Thank you, that's it! I was looking for something like this in the API documentation, but could not find it. – J. Gambolputty Apr 16 '18 at 21:12
  • @rebecca do you happen to know the answer to this one maybe? https://stackoverflow.com/questions/69746699/neo4j-python-driver-is-it-possible-to-run-explain Thank you! – Tim Oct 27 '21 at 23:49
0

Neo4j Python Driver 1.7

from neo4j.v1 import 
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

n_repeats = 3
cypher = "MATCH (a) -[:{}*]- (b) WHERE ID(a) < ID(b) RETURN DISTINCT a, b".format(graphname + '_edges')

with driver.session() as session:
    total_time = 0
    for _ in range(n_repeats):
        with session.begin_transaction() as tx:
            start = time.time()
            result = tx.run(cypher)
            records = list(result)  # consume the records
            tx.commit()
            total_time += time.time() - start

avg_time = total_time*1000 / n_repeats
print('Average execution time:', avg_time, 'ms')
The Demz
  • 7,066
  • 5
  • 39
  • 43