You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.6 KiB

  1. package kv
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "fmt"
  6. "os"
  7. "testing"
  8. dbm "github.com/tendermint/tm-db"
  9. abci "github.com/tendermint/tendermint/abci/types"
  10. "github.com/tendermint/tendermint/internal/pubsub/query"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func BenchmarkTxSearch(b *testing.B) {
  14. dbDir, err := os.MkdirTemp("", "benchmark_tx_search_test")
  15. if err != nil {
  16. b.Errorf("failed to create temporary directory: %s", err)
  17. }
  18. db, err := dbm.NewGoLevelDB("benchmark_tx_search_test", dbDir)
  19. if err != nil {
  20. b.Errorf("failed to create database: %s", err)
  21. }
  22. indexer := NewTxIndex(db)
  23. for i := 0; i < 35000; i++ {
  24. events := []abci.Event{
  25. {
  26. Type: "transfer",
  27. Attributes: []abci.EventAttribute{
  28. {Key: "address", Value: fmt.Sprintf("address_%d", i%100), Index: true},
  29. {Key: "amount", Value: "50", Index: true},
  30. },
  31. },
  32. }
  33. txBz := make([]byte, 8)
  34. if _, err := rand.Read(txBz); err != nil {
  35. b.Errorf("failed produce random bytes: %s", err)
  36. }
  37. txResult := &abci.TxResult{
  38. Height: int64(i),
  39. Index: 0,
  40. Tx: types.Tx(string(txBz)),
  41. Result: abci.ResponseDeliverTx{
  42. Data: []byte{0},
  43. Code: abci.CodeTypeOK,
  44. Log: "",
  45. Events: events,
  46. },
  47. }
  48. if err := indexer.Index([]*abci.TxResult{txResult}); err != nil {
  49. b.Errorf("failed to index tx: %s", err)
  50. }
  51. }
  52. txQuery := query.MustCompile(`transfer.address = 'address_43' AND transfer.amount = 50`)
  53. b.ResetTimer()
  54. ctx := context.Background()
  55. for i := 0; i < b.N; i++ {
  56. if _, err := indexer.Search(ctx, txQuery); err != nil {
  57. b.Errorf("failed to query for txs: %s", err)
  58. }
  59. }
  60. }