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

     1  package report_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/google/uuid"
     8  	"google.golang.org/protobuf/types/known/timestamppb"
     9  
    10  	"github.com/vipernet-xyz/tm/test/loadtime/payload"
    11  	"github.com/vipernet-xyz/tm/test/loadtime/report"
    12  	"github.com/vipernet-xyz/tm/types"
    13  )
    14  
    15  type mockBlockStore struct {
    16  	base   int64
    17  	blocks []*types.Block
    18  }
    19  
    20  func (m *mockBlockStore) Height() int64 {
    21  	return m.base + int64(len(m.blocks))
    22  }
    23  
    24  func (m *mockBlockStore) Base() int64 {
    25  	return m.base
    26  }
    27  
    28  func (m *mockBlockStore) LoadBlock(i int64) *types.Block {
    29  	return m.blocks[i-m.base]
    30  }
    31  
    32  func TestGenerateReport(t *testing.T) {
    33  	t1 := time.Now()
    34  	u := [16]byte(uuid.New())
    35  	b1, err := payload.NewBytes(&payload.Payload{
    36  		Id:   u[:],
    37  		Time: timestamppb.New(t1.Add(-10 * time.Second)),
    38  		Size: 1024,
    39  	})
    40  	if err != nil {
    41  		t.Fatalf("generating payload %s", err)
    42  	}
    43  	b2, err := payload.NewBytes(&payload.Payload{
    44  		Id:   u[:],
    45  		Time: timestamppb.New(t1.Add(-4 * time.Second)),
    46  		Size: 1024,
    47  	})
    48  	if err != nil {
    49  		t.Fatalf("generating payload %s", err)
    50  	}
    51  	b3, err := payload.NewBytes(&payload.Payload{
    52  		Id:   u[:],
    53  		Time: timestamppb.New(t1.Add(2 * time.Second)),
    54  		Size: 1024,
    55  	})
    56  	t2 := t1.Add(time.Second)
    57  	if err != nil {
    58  		t.Fatalf("generating payload %s", err)
    59  	}
    60  	s := &mockBlockStore{
    61  		blocks: []*types.Block{
    62  			{
    63  				Data: types.Data{
    64  					Txs: []types.Tx{b1, b2},
    65  				},
    66  			},
    67  			{
    68  				// The timestamp from block H+1 is used to calculate the
    69  				// latency for the transactions in block H.
    70  				Header: types.Header{
    71  					Time: t1,
    72  				},
    73  				Data: types.Data{
    74  					Txs: []types.Tx{[]byte("error")},
    75  				},
    76  			},
    77  			{
    78  				Data: types.Data{
    79  					Txs: []types.Tx{b3, b3},
    80  				},
    81  			},
    82  			{
    83  				Header: types.Header{
    84  					Time: t2,
    85  				},
    86  				Data: types.Data{
    87  					Txs: []types.Tx{},
    88  				},
    89  			},
    90  		},
    91  	}
    92  	rs, err := report.GenerateFromBlockStore(s)
    93  	if err != nil {
    94  		t.Fatalf("generating report %s", err)
    95  	}
    96  	if rs.ErrorCount() != 1 {
    97  		t.Fatalf("ErrorCount did not match expected. Expected %d but contained %d", 1, rs.ErrorCount())
    98  	}
    99  	rl := rs.List()
   100  	if len(rl) != 1 {
   101  		t.Fatalf("number of reports did not match expected. Expected %d but contained %d", 1, len(rl))
   102  	}
   103  	r := rl[0]
   104  	if len(r.All) != 4 {
   105  		t.Fatalf("report contained different number of data points from expected. Expected %d but contained %d", 4, len(r.All)) //nolint:lll
   106  	}
   107  	if r.NegativeCount != 2 {
   108  		t.Fatalf("NegativeCount did not match expected. Expected %d but contained %d", 2, r.NegativeCount)
   109  	}
   110  	if r.Avg != 3*time.Second {
   111  		t.Fatalf("Avg did not match expected. Expected %s but contained %s", 3*time.Second, r.Avg)
   112  	}
   113  	if r.Min != -time.Second {
   114  		t.Fatalf("Min did not match expected. Expected %s but contained %s", time.Second, r.Min)
   115  	}
   116  	if r.Max != 10*time.Second {
   117  		t.Fatalf("Max did not match expected. Expected %s but contained %s", 10*time.Second, r.Max)
   118  	}
   119  	// Verified using online standard deviation calculator:
   120  	// https://www.calculator.net/standard-deviation-calculator.html?numberinputs=10%2C+4%2C+-1%2C+-1&ctype=s&x=45&y=12
   121  	expectedStdDev := 5228129047 * time.Nanosecond
   122  	if r.StdDev != expectedStdDev {
   123  		t.Fatalf("StdDev did not match expected. Expected %s but contained %s", expectedStdDev, r.StdDev)
   124  	}
   125  }