Skip to content

Commit

Permalink
Improved node chain tx benchmark. Issue #22.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcvetta committed Oct 8, 2013
1 parent 7925375 commit 786697f
Showing 1 changed file with 73 additions and 49 deletions.
122 changes: 73 additions & 49 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
}

0 comments on commit 786697f

Please sign in to comment.