github.com/igggame/nebulas-go@v2.1.0+incompatible/storage/disk_storage_test.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package storage
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"math/rand"
    27  	"time"
    28  
    29  	"os"
    30  
    31  	"github.com/syndtr/goleveldb/leveldb"
    32  	"github.com/syndtr/goleveldb/leveldb/filter"
    33  	"github.com/syndtr/goleveldb/leveldb/opt"
    34  )
    35  
    36  func TestNewDiskStorage(t *testing.T) {
    37  	storage, err := NewDiskStorage("disk.db")
    38  	assert.Nil(t, err)
    39  	keys := [][]byte{[]byte("1"), []byte("2")}
    40  	values := [][]byte{[]byte("1"), []byte("2")}
    41  	storage.Put(keys[0], values[0])
    42  	storage.Put(keys[1], values[1])
    43  	value1, err1 := storage.Get(keys[0])
    44  	assert.Nil(t, err1)
    45  	assert.Equal(t, value1, values[0])
    46  	storage.Del(keys[1])
    47  	_, err2 := storage.Get(keys[1])
    48  	assert.NotNil(t, err2)
    49  }
    50  
    51  const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    52  
    53  func randBytes(n int) []byte {
    54  	b := make([]byte, n)
    55  	for i := range b {
    56  		b[i] = letterBytes[rand.Intn(len(letterBytes))]
    57  	}
    58  	return b
    59  }
    60  
    61  func TestLeveldbBenchmark(t *testing.T) {
    62  	file := "benchmark.db"
    63  	db, err := leveldb.OpenFile(file, &opt.Options{
    64  		OpenFilesCacheCapacity: 500,
    65  		BlockCacheCapacity:     8 * opt.MiB,
    66  		WriteBuffer:            4 * opt.MiB,
    67  		Filter:                 filter.NewBloomFilter(10),
    68  	})
    69  	assert.Nil(t, err)
    70  
    71  	tests := []struct {
    72  		name  string
    73  		key   []byte
    74  		value []byte
    75  		count int64
    76  	}{
    77  		{"1", []byte("key1"), []byte("value1"), 0},
    78  		//{"2", []byte("key2"), []byte("value2"), 10000},
    79  		//{"3", []byte("key3"), []byte("value3"), 100000},
    80  		//{"4", []byte("key4"), []byte("value4"), 1000000},
    81  		//{"5", []byte("key5"), []byte("value5"), 4000000},
    82  	}
    83  
    84  	count := int64(0)
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  
    88  			err := db.Put(tt.key, tt.value, nil)
    89  			assert.Nil(t, err)
    90  
    91  			count = count + tt.count
    92  			for i := int64(0); i < tt.count; i++ {
    93  				err := db.Put(randBytes(32), randBytes(rand.Intn(1024)), nil)
    94  				assert.Nil(t, err)
    95  			}
    96  
    97  			start := time.Now().UnixNano()
    98  			value, err := db.Get(tt.key, nil)
    99  			duration := time.Now().UnixNano() - start
   100  			assert.Nil(t, err)
   101  			assert.Equal(t, tt.value, value)
   102  
   103  			t.Log("count:", count, "duration:", duration, " Nano")
   104  		})
   105  	}
   106  	db.Close()
   107  	os.Remove(file)
   108  }