diff --git a/pkg/ledger/benchmarks_test.go b/pkg/ledger/benchmarks_test.go new file mode 100644 index 000000000..33d31e756 --- /dev/null +++ b/pkg/ledger/benchmarks_test.go @@ -0,0 +1,279 @@ +package ledger_test + +import ( + "context" + "fmt" + "strconv" + "testing" + + "github.com/numary/ledger/pkg/core" + "github.com/numary/ledger/pkg/ledger" + "github.com/stretchr/testify/require" +) + +const nbPostings = 1000 + +func BenchmarkLedger_PostTransactions_Scripts_Single_FixedAccounts(b *testing.B) { + var execResScript core.ExpandedTransaction + + txData := core.TransactionData{} + for i := 0; i < nbPostings; i++ { + txData.Postings = append(txData.Postings, core.Posting{ + Source: "world", + Destination: "benchmarks:" + strconv.Itoa(i), + Asset: "COIN", + Amount: core.NewMonetaryInt(10), + }) + } + _, err := txData.Postings.Validate() + require.NoError(b, err) + + runOnLedger(func(l *ledger.Ledger) { + b.ResetTimer() + + res := core.ExpandedTransaction{} + + for n := 0; n < b.N; n++ { + b.StopTimer() + script := txToScriptData(txData) + b.StartTimer() + res, err = l.ExecuteScript(context.Background(), true, script) + require.NoError(b, err) + require.Len(b, res.Postings, nbPostings) + } + + execResScript = res + require.Len(b, execResScript.Postings, nbPostings) + }) +} + +func BenchmarkLedger_PostTransactions_Postings_Single_FixedAccounts(b *testing.B) { + var execRes []core.ExpandedTransaction + + runOnLedger(func(l *ledger.Ledger) { + txData := core.TransactionData{} + for i := 0; i < nbPostings; i++ { + txData.Postings = append(txData.Postings, core.Posting{ + Source: "world", + Destination: "benchmarks:" + strconv.Itoa(i), + Asset: "COIN", + Amount: core.NewMonetaryInt(10), + }) + } + + b.ResetTimer() + + res := []core.ExpandedTransaction{} + + for n := 0; n < b.N; n++ { + _, err := txData.Postings.Validate() + require.NoError(b, err) + res, err = l.ExecuteTxsData(context.Background(), true, txData) + require.NoError(b, err) + require.Len(b, res, 1) + require.Len(b, res[0].Postings, nbPostings) + } + + execRes = res + require.Len(b, execRes, 1) + require.Len(b, execRes[0].Postings, nbPostings) + }) +} + +func BenchmarkLedger_PostTransactions_Postings_Batch_FixedAccounts(b *testing.B) { + var execRes []core.ExpandedTransaction + + txsData := newTxsData(1) + + runOnLedger(func(l *ledger.Ledger) { + b.ResetTimer() + + res := []core.ExpandedTransaction{} + + for n := 0; n < b.N; n++ { + var err error + for _, txData := range txsData { + _, err := txData.Postings.Validate() + require.NoError(b, err) + } + res, err = l.ExecuteTxsData(context.Background(), true, txsData...) + require.NoError(b, err) + require.Len(b, res, 7) + require.Len(b, res[0].Postings, 1) + require.Len(b, res[1].Postings, 1) + require.Len(b, res[2].Postings, 2) + require.Len(b, res[3].Postings, 4) + require.Len(b, res[4].Postings, 4) + require.Len(b, res[5].Postings, 1) + require.Len(b, res[6].Postings, 1) + } + + execRes = res + require.Len(b, execRes, 7) + require.Len(b, execRes[0].Postings, 1) + require.Len(b, execRes[1].Postings, 1) + require.Len(b, execRes[2].Postings, 2) + require.Len(b, execRes[3].Postings, 4) + require.Len(b, execRes[4].Postings, 4) + require.Len(b, execRes[5].Postings, 1) + require.Len(b, execRes[6].Postings, 1) + }) +} + +func BenchmarkLedger_PostTransactions_Postings_Batch_VaryingAccounts(b *testing.B) { + var execRes []core.ExpandedTransaction + + runOnLedger(func(l *ledger.Ledger) { + b.ResetTimer() + + res := []core.ExpandedTransaction{} + + for n := 0; n < b.N; n++ { + b.StopTimer() + txsData := newTxsData(n) + b.StartTimer() + var err error + for _, txData := range txsData { + _, err := txData.Postings.Validate() + require.NoError(b, err) + } + res, err = l.ExecuteTxsData(context.Background(), true, txsData...) + require.NoError(b, err) + require.Len(b, res, 7) + require.Len(b, res[0].Postings, 1) + require.Len(b, res[1].Postings, 1) + require.Len(b, res[2].Postings, 2) + require.Len(b, res[3].Postings, 4) + require.Len(b, res[4].Postings, 4) + require.Len(b, res[5].Postings, 1) + require.Len(b, res[6].Postings, 1) + } + + execRes = res + require.Len(b, execRes, 7) + require.Len(b, execRes[0].Postings, 1) + require.Len(b, execRes[1].Postings, 1) + require.Len(b, execRes[2].Postings, 2) + require.Len(b, execRes[3].Postings, 4) + require.Len(b, execRes[4].Postings, 4) + require.Len(b, execRes[5].Postings, 1) + require.Len(b, execRes[6].Postings, 1) + }) +} + +func newTxsData(i int) []core.TransactionData { + return []core.TransactionData{ + { + Postings: core.Postings{ + { + Source: "world", + Destination: fmt.Sprintf("payins:%d", i), + Amount: core.NewMonetaryInt(10000), + Asset: "EUR/2", + }, + }, + }, + { + Postings: core.Postings{ + { + Source: fmt.Sprintf("payins:%d", i), + Destination: fmt.Sprintf("users:%d:wallet", i), + Amount: core.NewMonetaryInt(10000), + Asset: "EUR/2", + }, + }, + }, + { + Postings: core.Postings{ + { + Source: "world", + Destination: fmt.Sprintf("teller:%d", i), + Amount: core.NewMonetaryInt(350000), + Asset: "RBLX/6", + }, + { + Source: "world", + Destination: fmt.Sprintf("teller:%d", i), + Amount: core.NewMonetaryInt(1840000), + Asset: "SNAP/6", + }, + }, + }, + { + Postings: core.Postings{ + { + Source: fmt.Sprintf("users:%d:wallet", i), + Destination: fmt.Sprintf("trades:%d", i), + Amount: core.NewMonetaryInt(1500), + Asset: "EUR/2", + }, + { + Source: fmt.Sprintf("trades:%d", i), + Destination: fmt.Sprintf("fiat:holdings:%d", i), + Amount: core.NewMonetaryInt(1500), + Asset: "EUR/2", + }, + { + Source: fmt.Sprintf("teller:%d", i), + Destination: fmt.Sprintf("trades:%d", i), + Amount: core.NewMonetaryInt(350000), + Asset: "RBLX/6", + }, + { + Source: fmt.Sprintf("trades:%d", i), + Destination: fmt.Sprintf("users:%d:wallet", i), + Amount: core.NewMonetaryInt(350000), + Asset: "RBLX/6", + }, + }, + }, + { + Postings: core.Postings{ + { + Source: fmt.Sprintf("users:%d:wallet", i), + Destination: fmt.Sprintf("trades:%d", i), + Amount: core.NewMonetaryInt(4230), + Asset: "EUR/2", + }, + { + Source: fmt.Sprintf("trades:%d", i), + Destination: fmt.Sprintf("fiat:holdings:%d", i), + Amount: core.NewMonetaryInt(4230), + Asset: "EUR/2", + }, + { + Source: fmt.Sprintf("teller:%d", i), + Destination: fmt.Sprintf("trades:%d", i), + Amount: core.NewMonetaryInt(1840000), + Asset: "SNAP/6", + }, + { + Source: fmt.Sprintf("trades:%d", i), + Destination: fmt.Sprintf("users:%d:wallet", i), + Amount: core.NewMonetaryInt(1840000), + Asset: "SNAP/6", + }, + }, + }, + { + Postings: core.Postings{ + { + Source: fmt.Sprintf("users:%d:wallet", i), + Destination: fmt.Sprintf("users:%d:withdrawals", i), + Amount: core.NewMonetaryInt(2270), + Asset: "EUR/2", + }, + }, + }, + { + Postings: core.Postings{ + { + Source: fmt.Sprintf("users:%d:withdrawals", i), + Destination: fmt.Sprintf("payouts:%d", i), + Amount: core.NewMonetaryInt(2270), + Asset: "EUR/2", + }, + }, + }, + } +} diff --git a/pkg/ledger/execute_script_test.go b/pkg/ledger/execute_script_test.go index 722bc2195..349cf4922 100644 --- a/pkg/ledger/execute_script_test.go +++ b/pkg/ledger/execute_script_test.go @@ -834,43 +834,6 @@ func TestNewMachineFromScript(t *testing.T) { }) } -var execRes []core.ExpandedTransaction -var execResScript core.ExpandedTransaction - -const nbPostings = 1000 - -func BenchmarkLedger_PostTransactions_Scripts_Single_FixedAccounts(b *testing.B) { - runOnLedger(func(l *ledger.Ledger) { - txData := core.TransactionData{} - for i := 0; i < nbPostings; i++ { - txData.Postings = append(txData.Postings, core.Posting{ - Source: "world", - Destination: "benchmarks:" + strconv.Itoa(i), - Asset: "COIN", - Amount: core.NewMonetaryInt(10), - }) - } - script := txToScriptData(txData) - - b.ResetTimer() - - res := core.ExpandedTransaction{} - - for n := 0; n < b.N; n++ { - _, err := txData.Postings.Validate() - require.NoError(b, err) - res, err = l.ExecuteScript(context.Background(), true, script) - require.NoError(b, err) - require.Len(b, res, 1) - require.Len(b, res.Postings, nbPostings) - } - - execResScript = res - require.Len(b, execRes, 1) - require.Len(b, execRes[0].Postings, nbPostings) - }) -} - type variable struct { name string jsonVal json.RawMessage diff --git a/pkg/ledger/execute_txsdata_test.go b/pkg/ledger/execute_txsdata_test.go index 9e82a371e..9057e4a44 100644 --- a/pkg/ledger/execute_txsdata_test.go +++ b/pkg/ledger/execute_txsdata_test.go @@ -2,8 +2,6 @@ package ledger_test import ( "context" - "fmt" - "strconv" "testing" "time" @@ -357,228 +355,3 @@ func TestLedger_ExecuteTxsData(t *testing.T) { }) }, ledger.WithPastTimestamps) } - -func BenchmarkLedger_PostTransactions_Postings_Single_FixedAccounts(b *testing.B) { - runOnLedger(func(l *ledger.Ledger) { - txData := core.TransactionData{} - for i := 0; i < nbPostings; i++ { - txData.Postings = append(txData.Postings, core.Posting{ - Source: "world", - Destination: "benchmarks:" + strconv.Itoa(i), - Asset: "COIN", - Amount: core.NewMonetaryInt(10), - }) - } - - b.ResetTimer() - - res := []core.ExpandedTransaction{} - - for n := 0; n < b.N; n++ { - _, err := txData.Postings.Validate() - require.NoError(b, err) - res, err = l.ExecuteTxsData(context.Background(), true, txData) - require.NoError(b, err) - require.Len(b, res, 1) - require.Len(b, res[0].Postings, nbPostings) - } - - execRes = res - require.Len(b, execRes, 1) - require.Len(b, execRes[0].Postings, nbPostings) - }) -} - -func BenchmarkLedger_PostTransactions_Postings_Batch_FixedAccounts(b *testing.B) { - runOnLedger(func(l *ledger.Ledger) { - txsData := newTxsData(1) - - b.ResetTimer() - - res := []core.ExpandedTransaction{} - - for n := 0; n < b.N; n++ { - var err error - for _, txData := range txsData { - _, err := txData.Postings.Validate() - require.NoError(b, err) - } - res, err = l.ExecuteTxsData(context.Background(), true, txsData...) - require.NoError(b, err) - require.Len(b, res, 7) - require.Len(b, res[0].Postings, 1) - require.Len(b, res[1].Postings, 1) - require.Len(b, res[2].Postings, 2) - require.Len(b, res[3].Postings, 4) - require.Len(b, res[4].Postings, 4) - require.Len(b, res[5].Postings, 1) - require.Len(b, res[6].Postings, 1) - } - - execRes = res - require.Len(b, execRes, 7) - require.Len(b, execRes[0].Postings, 1) - require.Len(b, execRes[1].Postings, 1) - require.Len(b, execRes[2].Postings, 2) - require.Len(b, execRes[3].Postings, 4) - require.Len(b, execRes[4].Postings, 4) - require.Len(b, execRes[5].Postings, 1) - require.Len(b, execRes[6].Postings, 1) - }) -} - -func BenchmarkLedger_PostTransactions_Postings_Batch_VaryingAccounts(b *testing.B) { - runOnLedger(func(l *ledger.Ledger) { - b.ResetTimer() - - res := []core.ExpandedTransaction{} - - for n := 0; n < b.N; n++ { - b.StopTimer() - txsData := newTxsData(n) - b.StartTimer() - var err error - for _, txData := range txsData { - _, err := txData.Postings.Validate() - require.NoError(b, err) - } - res, err = l.ExecuteTxsData(context.Background(), true, txsData...) - require.NoError(b, err) - require.Len(b, res, 7) - require.Len(b, res[0].Postings, 1) - require.Len(b, res[1].Postings, 1) - require.Len(b, res[2].Postings, 2) - require.Len(b, res[3].Postings, 4) - require.Len(b, res[4].Postings, 4) - require.Len(b, res[5].Postings, 1) - require.Len(b, res[6].Postings, 1) - } - - execRes = res - require.Len(b, execRes, 7) - require.Len(b, execRes[0].Postings, 1) - require.Len(b, execRes[1].Postings, 1) - require.Len(b, execRes[2].Postings, 2) - require.Len(b, execRes[3].Postings, 4) - require.Len(b, execRes[4].Postings, 4) - require.Len(b, execRes[5].Postings, 1) - require.Len(b, execRes[6].Postings, 1) - }) -} - -func newTxsData(i int) []core.TransactionData { - return []core.TransactionData{ - { - Postings: core.Postings{ - { - Source: "world", - Destination: fmt.Sprintf("payins:%d", i), - Amount: core.NewMonetaryInt(10000), - Asset: "EUR/2", - }, - }, - }, - { - Postings: core.Postings{ - { - Source: fmt.Sprintf("payins:%d", i), - Destination: fmt.Sprintf("users:%d:wallet", i), - Amount: core.NewMonetaryInt(10000), - Asset: "EUR/2", - }, - }, - }, - { - Postings: core.Postings{ - { - Source: "world", - Destination: fmt.Sprintf("teller:%d", i), - Amount: core.NewMonetaryInt(350000), - Asset: "RBLX/6", - }, - { - Source: "world", - Destination: fmt.Sprintf("teller:%d", i), - Amount: core.NewMonetaryInt(1840000), - Asset: "SNAP/6", - }, - }, - }, - { - Postings: core.Postings{ - { - Source: fmt.Sprintf("users:%d:wallet", i), - Destination: fmt.Sprintf("trades:%d", i), - Amount: core.NewMonetaryInt(1500), - Asset: "EUR/2", - }, - { - Source: fmt.Sprintf("trades:%d", i), - Destination: fmt.Sprintf("fiat:holdings:%d", i), - Amount: core.NewMonetaryInt(1500), - Asset: "EUR/2", - }, - { - Source: fmt.Sprintf("teller:%d", i), - Destination: fmt.Sprintf("trades:%d", i), - Amount: core.NewMonetaryInt(350000), - Asset: "RBLX/6", - }, - { - Source: fmt.Sprintf("trades:%d", i), - Destination: fmt.Sprintf("users:%d:wallet", i), - Amount: core.NewMonetaryInt(350000), - Asset: "RBLX/6", - }, - }, - }, - { - Postings: core.Postings{ - { - Source: fmt.Sprintf("users:%d:wallet", i), - Destination: fmt.Sprintf("trades:%d", i), - Amount: core.NewMonetaryInt(4230), - Asset: "EUR/2", - }, - { - Source: fmt.Sprintf("trades:%d", i), - Destination: fmt.Sprintf("fiat:holdings:%d", i), - Amount: core.NewMonetaryInt(4230), - Asset: "EUR/2", - }, - { - Source: fmt.Sprintf("teller:%d", i), - Destination: fmt.Sprintf("trades:%d", i), - Amount: core.NewMonetaryInt(1840000), - Asset: "SNAP/6", - }, - { - Source: fmt.Sprintf("trades:%d", i), - Destination: fmt.Sprintf("users:%d:wallet", i), - Amount: core.NewMonetaryInt(1840000), - Asset: "SNAP/6", - }, - }, - }, - { - Postings: core.Postings{ - { - Source: fmt.Sprintf("users:%d:wallet", i), - Destination: fmt.Sprintf("users:%d:withdrawals", i), - Amount: core.NewMonetaryInt(2270), - Asset: "EUR/2", - }, - }, - }, - { - Postings: core.Postings{ - { - Source: fmt.Sprintf("users:%d:withdrawals", i), - Destination: fmt.Sprintf("payouts:%d", i), - Amount: core.NewMonetaryInt(2270), - Asset: "EUR/2", - }, - }, - }, - } -}