github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/tools/utxo_benchmark/utxo_benchmark.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/binary"
     5  	"os"
     6  	"runtime/debug"
     7  	"time"
     8  
     9  	"github.com/piotrnar/gocoin/lib/others/sys"
    10  	"github.com/piotrnar/gocoin/lib/utxo"
    11  )
    12  
    13  func main() {
    14  	var tmp uint32
    15  	var dir = ""
    16  
    17  	println("UtxoIdxLen:", utxo.UtxoIdxLen)
    18  	if len(os.Args) > 1 {
    19  		dir = os.Args[1]
    20  	}
    21  
    22  	sta := time.Now()
    23  	db := utxo.NewUnspentDb(&utxo.NewUnspentOpts{Dir: dir})
    24  	if db == nil {
    25  		println("place UTXO.db or UTXO.old in the current folder")
    26  		return
    27  	}
    28  
    29  	var cnt int
    30  	for i := range db.HashMap {
    31  		cnt += len(db.HashMap[i])
    32  	}
    33  	println(cnt, "UTXO records/txs loaded in", time.Now().Sub(sta).String())
    34  
    35  	debug.SetGCPercent(30)
    36  
    37  	print("Going through the map...")
    38  	sta = time.Now()
    39  	for i := range db.HashMap {
    40  		for k, v := range db.HashMap[i] {
    41  			if v != nil {
    42  				tmp += binary.LittleEndian.Uint32(k[:])
    43  
    44  			}
    45  		}
    46  	}
    47  	tim := time.Now().Sub(sta)
    48  	println("\rGoing through the map done in", tim.String(), tmp)
    49  
    50  	print("Going through the map for the slice...")
    51  	tmp = 0
    52  	sta = time.Now()
    53  	for i := range db.HashMap {
    54  		for _, v := range db.HashMap[i] {
    55  			tmp += binary.LittleEndian.Uint32(v)
    56  		}
    57  	}
    58  	println("\rGoing through the map for the slice done in", time.Now().Sub(sta).String(), tmp)
    59  
    60  	print("Decoding all records in static mode ...")
    61  	tmp = 0
    62  	sta = time.Now()
    63  	for i := range db.HashMap {
    64  		for k, v := range db.HashMap[i] {
    65  			tmp += utxo.NewUtxoRecStatic(k, v).InBlock
    66  		}
    67  	}
    68  	println("\rDecoding all records in static mode done in", time.Now().Sub(sta).String(), tmp)
    69  
    70  	print("Decoding all records in dynamic mode ...")
    71  	tmp = 0
    72  	sta = time.Now()
    73  	for i := range db.HashMap {
    74  		for k, v := range db.HashMap[i] {
    75  			tmp += utxo.NewUtxoRec(k, v).InBlock
    76  		}
    77  	}
    78  	println("\rDecoding all records in dynamic mode done in", time.Now().Sub(sta).String(), tmp)
    79  
    80  	al, sy := sys.MemUsed()
    81  	println("Mem Used:", al>>20, "/", sy>>20)
    82  }