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.

72 lines
1.7 KiB

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