github.com/ethereum/go-ethereum@v1.14.3/ethdb/memorydb/memorydb_test.go (about)

     1  // Copyright 2018 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 memorydb
    18  
    19  import (
    20  	"encoding/binary"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/ethdb"
    24  	"github.com/ethereum/go-ethereum/ethdb/dbtest"
    25  )
    26  
    27  func TestMemoryDB(t *testing.T) {
    28  	t.Run("DatabaseSuite", func(t *testing.T) {
    29  		dbtest.TestDatabaseSuite(t, func() ethdb.KeyValueStore {
    30  			return New()
    31  		})
    32  	})
    33  }
    34  
    35  // BenchmarkBatchAllocs measures the time/allocs for storing 120 kB of data
    36  func BenchmarkBatchAllocs(b *testing.B) {
    37  	b.ReportAllocs()
    38  	var key = make([]byte, 20)
    39  	var val = make([]byte, 100)
    40  	// 120 * 1_000 -> 120_000 == 120kB
    41  	for i := 0; i < b.N; i++ {
    42  		batch := New().NewBatch()
    43  		for j := uint64(0); j < 1000; j++ {
    44  			binary.BigEndian.PutUint64(key, j)
    45  			binary.BigEndian.PutUint64(val, j)
    46  			batch.Put(key, val)
    47  		}
    48  		batch.Write()
    49  	}
    50  }