Skip to content

Commit

Permalink
Add benchmark for getLedgerentries method
Browse files Browse the repository at this point in the history
The benchmark tests the two backends for the implementation:

* Using the new Captive Core HTTP endpoint
* Using the DB-based internal soroban-rpc implementation
  • Loading branch information
2opremio committed Mar 5, 2025
1 parent a21c84a commit f052a08
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) {
})
}

func testGetLedgerEntriesSucceeds(t *testing.T, useCore bool) {
func testGetLedgerEntriesSucceeds(t testing.TB, useCore bool) {
test := infrastructure.NewTest(t, &infrastructure.TestConfig{
EnableCoreHTTPQueryServer: useCore,
})
Expand Down Expand Up @@ -162,3 +162,65 @@ func testGetLedgerEntriesSucceeds(t *testing.T, useCore bool) {
Type: xdr.ScValTypeScvLedgerKeyContractInstance,
}))
}

func benchmarkGetLedgerEntries(b *testing.B, useCore bool) {
test := infrastructure.NewTest(b, &infrastructure.TestConfig{
EnableCoreHTTPQueryServer: useCore,
})
_, contractID, contractHash := test.CreateHelloWorldContract()

contractCodeKeyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractCode,
ContractCode: &xdr.LedgerKeyContractCode{
Hash: contractHash,
},
})
require.NoError(b, err)

// Doesn't exist.
notFoundKeyB64, err := xdr.MarshalBase64(getCounterLedgerKey(contractID))
require.NoError(b, err)

contractIDHash := xdr.Hash(contractID)
contractInstanceKeyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.LedgerKeyContractData{
Contract: xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &contractIDHash,
},
Key: xdr.ScVal{
Type: xdr.ScValTypeScvLedgerKeyContractInstance,
},
Durability: xdr.ContractDataDurabilityPersistent,
},
})
require.NoError(b, err)

keys := []string{contractCodeKeyB64, notFoundKeyB64, contractInstanceKeyB64}
request := protocol.GetLedgerEntriesRequest{
Keys: keys,
}

client := test.GetRPCLient()

b.ResetTimer()

for range b.N {
result, err := client.GetLedgerEntries(context.Background(), request)
b.StopTimer()
require.NoError(b, err)
require.Len(b, result.Entries, 2)
require.Positive(b, result.LatestLedger)
b.StartTimer()
}
}

func BenchmarkGetLedgerEntries(b *testing.B) {
b.Run("WithCore", func(b *testing.B) {
benchmarkGetLedgerEntries(b, true)
})
b.Run("WithoutCore", func(b *testing.B) {
benchmarkGetLedgerEntries(b, false)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"github.com/stellar/stellar-rpc/protocol"
)

func getTransaction(t *testing.T, client *client.Client, hash string) protocol.GetTransactionResponse {
func getTransaction(t testing.TB, client *client.Client, hash string) protocol.GetTransactionResponse {
var result protocol.GetTransactionResponse
for i := 0; i < 60; i++ {
for range 60 {
var err error
request := protocol.GetTransactionRequest{Hash: hash}
result, err = client.GetTransaction(context.Background(), request)
Expand All @@ -36,7 +36,7 @@ func getTransaction(t *testing.T, client *client.Client, hash string) protocol.G
return result
}

func logTransactionResult(t *testing.T, response protocol.GetTransactionResponse) {
func logTransactionResult(t testing.TB, response protocol.GetTransactionResponse) {
var txResult xdr.TransactionResult
require.NoError(t, xdr.SafeUnmarshalBase64(response.ResultXDR, &txResult))
t.Logf("error: %#v\n", txResult)
Expand All @@ -61,7 +61,7 @@ func logTransactionResult(t *testing.T, response protocol.GetTransactionResponse
}
}

func SendSuccessfulTransaction(t *testing.T, client *client.Client, kp *keypair.Full,
func SendSuccessfulTransaction(t testing.TB, client *client.Client, kp *keypair.Full,
transaction *txnbuild.Transaction,
) protocol.GetTransactionResponse {
tx, err := transaction.Sign(StandaloneNetworkPassphrase, kp)
Expand Down Expand Up @@ -99,7 +99,7 @@ func SendSuccessfulTransaction(t *testing.T, client *client.Client, kp *keypair.
return response
}

func SimulateTransactionFromTxParams(t *testing.T, client *client.Client,
func SimulateTransactionFromTxParams(t testing.TB, client *client.Client,
params txnbuild.TransactionParams,
) protocol.SimulateTransactionResponse {
savedAutoIncrement := params.IncrementSequenceNum
Expand All @@ -115,7 +115,7 @@ func SimulateTransactionFromTxParams(t *testing.T, client *client.Client,
return response
}

func PreflightTransactionParamsLocally(t *testing.T, params txnbuild.TransactionParams,
func PreflightTransactionParamsLocally(t testing.TB, params txnbuild.TransactionParams,
response protocol.SimulateTransactionResponse,
) txnbuild.TransactionParams {
if !assert.Empty(t, response.Error) {
Expand Down Expand Up @@ -144,13 +144,13 @@ func PreflightTransactionParamsLocally(t *testing.T, params txnbuild.Transaction
}
v.Auth = auth
case *txnbuild.ExtendFootprintTtl:
require.Len(t, response.Results, 0)
require.Empty(t, response.Results)
v.Ext = xdr.TransactionExt{
V: 1,
SorobanData: &transactionData,
}
case *txnbuild.RestoreFootprint:
require.Len(t, response.Results, 0)
require.Empty(t, response.Results)
v.Ext = xdr.TransactionExt{
V: 1,
SorobanData: &transactionData,
Expand All @@ -165,7 +165,7 @@ func PreflightTransactionParamsLocally(t *testing.T, params txnbuild.Transaction
return params
}

func PreflightTransactionParams(t *testing.T, client *client.Client, params txnbuild.TransactionParams,
func PreflightTransactionParams(t testing.TB, client *client.Client, params txnbuild.TransactionParams,
) txnbuild.TransactionParams {
response := SimulateTransactionFromTxParams(t, client, params)
// The preamble should be zero except for the special restore case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func CreateInvokeHostOperation(sourceAccount string, contractID xdr.Hash, method
}
}

func getContractID(t *testing.T, sourceAccount string, salt [32]byte, networkPassphrase string) [32]byte {
func getContractID(t testing.TB, sourceAccount string, salt [32]byte, networkPassphrase string) [32]byte {
sourceAccountID := xdr.MustAddress(sourceAccount)
preImage := xdr.HashIdPreimage{
Type: xdr.EnvelopeTypeEnvelopeTypeContractId,
Expand Down
12 changes: 6 additions & 6 deletions cmd/stellar-rpc/internal/integrationtest/infrastructure/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type TestPorts struct {
}

type Test struct {
t *testing.T
t testing.TB

testPorts TestPorts

Expand All @@ -119,7 +119,7 @@ type Test struct {
enableCoreHTTPQueryServer bool
}

func NewTest(t *testing.T, cfg *TestConfig) *Test {
func NewTest(t testing.TB, cfg *TestConfig) *Test {
if os.Getenv("STELLAR_RPC_INTEGRATION_TESTS_ENABLED") == "" {
t.Skip("skipping integration test: STELLAR_RPC_INTEGRATION_TESTS_ENABLED not set")
}
Expand Down Expand Up @@ -153,8 +153,8 @@ func NewTest(t *testing.T, cfg *TestConfig) *Test {
i.sqlitePath = path.Join(i.t.TempDir(), "stellar_rpc.sqlite")
}

if parallel {
t.Parallel()
if tt, ok := t.(*testing.T); ok && parallel {
tt.Parallel()
}

if i.protocolVersion == 0 {
Expand Down Expand Up @@ -424,13 +424,13 @@ func (i *Test) generateRPCConfigFile(rpcConfig rpcConfig) {
require.NoError(i.t, err)
}

func newTestLogWriter(t *testing.T, prefix string) *testLogWriter {
func newTestLogWriter(t testing.TB, prefix string) *testLogWriter {
tw := &testLogWriter{t: t, prefix: prefix}
return tw
}

type testLogWriter struct {
t *testing.T
t testing.TB
prefix string
}

Expand Down

0 comments on commit f052a08

Please sign in to comment.