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