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.

75 lines
1.7 KiB

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