Skip to content

Commit

Permalink
Merge pull request #80 from oasisprotocol/matevz/fix/paratime-stats
Browse files Browse the repository at this point in the history
oasis paratime statistics fixes
  • Loading branch information
matevz committed May 30, 2023
2 parents bb0d0fe + a16f162 commit c60fc0e
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions cmd/paratime/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
Expand All @@ -28,17 +27,7 @@ import (
"github.com/oasisprotocol/cli/metadata"
)

var (
writeCSV bool
fileCSV string

csvFlags *flag.FlagSet = func() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.BoolVar(&writeCSV, "write-csv", false, "write stats to CSV")
fs.StringVar(&fileCSV, "csv-file", "", "custom CSV file path")
return fs
}()
)
var fileCSV string

type runtimeStats struct {
// Rounds.
Expand Down Expand Up @@ -101,8 +90,11 @@ type entityStats struct {
}

var statsCmd = &cobra.Command{
Use: "statistics [<start-height> [<end-height>]]",
Short: "Show ParaTime statistics",
Use: "statistics [<start-height> [<end-height>]]",
Short: "Show ParaTime statistics",
Long: "Show ParaTime statistics between start-height and end-height round of blocks." +
"\nIf negative start-height is passed, it will show statistics for the last given blocks." +
"\nIf 0 start-height passed, it will generate statistics from the oldest block available to the endpoint.",
Aliases: []string{"stats"},
Args: cobra.MaximumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -115,7 +107,7 @@ var statsCmd = &cobra.Command{

// Parse command line arguments
var (
startHeightArg int64
startHeightArg int64 = -1
endHeight uint64
)
if argLen := len(args); argLen > 0 {
Expand Down Expand Up @@ -166,12 +158,15 @@ var statsCmd = &cobra.Command{
cobra.CheckErr(err)
signature.SetChainContext(chainCtx)

fmt.Printf(
"gathering statistics: runtime-id: %s, start-height: %d, end-height: %d\n",
runtimeID,
startHeight,
endHeight,
)
fmt.Println("=== PARATIME STATISTICS ===")
fmt.Printf("%-26s %s", "Network:", npa.PrettyPrintNetwork())
fmt.Println()
fmt.Printf("%-26s %s", "ParaTime ID:", runtimeID)
fmt.Println()
fmt.Printf("%-26s %8d", "Start height:", startHeight)
fmt.Println()
fmt.Printf("%-26s %8d", "End height:", endHeight)
fmt.Println()

// Do the actual work
stats := &runtimeStats{
Expand Down Expand Up @@ -458,14 +453,12 @@ var statsCmd = &cobra.Command{
stats.prepareEntitiesOutput(entityMetadataLookup)
stats.printStats()

if !writeCSV {
if fileCSV == "" {
stats.printEntityStats()
return
}

// Also save entity stats in a csv.
if fileCSV == "" {
fileCSV = fmt.Sprintf("runtime-%s-%d-%d-stats.csv", runtimeID, startHeight, endHeight)
}
fout, err := os.Create(fileCSV)
cobra.CheckErr(err)
defer fout.Close()
Expand Down Expand Up @@ -539,15 +532,25 @@ func (s *runtimeStats) prepareEntitiesOutput(
}

func (s *runtimeStats) printStats() {
fmt.Printf("ParaTime rounds: %d\n", s.rounds)
fmt.Printf("Successful rounds: %d\n", s.successfulRounds)
fmt.Printf("Epoch transition rounds: %d\n", s.epochTransitionRounds)
fmt.Printf("Proposer timeouted rounds: %d\n", s.proposerTimeoutedRounds)
fmt.Printf("Failed rounds: %d\n", s.failedRounds)
fmt.Printf("Discrepancies: %d\n", s.discrepancyDetected)
fmt.Printf("Discrepancies (timeout): %d\n", s.discrepancyDetectedTimeout)
fmt.Printf("Suspended: %d\n", s.suspendedRounds)
fmt.Printf("%-26s %d", "ParaTime rounds:", s.rounds)
fmt.Println()
fmt.Printf("%-26s %d", "Successful rounds:", s.successfulRounds)
fmt.Println()
fmt.Printf("%-26s %d", "Epoch transition rounds:", s.epochTransitionRounds)
fmt.Println()
fmt.Printf("%-26s %d", "Proposer timed out rounds:", s.proposerTimeoutedRounds)
fmt.Println()
fmt.Printf("%-26s %d", "Failed rounds:", s.failedRounds)
fmt.Println()
fmt.Printf("%-26s %d", "Discrepancies:", s.discrepancyDetected)
fmt.Println()
fmt.Printf("%-26s %d", "Discrepancies (timeout):", s.discrepancyDetectedTimeout)
fmt.Println()
fmt.Printf("%-26s %d", "Suspended:", s.suspendedRounds)
fmt.Println()
}

func (s *runtimeStats) printEntityStats() {
fmt.Println("\n=== ENTITY STATISTICS ===")
table := tablewriter.NewWriter(os.Stdout)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
Expand All @@ -559,5 +562,5 @@ func (s *runtimeStats) printStats() {

func init() {
statsCmd.Flags().AddFlagSet(common.SelectorNPFlags)
statsCmd.Flags().AddFlagSet(csvFlags)
statsCmd.Flags().StringVarP(&fileCSV, "output-file", "o", "", "output statistics into specified CSV file")
}

0 comments on commit c60fc0e

Please sign in to comment.