github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/storage/memstore_test.go (about)

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