github.com/MaynardMiner/ethereumprogpow@v1.8.23/swarm/storage/memstore_test.go (about)

     1  // Copyright 2016 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 storage
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/ethereumprogpow/ethereumprogpow/swarm/log"
    24  )
    25  
    26  func newTestMemStore() *MemStore {
    27  	storeparams := NewDefaultStoreParams()
    28  	return NewMemStore(storeparams, nil)
    29  }
    30  
    31  func testMemStoreRandom(n int, chunksize int64, t *testing.T) {
    32  	m := newTestMemStore()
    33  	defer m.Close()
    34  	testStoreRandom(m, n, chunksize, t)
    35  }
    36  
    37  func testMemStoreCorrect(n int, chunksize int64, t *testing.T) {
    38  	m := newTestMemStore()
    39  	defer m.Close()
    40  	testStoreCorrect(m, n, chunksize, t)
    41  }
    42  
    43  func TestMemStoreRandom_1(t *testing.T) {
    44  	testMemStoreRandom(1, 0, t)
    45  }
    46  
    47  func TestMemStoreCorrect_1(t *testing.T) {
    48  	testMemStoreCorrect(1, 4104, t)
    49  }
    50  
    51  func TestMemStoreRandom_1k(t *testing.T) {
    52  	testMemStoreRandom(1000, 0, t)
    53  }
    54  
    55  func TestMemStoreCorrect_1k(t *testing.T) {
    56  	testMemStoreCorrect(100, 4096, t)
    57  }
    58  
    59  func TestMemStoreNotFound(t *testing.T) {
    60  	m := newTestMemStore()
    61  	defer m.Close()
    62  
    63  	_, err := m.Get(context.TODO(), ZeroAddr)
    64  	if err != ErrChunkNotFound {
    65  		t.Errorf("Expected ErrChunkNotFound, got %v", err)
    66  	}
    67  }
    68  
    69  func benchmarkMemStorePut(n int, processors int, chunksize int64, b *testing.B) {
    70  	m := newTestMemStore()
    71  	defer m.Close()
    72  	benchmarkStorePut(m, n, chunksize, b)
    73  }
    74  
    75  func benchmarkMemStoreGet(n int, processors int, chunksize int64, b *testing.B) {
    76  	m := newTestMemStore()
    77  	defer m.Close()
    78  	benchmarkStoreGet(m, n, chunksize, b)
    79  }
    80  
    81  func BenchmarkMemStorePut_1_500(b *testing.B) {
    82  	benchmarkMemStorePut(500, 1, 4096, b)
    83  }
    84  
    85  func BenchmarkMemStorePut_8_500(b *testing.B) {
    86  	benchmarkMemStorePut(500, 8, 4096, b)
    87  }
    88  
    89  func BenchmarkMemStoreGet_1_500(b *testing.B) {
    90  	benchmarkMemStoreGet(500, 1, 4096, b)
    91  }
    92  
    93  func BenchmarkMemStoreGet_8_500(b *testing.B) {
    94  	benchmarkMemStoreGet(500, 8, 4096, b)
    95  }
    96  
    97  func TestMemStoreAndLDBStore(t *testing.T) {
    98  	ldb, cleanup := newLDBStore(t)
    99  	ldb.setCapacity(4000)
   100  	defer cleanup()
   101  
   102  	cacheCap := 200
   103  	memStore := NewMemStore(NewStoreParams(4000, 200, nil, nil), nil)
   104  
   105  	tests := []struct {
   106  		n         int   // number of chunks to push to memStore
   107  		chunkSize int64 // size of chunk (by default in Swarm - 4096)
   108  	}{
   109  		{
   110  			n:         1,
   111  			chunkSize: 4096,
   112  		},
   113  		{
   114  			n:         101,
   115  			chunkSize: 4096,
   116  		},
   117  		{
   118  			n:         501,
   119  			chunkSize: 4096,
   120  		},
   121  		{
   122  			n:         1100,
   123  			chunkSize: 4096,
   124  		},
   125  	}
   126  
   127  	for i, tt := range tests {
   128  		log.Info("running test", "idx", i, "tt", tt)
   129  		var chunks []Chunk
   130  
   131  		for i := 0; i < tt.n; i++ {
   132  			c := GenerateRandomChunk(tt.chunkSize)
   133  			chunks = append(chunks, c)
   134  		}
   135  
   136  		for i := 0; i < tt.n; i++ {
   137  			err := ldb.Put(context.TODO(), chunks[i])
   138  			if err != nil {
   139  				t.Fatal(err)
   140  			}
   141  			err = memStore.Put(context.TODO(), chunks[i])
   142  			if err != nil {
   143  				t.Fatal(err)
   144  			}
   145  
   146  			if got := memStore.cache.Len(); got > cacheCap {
   147  				t.Fatalf("expected to get cache capacity less than %v, but got %v", cacheCap, got)
   148  			}
   149  
   150  		}
   151  
   152  		for i := 0; i < tt.n; i++ {
   153  			_, err := memStore.Get(context.TODO(), chunks[i].Address())
   154  			if err != nil {
   155  				if err == ErrChunkNotFound {
   156  					_, err := ldb.Get(context.TODO(), chunks[i].Address())
   157  					if err != nil {
   158  						t.Fatalf("couldn't get chunk %v from ldb, got error: %v", i, err)
   159  					}
   160  				} else {
   161  					t.Fatalf("got error from memstore: %v", err)
   162  				}
   163  			}
   164  		}
   165  	}
   166  }