-
Notifications
You must be signed in to change notification settings - Fork 879
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
175 lines (145 loc) · 5.21 KB
/
benchmark_test.go
File metadata and controls
175 lines (145 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package app
import (
"context"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/app/benchmark"
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/log"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db"
)
func createTestContext() sdk.Context {
db := dbm.NewMemDB()
logger := log.NewNopLogger()
ms := rootmulti.NewStore(db, log.NewNopLogger())
return sdk.NewContext(ms, sdk.Header{}, false, logger)
}
func TestPrepareProposalBenchmarkHandler(t *testing.T) {
// Create a mock app with benchmark mode enabled
logger := log.NewNopLogger()
app := &App{}
// Test handler with nil manager (should return empty proposal)
ctx := createTestContext()
req := &abci.RequestPrepareProposal{
Height: 1,
Time: time.Now(),
}
resp, err := app.PrepareProposalBenchmarkHandler(ctx, req)
require.NoError(t, err)
require.NotNil(t, resp)
require.Len(t, resp.TxRecords, 0)
// Create a mock manager with a channel
proposalCh := make(chan *abci.ResponsePrepareProposal, 1)
testProposal := &abci.ResponsePrepareProposal{
TxRecords: []*abci.TxRecord{
{Action: abci.TxRecord_UNMODIFIED, Tx: []byte("tx1")},
{Action: abci.TxRecord_UNMODIFIED, Tx: []byte("tx2")},
},
}
proposalCh <- testProposal
app.benchmarkManager = &benchmark.Manager{
Logger: benchmark.NewLogger(logger),
}
// We can't easily set the proposalCh since it's unexported, so we test the nil case
// Test that handler doesn't panic with nil manager
app.benchmarkManager = nil
resp2, err := app.PrepareProposalBenchmarkHandler(ctx, req)
require.NoError(t, err)
require.NotNil(t, resp2)
require.Len(t, resp2.TxRecords, 0)
}
func TestBenchmarkHelperMethods(t *testing.T) {
app := &App{}
// Test helper methods with nil manager (should not panic)
app.RecordBenchmarkCommitTime(100 * time.Millisecond)
app.StartBenchmarkBlockProcessing()
app.EndBenchmarkBlockProcessing()
// BenchmarkLogger should return nil when manager is nil
require.Nil(t, app.BenchmarkLogger())
// Create a benchmark manager with logger
benchLogger := benchmark.NewLogger(log.NewNopLogger())
app.benchmarkManager = &benchmark.Manager{
Logger: benchLogger,
}
// Now helper methods should work (just verify they don't panic)
app.RecordBenchmarkCommitTime(100 * time.Millisecond)
app.StartBenchmarkBlockProcessing()
app.EndBenchmarkBlockProcessing()
// BenchmarkLogger should return the logger
require.NotNil(t, app.BenchmarkLogger())
require.Equal(t, benchLogger, app.BenchmarkLogger())
}
func TestInitBenchmark_PanicsOnLiveChainID(t *testing.T) {
logger := log.NewNopLogger()
chainID := "pacific-1"
liveEVMChainID := int64(1329) // pacific-1's EVM chain ID (live)
// Create a minimal App struct
app := &App{
encodingConfig: MakeEncodingConfig(),
}
ctx := context.Background()
// Test that InitBenchmark panics with live chain ID
require.Panics(t, func() {
app.InitBenchmark(ctx, chainID, liveEVMChainID, logger)
}, "InitBenchmark should panic on live chain ID")
// Verify nothing was initialized
require.Nil(t, app.benchmarkManager, "benchmarkManager should not be initialized on panic")
}
func TestInitBenchmark_AllLiveChainIDs(t *testing.T) {
logger := log.NewNopLogger()
liveChainIDs := []struct {
chainID string
evmChainID int64
description string
}{
{"pacific-1", 1329, "pacific-1"},
{"atlantic-2", 1328, "atlantic-2"},
{"arctic-1", 713715, "arctic-1"},
}
for _, tc := range liveChainIDs {
t.Run(tc.description, func(t *testing.T) {
app := &App{
encodingConfig: MakeEncodingConfig(),
}
ctx := context.Background()
require.Panics(t, func() {
app.InitBenchmark(ctx, tc.chainID, tc.evmChainID, logger)
}, "InitBenchmark should panic on live chain ID: %s", tc.description)
})
}
}
func TestInitBenchmark_Success(t *testing.T) {
logger := log.NewNopLogger()
chainID := "test-chain"
evmChainID := int64(12345) // Non-live chain ID
// Create a minimal App struct with required fields
app := &App{
encodingConfig: MakeEncodingConfig(),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Test InitBenchmark with non-live chain ID
app.InitBenchmark(ctx, chainID, evmChainID, logger)
// Verify benchmarkManager is set
require.NotNil(t, app.benchmarkManager, "benchmarkManager should be initialized")
require.NotNil(t, app.benchmarkManager.Logger, "benchmarkManager.Logger should be set")
require.NotNil(t, app.benchmarkManager.Generator, "benchmarkManager.Generator should be set")
// Verify we can get the proposal channel
require.NotNil(t, app.benchmarkManager.ProposalChannel(), "proposal channel should be available")
// Consume a proposal to verify the channel is working
select {
case proposal, ok := <-app.benchmarkManager.ProposalChannel():
if ok {
require.NotNil(t, proposal, "Proposal should not be nil")
// EVMTransfer scenario doesn't need deployment, so should get load txs immediately
t.Logf("Received proposal with %d tx records", len(proposal.TxRecords))
}
case <-time.After(5 * time.Second):
t.Log("Timeout waiting for proposal (may be in setup phase)")
}
// Cancel context to stop the generator
cancel()
}