From 850144f3e74301edaba58919c23c606fdf3fd164 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 21 May 2014 19:06:43 -0700 Subject: [PATCH] updated benchmark --- merkle/iavl_test.go | 39 ++++++++++++++++----------------------- merkle/types.go | 2 +- merkle/util.go | 6 +++--- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/merkle/iavl_test.go b/merkle/iavl_test.go index 5ca6f7b8b..26d3b8e9c 100644 --- a/merkle/iavl_test.go +++ b/merkle/iavl_test.go @@ -53,12 +53,12 @@ func TestImmutableAvlPutHasGetRemove(t *testing.T) { var val Value var updated bool - ranrec := func() *record { + randomRecord := func() *record { return &record{ randstr(20), randstr(20) } } for i := range records { - r := ranrec() + r := randomRecord() records[i] = r tree, updated = tree.Put(r.key, String("")) if updated { @@ -121,25 +121,21 @@ func BenchmarkImmutableAvlTree(b *testing.B) { value String } - records := make([]*record, 100) - - ranrec := func() *record { - return &record{ randstr(20), randstr(20) } + randomRecord := func() *record { + return &record{ randstr(32), randstr(32) } } - for i := range records { - records[i] = ranrec() + t := NewIAVLTree() + for i:=0; i<1000000; i++ { + r := randomRecord() + t.Put(r.key, r.value) } b.StartTimer() for i := 0; i < b.N; i++ { - t := NewIAVLTree() - for _, r := range records { - t.Put(r.key, r.value) - } - for _, r := range records { - t.Remove(r.key) - } + r := randomRecord() + t.Put(r.key, r.value) + t.Remove(r.key) } } @@ -161,11 +157,9 @@ func TestTraversals(t *testing.T) { } j := 0 - for - tn, next := Iterator(T.Root())(); - next != nil; - tn, next = next () { - if int(tn.Key().(Int)) != data[j] { + itr := Iterator(T.Root()); + for node := itr(); node != nil; node = itr() { + if int(node.Key().(Int)) != data[j] { t.Error("key in wrong spot in-order") } j += 1 @@ -206,9 +200,8 @@ func TestGriffin(t *testing.T) { t.Fatalf("Expected %v new hashes, got %v", hashCount, count) } // nuke hashes and reconstruct hash, ensure it's the same. - var node Node - for itr:=Iterator(n2); itr!=nil; { - node, itr = itr() + itr := Iterator(n2) + for node:=itr(); node!=nil; node = itr() { if node != nil { node.(*IAVLNode).hash = nil } diff --git a/merkle/types.go b/merkle/types.go index bd042c3bd..aa1567fa1 100644 --- a/merkle/types.go +++ b/merkle/types.go @@ -41,7 +41,7 @@ type Node interface { Remove(key Key) (_ *IAVLNode, value Value, err error) } -type NodeIterator func() (node Node, next NodeIterator) +type NodeIterator func() (node Node) func NotFound(key Key) error { return fmt.Errorf("Key was not found.") diff --git a/merkle/util.go b/merkle/util.go index b4871645d..c2076bbfa 100644 --- a/merkle/util.go +++ b/merkle/util.go @@ -8,7 +8,7 @@ func Iterator(node Node) NodeIterator { stack := make([]Node, 0, 10) var cur Node = node var tn_iterator NodeIterator - tn_iterator = func()(tn Node, next NodeIterator) { + tn_iterator = func()(tn Node) { if len(stack) > 0 || cur != nil { for cur != nil { stack = append(stack, cur) @@ -17,9 +17,9 @@ func Iterator(node Node) NodeIterator { stack, cur = pop(stack) tn = cur cur = cur.Right() - return tn, tn_iterator + return tn } else { - return nil, nil + return nil } } return tn_iterator