From f1b14c3d7df5fee63746a28ca41473396e8c3779 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 1 Aug 2023 14:18:05 +0200 Subject: [PATCH] Add DB benchmark and extra ledger entry --- .../internal/preflight/preflight_test.go | 75 +++++++++++++++++-- 1 file changed, 70 insertions(+), 5 deletions(-) diff --git a/cmd/soroban-rpc/internal/preflight/preflight_test.go b/cmd/soroban-rpc/internal/preflight/preflight_test.go index 56a02928e1..5bfdfea8d3 100644 --- a/cmd/soroban-rpc/internal/preflight/preflight_test.go +++ b/cmd/soroban-rpc/internal/preflight/preflight_test.go @@ -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} @@ -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{ @@ -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 { @@ -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(), @@ -221,9 +263,24 @@ 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) @@ -231,3 +288,11 @@ func BenchmarkGetPreflight(b *testing.B) { require.NoError(b, err) } } + +func BenchmarkGetPreflightInMemory(b *testing.B) { + benchmark(b, true) +} + +func BenchmarkGetPreflightInDB(b *testing.B) { + benchmark(b, false) +}