@@ -18,11 +18,15 @@ package trie
1818
1919import (
2020 "bytes"
21+ "encoding/binary"
2122 "fmt"
23+ "io/ioutil"
24+ "os"
2225 "testing"
2326
2427 "github.com/ethereum/go-ethereum/common"
2528 "github.com/ethereum/go-ethereum/crypto"
29+ "github.com/ethereum/go-ethereum/ethdb"
2630)
2731
2832type Db map [string ][]byte
@@ -100,7 +104,6 @@ func TestGet(t *testing.T) {
100104
101105func TestDelete (t * testing.T ) {
102106 trie := NewEmpty ()
103-
104107 vals := []struct { k , v string }{
105108 {"do" , "verb" },
106109 {"ether" , "wookiedoo" },
@@ -254,39 +257,6 @@ func TestOutput(t *testing.T) {
254257 fmt .Println (trie2 .root )
255258}
256259
257- func BenchmarkGets (b * testing.B ) {
258- trie := NewEmpty ()
259- vals := []struct { k , v string }{
260- {"do" , "verb" },
261- {"ether" , "wookiedoo" },
262- {"horse" , "stallion" },
263- {"shaman" , "horse" },
264- {"doge" , "coin" },
265- {"ether" , "" },
266- {"dog" , "puppy" },
267- {"shaman" , "" },
268- {"somethingveryoddindeedthis is" , "myothernodedata" },
269- }
270- for _ , val := range vals {
271- trie .UpdateString (val .k , val .v )
272- }
273-
274- b .ResetTimer ()
275- for i := 0 ; i < b .N ; i ++ {
276- trie .Get ([]byte ("horse" ))
277- }
278- }
279-
280- func BenchmarkUpdate (b * testing.B ) {
281- trie := NewEmpty ()
282-
283- b .ResetTimer ()
284- for i := 0 ; i < b .N ; i ++ {
285- trie .UpdateString (fmt .Sprintf ("aaaaaaaaa%d" , i ), "value" )
286- }
287- trie .Hash ()
288- }
289-
290260type kv struct {
291261 k , v []byte
292262 t bool
@@ -352,3 +322,71 @@ func TestSecureDelete(t *testing.T) {
352322 t .Errorf ("expected %x got %x" , exp , hash )
353323 }
354324}
325+
326+ func BenchmarkGet (b * testing.B ) { benchGet (b , false ) }
327+ func BenchmarkGetDB (b * testing.B ) { benchGet (b , true ) }
328+ func BenchmarkUpdateBE (b * testing.B ) { benchUpdate (b , binary .BigEndian ) }
329+ func BenchmarkUpdateLE (b * testing.B ) { benchUpdate (b , binary .LittleEndian ) }
330+ func BenchmarkHashBE (b * testing.B ) { benchHash (b , binary .BigEndian ) }
331+ func BenchmarkHashLE (b * testing.B ) { benchHash (b , binary .LittleEndian ) }
332+
333+ const benchElemCount = 20000
334+
335+ func benchGet (b * testing.B , commit bool ) {
336+ trie := New (nil , nil )
337+ if commit {
338+ dir , tmpdb := tempDB ()
339+ defer os .RemoveAll (dir )
340+ trie = New (nil , tmpdb )
341+ }
342+ k := make ([]byte , 32 )
343+ for i := 0 ; i < benchElemCount ; i ++ {
344+ binary .LittleEndian .PutUint64 (k , uint64 (i ))
345+ trie .Update (k , k )
346+ }
347+ binary .LittleEndian .PutUint64 (k , benchElemCount / 2 )
348+ if commit {
349+ trie .Commit ()
350+ }
351+
352+ b .ResetTimer ()
353+ for i := 0 ; i < b .N ; i ++ {
354+ trie .Get (k )
355+ }
356+ }
357+
358+ func benchUpdate (b * testing.B , e binary.ByteOrder ) * Trie {
359+ trie := NewEmpty ()
360+ k := make ([]byte , 32 )
361+ for i := 0 ; i < b .N ; i ++ {
362+ e .PutUint64 (k , uint64 (i ))
363+ trie .Update (k , k )
364+ }
365+ return trie
366+ }
367+
368+ func benchHash (b * testing.B , e binary.ByteOrder ) {
369+ trie := NewEmpty ()
370+ k := make ([]byte , 32 )
371+ for i := 0 ; i < benchElemCount ; i ++ {
372+ e .PutUint64 (k , uint64 (i ))
373+ trie .Update (k , k )
374+ }
375+
376+ b .ResetTimer ()
377+ for i := 0 ; i < b .N ; i ++ {
378+ trie .Hash ()
379+ }
380+ }
381+
382+ func tempDB () (string , Backend ) {
383+ dir , err := ioutil .TempDir ("" , "trie-bench" )
384+ if err != nil {
385+ panic (fmt .Sprintf ("can't create temporary directory: %v" , err ))
386+ }
387+ db , err := ethdb .NewLDBDatabase (dir , 300 * 1024 )
388+ if err != nil {
389+ panic (fmt .Sprintf ("can't create temporary database: %v" , err ))
390+ }
391+ return dir , db
392+ }
0 commit comments