diff options
Diffstat (limited to 'merkle_test.go')
| -rw-r--r-- | merkle_test.go | 71 |
1 files changed, 57 insertions, 14 deletions
diff --git a/merkle_test.go b/merkle_test.go index f3a070e..adc8861 100644 --- a/merkle_test.go +++ b/merkle_test.go @@ -4,9 +4,14 @@ import ( "bytes" "fmt" "math/rand" + "os" + "sort" "testing" + "time" ) +const N = 200000 + func testMerkle(mtype string) bool { m := NewMerkle(mtype, 5, "test.db") root := m.Build() @@ -36,32 +41,70 @@ func TestMerkle(t *testing.T) { } } -func BenchmarkBFSMerkle(b *testing.B) { +func TestBenchmarkBFSMerkle(t *testing.T) { m := NewBFSMerkle(25, "/mnt/data/bfs.db") - root := make([]byte, hashSize) - m.Read(root, 0) - var id int64 var proof [][]byte - for i := 0; i < b.N; i++ { - id = rand.Int63n(m.Size()/2) + m.Size()/2 - proof = m.Proof(id) + start := time.Now() + for i := 0; i < N; i++ { + proof = m.Proof(ids[i]) if len(proof) != int(m.height) { fmt.Println("error") } } + elapsed := time.Since(start) + fmt.Println(elapsed, elapsed.Nanoseconds()/N) } -func BenchmarkPostMerkle(b *testing.B) { +func TestBenchmarkPostMerkle(t *testing.T) { m := NewPostMerkle(25, "/mnt/data/post.db") - root := make([]byte, hashSize) - m.Read(root, 0) - var id int64 var proof [][]byte - for i := 0; i < b.N; i++ { - id = rand.Int63n(m.Size()/2) + m.Size()/2 - proof = m.Proof(id) + start := time.Now() + for i := 0; i < N; i++ { + proof = m.Proof(ids[i]) if len(proof) != int(m.height) { fmt.Println("error") } } + elapsed := time.Since(start) + fmt.Println(elapsed, elapsed.Nanoseconds()/N) +} + +var proofs [][][]byte + +func TestProofsBFSMerkle(t *testing.T) { + m := NewBFSMerkle(25, "/mnt/data/bfs.db") + root := make([]byte, hashSize) + m.Read(root, 0) + start := time.Now() + proofs = m.Proofs(ids) + elapsed := time.Since(start) + fmt.Println(elapsed, elapsed.Nanoseconds()/N) +} + +func TestBatchProofsBFSMerkle(t *testing.T) { + m := NewBFSMerkle(25, "/mnt/data/bfs.db") + root := make([]byte, hashSize) + m.Read(root, 0) + start := time.Now() + proofs = m.BatchProofs(ids) + elapsed := time.Since(start) + fmt.Println(elapsed, elapsed.Nanoseconds()/N) +} + +var ids []int64 + +type Int64Slice []int64 + +func (p Int64Slice) Len() int { return len(p) } +func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] } +func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + +func TestMain(m *testing.M) { + ids = make([]int64, N) + tmp := NewBFSMerkle(25, "/mnt/data/bfs.db") + for i := 0; i < N; i++ { + ids[i] = rand.Int63n(tmp.Size()/2) + tmp.Size()/2 + } + sort.Sort(Int64Slice(ids)) + os.Exit(m.Run()) } |
