github.com/igggame/nebulas-go@v2.1.0+incompatible/nebtestkit/benchmark/storage.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 main
    20  
    21  import (
    22  	"fmt"
    23  	"os"
    24  	"strconv"
    25  	"time"
    26  
    27  	"github.com/nebulasio/go-nebulas/common/trie"
    28  	"github.com/nebulasio/go-nebulas/storage"
    29  	"github.com/nebulasio/go-nebulas/util/byteutils"
    30  )
    31  
    32  func main() {
    33  	file := os.Args[1]
    34  	cnt, err := strconv.ParseInt(os.Args[2], 10, 32)
    35  	if err != nil {
    36  		fmt.Println("Parse Int Error ", err)
    37  		return
    38  	}
    39  	rootHash := os.Args[3]
    40  	stor, err := storage.NewDiskStorage(file)
    41  	if err != nil {
    42  		fmt.Println("OpenDB Error ", err)
    43  		return
    44  	}
    45  	fmt.Println("cnt:", cnt, " file:", file, " root:", rootHash)
    46  
    47  	startAt := time.Now().Unix()
    48  	root, err := byteutils.FromHex(rootHash)
    49  	if err != nil {
    50  		fmt.Println("Parse Hex Error ", err)
    51  		return
    52  	}
    53  	txsState, err := trie.NewTrie(root, stor, false)
    54  	if err != nil {
    55  		fmt.Println("NewTrie Error ", err)
    56  		return
    57  	}
    58  	iter, err := txsState.Iterator(nil)
    59  	if err != nil {
    60  		fmt.Println("Iterator Error ", err)
    61  		return
    62  	}
    63  	exist, err := iter.Next()
    64  	if err != nil {
    65  		fmt.Println("Next Error1 ", err)
    66  		return
    67  	}
    68  	i := cnt
    69  	for exist {
    70  		exist, err = iter.Next()
    71  		i--
    72  		if i == 0 {
    73  			fmt.Println("Read Over")
    74  			break
    75  		}
    76  		if err != nil {
    77  			fmt.Println("Next Error2 ", err)
    78  			return
    79  		}
    80  	}
    81  	endAt := time.Now().Unix()
    82  	diff := endAt - startAt
    83  	if diff == 0 {
    84  		fmt.Println("Diff is zero")
    85  		return
    86  	}
    87  	op := cnt - i
    88  	fmt.Println("TPS ", op, "/", diff, "s = ", op/diff)
    89  	fmt.Println("Done")
    90  }