github.com/theQRL/go-zond@v0.1.1/trie/triedb/pathdb/difflayer_test.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package pathdb
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/theQRL/go-zond/common"
    24  	"github.com/theQRL/go-zond/core/rawdb"
    25  	"github.com/theQRL/go-zond/trie/testutil"
    26  	"github.com/theQRL/go-zond/trie/trienode"
    27  )
    28  
    29  func emptyLayer() *diskLayer {
    30  	return &diskLayer{
    31  		db:     New(rawdb.NewMemoryDatabase(), nil),
    32  		buffer: newNodeBuffer(DefaultBufferSize, nil, 0),
    33  	}
    34  }
    35  
    36  // goos: darwin
    37  // goarch: arm64
    38  // pkg: github.com/theQRL/go-zond/trie
    39  // BenchmarkSearch128Layers
    40  // BenchmarkSearch128Layers-8   	  243826	      4755 ns/op
    41  func BenchmarkSearch128Layers(b *testing.B) { benchmarkSearch(b, 0, 128) }
    42  
    43  // goos: darwin
    44  // goarch: arm64
    45  // pkg: github.com/theQRL/go-zond/trie
    46  // BenchmarkSearch512Layers
    47  // BenchmarkSearch512Layers-8   	   49686	     24256 ns/op
    48  func BenchmarkSearch512Layers(b *testing.B) { benchmarkSearch(b, 0, 512) }
    49  
    50  // goos: darwin
    51  // goarch: arm64
    52  // pkg: github.com/theQRL/go-zond/trie
    53  // BenchmarkSearch1Layer
    54  // BenchmarkSearch1Layer-8   	14062725	        88.40 ns/op
    55  func BenchmarkSearch1Layer(b *testing.B) { benchmarkSearch(b, 127, 128) }
    56  
    57  func benchmarkSearch(b *testing.B, depth int, total int) {
    58  	var (
    59  		npath []byte
    60  		nhash common.Hash
    61  		nblob []byte
    62  	)
    63  	// First, we set up 128 diff layers, with 3K items each
    64  	fill := func(parent layer, index int) *diffLayer {
    65  		nodes := make(map[common.Hash]map[string]*trienode.Node)
    66  		nodes[common.Hash{}] = make(map[string]*trienode.Node)
    67  		for i := 0; i < 3000; i++ {
    68  			var (
    69  				path = testutil.RandBytes(32)
    70  				node = testutil.RandomNode()
    71  			)
    72  			nodes[common.Hash{}][string(path)] = trienode.New(node.Hash, node.Blob)
    73  			if npath == nil && depth == index {
    74  				npath = common.CopyBytes(path)
    75  				nblob = common.CopyBytes(node.Blob)
    76  				nhash = node.Hash
    77  			}
    78  		}
    79  		return newDiffLayer(parent, common.Hash{}, 0, 0, nodes, nil)
    80  	}
    81  	var layer layer
    82  	layer = emptyLayer()
    83  	for i := 0; i < total; i++ {
    84  		layer = fill(layer, i)
    85  	}
    86  	b.ResetTimer()
    87  
    88  	var (
    89  		have []byte
    90  		err  error
    91  	)
    92  	for i := 0; i < b.N; i++ {
    93  		have, err = layer.Node(common.Hash{}, npath, nhash)
    94  		if err != nil {
    95  			b.Fatal(err)
    96  		}
    97  	}
    98  	if !bytes.Equal(have, nblob) {
    99  		b.Fatalf("have %x want %x", have, nblob)
   100  	}
   101  }
   102  
   103  // goos: darwin
   104  // goarch: arm64
   105  // pkg: github.com/theQRL/go-zond/trie
   106  // BenchmarkPersist
   107  // BenchmarkPersist-8   	      10	 111252975 ns/op
   108  func BenchmarkPersist(b *testing.B) {
   109  	// First, we set up 128 diff layers, with 3K items each
   110  	fill := func(parent layer) *diffLayer {
   111  		nodes := make(map[common.Hash]map[string]*trienode.Node)
   112  		nodes[common.Hash{}] = make(map[string]*trienode.Node)
   113  		for i := 0; i < 3000; i++ {
   114  			var (
   115  				path = testutil.RandBytes(32)
   116  				node = testutil.RandomNode()
   117  			)
   118  			nodes[common.Hash{}][string(path)] = trienode.New(node.Hash, node.Blob)
   119  		}
   120  		return newDiffLayer(parent, common.Hash{}, 0, 0, nodes, nil)
   121  	}
   122  	for i := 0; i < b.N; i++ {
   123  		b.StopTimer()
   124  		var layer layer
   125  		layer = emptyLayer()
   126  		for i := 1; i < 128; i++ {
   127  			layer = fill(layer)
   128  		}
   129  		b.StartTimer()
   130  
   131  		dl, ok := layer.(*diffLayer)
   132  		if !ok {
   133  			break
   134  		}
   135  		dl.persist(false)
   136  	}
   137  }
   138  
   139  // BenchmarkJournal benchmarks the performance for journaling the layers.
   140  //
   141  // BenchmarkJournal
   142  // BenchmarkJournal-8   	      10	 110969279 ns/op
   143  func BenchmarkJournal(b *testing.B) {
   144  	b.SkipNow()
   145  
   146  	// First, we set up 128 diff layers, with 3K items each
   147  	fill := func(parent layer) *diffLayer {
   148  		nodes := make(map[common.Hash]map[string]*trienode.Node)
   149  		nodes[common.Hash{}] = make(map[string]*trienode.Node)
   150  		for i := 0; i < 3000; i++ {
   151  			var (
   152  				path = testutil.RandBytes(32)
   153  				node = testutil.RandomNode()
   154  			)
   155  			nodes[common.Hash{}][string(path)] = trienode.New(node.Hash, node.Blob)
   156  		}
   157  		// TODO(rjl493456442) a non-nil state set is expected.
   158  		return newDiffLayer(parent, common.Hash{}, 0, 0, nodes, nil)
   159  	}
   160  	var layer layer
   161  	layer = emptyLayer()
   162  	for i := 0; i < 128; i++ {
   163  		layer = fill(layer)
   164  	}
   165  	b.ResetTimer()
   166  
   167  	for i := 0; i < b.N; i++ {
   168  		layer.journal(new(bytes.Buffer))
   169  	}
   170  }