github.com/vipernet-xyz/tm@v0.34.24/test/loadtime/cmd/report/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/csv"
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"os"
     9  	"strconv"
    10  	"strings"
    11  
    12  	dbm "github.com/tendermint/tm-db"
    13  
    14  	"github.com/vipernet-xyz/tm/store"
    15  	"github.com/vipernet-xyz/tm/test/loadtime/report"
    16  )
    17  
    18  var (
    19  	db     = flag.String("database-type", "goleveldb", "the type of database holding the blockstore")
    20  	dir    = flag.String("data-dir", "", "path to the directory containing the tendermint databases")
    21  	csvOut = flag.String("csv", "", "dump the extracted latencies as raw csv for use in additional tooling")
    22  )
    23  
    24  func main() {
    25  	flag.Parse()
    26  	if *db == "" {
    27  		log.Fatalf("must specify a database-type")
    28  	}
    29  	if *dir == "" {
    30  		log.Fatalf("must specify a data-dir")
    31  	}
    32  	d := strings.TrimPrefix(*dir, "~/")
    33  	if d != *dir {
    34  		h, err := os.UserHomeDir()
    35  		if err != nil {
    36  			panic(err)
    37  		}
    38  		d = h + "/" + d
    39  	}
    40  	_, err := os.Stat(d)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	dbType := dbm.BackendType(*db)
    45  	db, err := dbm.NewDB("blockstore", dbType, d)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	s := store.NewBlockStore(db)
    50  	defer s.Close()
    51  	rs, err := report.GenerateFromBlockStore(s)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	if *csvOut != "" {
    56  		cf, err := os.Create(*csvOut)
    57  		if err != nil {
    58  			panic(err)
    59  		}
    60  		w := csv.NewWriter(cf)
    61  		err = w.WriteAll(toCSVRecords(rs.List()))
    62  		if err != nil {
    63  			panic(err)
    64  		}
    65  		return
    66  	}
    67  	for _, r := range rs.List() {
    68  		fmt.Printf(""+
    69  			"Experiment ID: %s\n\n"+
    70  			"\tConnections: %d\n"+
    71  			"\tRate: %d\n"+
    72  			"\tSize: %d\n\n"+
    73  			"\tTotal Valid Tx: %d\n"+
    74  			"\tTotal Negative Latencies: %d\n"+
    75  			"\tMinimum Latency: %s\n"+
    76  			"\tMaximum Latency: %s\n"+
    77  			"\tAverage Latency: %s\n"+
    78  			"\tStandard Deviation: %s\n\n", r.ID, r.Connections, r.Rate, r.Size, len(r.All), r.NegativeCount, r.Min, r.Max, r.Avg, r.StdDev) //nolint:lll
    79  
    80  	}
    81  	fmt.Printf("Total Invalid Tx: %d\n", rs.ErrorCount())
    82  }
    83  
    84  func toCSVRecords(rs []report.Report) [][]string {
    85  	total := 0
    86  	for _, v := range rs {
    87  		total += len(v.All)
    88  	}
    89  	res := make([][]string, total+1)
    90  
    91  	res[0] = []string{"experiment_id", "block_time", "duration_ns", "tx_hash", "connections", "rate", "size"}
    92  	offset := 1
    93  	for _, r := range rs {
    94  		idStr := r.ID.String()
    95  		connStr := strconv.FormatInt(int64(r.Connections), 10)
    96  		rateStr := strconv.FormatInt(int64(r.Rate), 10)
    97  		sizeStr := strconv.FormatInt(int64(r.Size), 10)
    98  		for i, v := range r.All {
    99  			res[offset+i] = []string{idStr, strconv.FormatInt(v.BlockTime.UnixNano(), 10), strconv.FormatInt(int64(v.Duration), 10), fmt.Sprintf("%X", v.Hash), connStr, rateStr, sizeStr} //nolint: lll
   100  		}
   101  		offset += len(r.All)
   102  	}
   103  	return res
   104  }