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. dbm "github.com/tendermint/tm-db"
  9. abci "github.com/tendermint/tendermint/abci/types"
  10. "github.com/tendermint/tendermint/libs/pubsub/query"
  11. "github.com/tendermint/tendermint/types"
  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: []abci.EventAttribute{
  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. ctx := context.Background()
  56. for i := 0; i < b.N; i++ {
  57. if _, err := indexer.Search(ctx, txQuery); err != nil {
  58. b.Errorf("failed to query for txs: %s", err)
  59. }
  60. }
  61. }