github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/syndtr/goleveldb/leveldb/memdb/bench_test.go (about) 1 // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com> 2 // All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 package memdb 8 9 import ( 10 "encoding/binary" 11 "math/rand" 12 "testing" 13 14 "github.com/insionng/yougam/libraries/syndtr/goleveldb/leveldb/comparer" 15 ) 16 17 func BenchmarkPut(b *testing.B) { 18 buf := make([][4]byte, b.N) 19 for i := range buf { 20 binary.LittleEndian.PutUint32(buf[i][:], uint32(i)) 21 } 22 23 b.ResetTimer() 24 p := New(comparer.DefaultComparer, 0) 25 for i := range buf { 26 p.Put(buf[i][:], nil) 27 } 28 } 29 30 func BenchmarkPutRandom(b *testing.B) { 31 buf := make([][4]byte, b.N) 32 for i := range buf { 33 binary.LittleEndian.PutUint32(buf[i][:], uint32(rand.Int())) 34 } 35 36 b.ResetTimer() 37 p := New(comparer.DefaultComparer, 0) 38 for i := range buf { 39 p.Put(buf[i][:], nil) 40 } 41 } 42 43 func BenchmarkGet(b *testing.B) { 44 buf := make([][4]byte, b.N) 45 for i := range buf { 46 binary.LittleEndian.PutUint32(buf[i][:], uint32(i)) 47 } 48 49 p := New(comparer.DefaultComparer, 0) 50 for i := range buf { 51 p.Put(buf[i][:], nil) 52 } 53 54 b.ResetTimer() 55 for i := range buf { 56 p.Get(buf[i][:]) 57 } 58 } 59 60 func BenchmarkGetRandom(b *testing.B) { 61 buf := make([][4]byte, b.N) 62 for i := range buf { 63 binary.LittleEndian.PutUint32(buf[i][:], uint32(i)) 64 } 65 66 p := New(comparer.DefaultComparer, 0) 67 for i := range buf { 68 p.Put(buf[i][:], nil) 69 } 70 71 b.ResetTimer() 72 for i := 0; i < b.N; i++ { 73 p.Get(buf[rand.Int()%b.N][:]) 74 } 75 }