Skip to content

Commit

Permalink
Add DB benchmark and extra ledger entry
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Aug 1, 2023
1 parent fcef978 commit f1b14c3
Showing 1 changed file with 70 additions and 5 deletions.
75 changes: 70 additions & 5 deletions cmd/soroban-rpc/internal/preflight/preflight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/require"

"github.com/stellar/soroban-tools/cmd/soroban-rpc/internal/db"
)

var mockContractID = xdr.Hash{0xa, 0xb, 0xc}
Expand All @@ -28,7 +30,8 @@ var mockLedgerEntries = []xdr.LedgerEntry{
Key: xdr.ScVal{
Type: xdr.ScValTypeScvLedgerKeyContractInstance,
},
Durability: xdr.ContractDataDurabilityPersistent,
Durability: xdr.ContractDataDurabilityPersistent,
ExpirationLedgerSeq: 100000,
Body: xdr.ContractDataEntryBody{
BodyType: xdr.ContractEntryBodyTypeDataEntry,
Data: &xdr.ContractDataEntryData{
Expand Down Expand Up @@ -142,6 +145,26 @@ var mockLedgerEntries = []xdr.LedgerEntry{
},
},
},
{
LastModifiedLedgerSeq: 2,
Data: xdr.LedgerEntryData{
Type: xdr.LedgerEntryTypeConfigSetting,
ConfigSetting: &xdr.ConfigSettingEntry{
ConfigSettingId: xdr.ConfigSettingIdConfigSettingStateExpiration,
StateExpirationSettings: &xdr.StateExpirationSettings{
MaxEntryExpiration: 100,
MinTempEntryExpiration: 100,
MinPersistentEntryExpiration: 100,
AutoBumpLedgers: 100,
PersistentRentRateDenominator: 100,
TempRentRateDenominator: 100,
MaxEntriesToExpire: 100,
BucketListSizeWindowSampleSize: 100,
EvictionScanSize: 100,
},
},
},
},
}

var helloWorldContract = func() []byte {
Expand Down Expand Up @@ -193,9 +216,28 @@ func (m inMemoryLedgerEntryReadTx) Done() error {
return nil
}

func BenchmarkGetPreflight(b *testing.B) {
ledgerEntryReadTx, err := newInMemoryLedgerEntryReadTx(mockLedgerEntries)
require.NoError(b, err)
func getPreflightParameters(t testing.TB, inMemory bool) PreflightParameters {
var ledgerEntryReadTx db.LedgerEntryReadTx
if inMemory {
var err error
ledgerEntryReadTx, err = newInMemoryLedgerEntryReadTx(mockLedgerEntries)
require.NoError(t, err)
} else {
d := t.TempDir()
dbInstance, err := db.OpenSQLiteDB(path.Join(d, "soroban_rpc.sqlite"))
require.NoError(t, err)
readWriter := db.NewReadWriter(dbInstance, 100, 10000)
tx, err := readWriter.NewTx(context.Background())
require.NoError(t, err)
for _, e := range mockLedgerEntries {
err := tx.LedgerEntryWriter().UpsertLedgerEntry(e)
require.NoError(t, err)
}
err = tx.Commit(2)
require.NoError(t, err)
ledgerEntryReadTx, err = db.NewLedgerEntryReader(dbInstance).NewTx(context.Background())
require.NoError(t, err)
}
argSymbol := xdr.ScSymbol("world")
params := PreflightParameters{
Logger: log.New(),
Expand All @@ -221,13 +263,36 @@ func BenchmarkGetPreflight(b *testing.B) {
}},
NetworkPassphrase: "foo",
LedgerEntryReadTx: ledgerEntryReadTx,
BucketListSize: 200,
}
b.ResetTimer()
return params
}

func TestGetPreflight(t *testing.T) {
params := getPreflightParameters(t, false)
_, err := GetPreflight(context.Background(), params)
require.NoError(t, err)

params = getPreflightParameters(t, true)
_, err = GetPreflight(context.Background(), params)
require.NoError(t, err)
}

func benchmark(b *testing.B, inMemory bool) {
params := getPreflightParameters(b, inMemory)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
_, err := GetPreflight(context.Background(), params)
b.StopTimer()
require.NoError(b, err)
}
}

func BenchmarkGetPreflightInMemory(b *testing.B) {
benchmark(b, true)
}

func BenchmarkGetPreflightInDB(b *testing.B) {
benchmark(b, false)
}

0 comments on commit f1b14c3

Please sign in to comment.