From 786697f1b730d39e680fd46f5306608a5b445316 Mon Sep 17 00:00:00 2001 From: Jason McVetta Date: Mon, 7 Oct 2013 18:20:38 -0700 Subject: [PATCH] Improved node chain tx benchmark. Issue #22. --- benchmark_test.go | 122 +++++++++++++++++++++++++++------------------- 1 file changed, 73 insertions(+), 49 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index f96d8b6..9cc241e 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -18,6 +18,21 @@ func connectBench(b *testing.B) *Database { return db } +func benchCleanup(b *testing.B, db *Database) { + qs := []*CypherQuery{ + &CypherQuery{ + Statement: `START r=rel(*) DELETE r`, + }, + &CypherQuery{ + Statement: `START n=node(*) DELETE n`, + }, + } + err := db.CypherBatch(qs) + if err != nil { + b.Fatal(err) + } +} + func BenchmarkNodeChain(b *testing.B) { b.StopTimer() db := connectBench(b) @@ -93,65 +108,74 @@ func BenchmarkNodeChainBatch(b *testing.B) { } } -func BenchmarkNodeChainTx(b *testing.B) { +func BenchmarkNodeChainTx10___(b *testing.B) { + nodeChainTx(b, 10) +} + +func BenchmarkNodeChainTx100__(b *testing.B) { + nodeChainTx(b, 100) +} + +func BenchmarkNodeChainTx1000_(b *testing.B) { + nodeChainTx(b, 1000) +} + +// nodeChain benchmarks the creating then querying a node chain. +func nodeChainTx(b *testing.B, chainLength int) { b.StopTimer() db := connectBench(b) + defer benchCleanup(b, db) b.StartTimer() - qs := []*CypherQuery{} - nodes := []int{} - rels := []int{} - cq := CypherQuery{ - Statement: `CREATE (n:Person {name: {i}}) RETURN n`, - Parameters: Props{"i": 0}, - Result: &[]Node{}, - } - qs = append(qs, &cq) - nodes = append(nodes, 0) - for i := 1; i < b.N; i++ { - cq0 := CypherQuery{ + for cnt := 1; cnt < b.N; cnt++ { + qs := []*CypherQuery{} + cq := CypherQuery{ Statement: `CREATE (n:Person {name: {i}}) RETURN n`, - Parameters: Props{"i": i}, + Parameters: Props{"i": 0}, Result: &[]Node{}, } - qs = append(qs, &cq0) - nodes = append(nodes, i) + qs = append(qs, &cq) + for i := 0; i < chainLength; i++ { + cq := CypherQuery{ + Statement: ` + CREATE (a:Person {name: {a_name}})-[r:knows]->(b:Person {name: {b_name}}) + RETURN a.name, type(r), b.name + `, + Parameters: Props{ + "a_name": i, + "b_name": i + 1, + }, + Result: &[]struct { + A int `json:"a.name"` + R string `json:"type(r)"` + B int `json:"b.name"` + }{}, + } + qs = append(qs, &cq) + } cq1 := CypherQuery{ - Statement: `MATCH a:Person, b:Person WHERE a.name = {i} AND b.name = {k} CREATE a-[r:Knows {name: {i}}]->b RETURN id(r)`, - Parameters: Props{"i": i, "k": i - 1}, - Result: &[]Relationship{}, + Statement: ` + MATCH (a:Person)-[r]->(b:Person) + RETURN a.name, type(r), b.name + `, + Result: &[]struct { + A int `json:"a.name"` + R string `json:"type(r)"` + B int `json:"b.name"` + }{}, } qs = append(qs, &cq1) - rels = append(rels, i) - } - tx, err := db.Begin(qs) - if err != nil { - b.Fatal(err) - } - err = tx.Commit() - if err != nil { - b.Fatal(err) - } - b.StopTimer() - // - // Cleanup - // - qs = []*CypherQuery{} - for _, r := range rels { - cq := CypherQuery{ - Statement: `MATCH ()-[r:Knows]->() WHERE r.name = {i} DELETE r`, - Parameters: Props{"i": r}, + tx, err := db.Begin(qs) + if err != nil { + tx.Rollback() + logPretty(err) + b.Fatal(err) } - qs = append(qs, &cq) - } - for _, n := range nodes { - cq := CypherQuery{ - Statement: `MATCH n:Person WHERE n.name = {i} DELETE n`, - Parameters: Props{"i": n}, + err = tx.Commit() + if err != nil { + b.Fatal(err) } - qs = append(qs, &cq) - } - err = db.CypherBatch(qs) - if err != nil { - b.Fatal(err) + b.StopTimer() + benchCleanup(b, db) + b.StartTimer() } }