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.

63 lines
1.7 KiB

8 years ago
  1. package kv
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. abci "github.com/tendermint/abci/types"
  9. db "github.com/tendermint/tmlibs/db"
  10. "github.com/tendermint/tendermint/state/txindex"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func TestTxIndex(t *testing.T) {
  14. indexer := &TxIndex{store: db.NewMemDB()}
  15. tx := types.Tx("HELLO WORLD")
  16. txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}}
  17. hash := tx.Hash()
  18. batch := txindex.NewBatch(1)
  19. batch.Add(*txResult)
  20. err := indexer.AddBatch(batch)
  21. require.Nil(t, err)
  22. loadedTxResult, err := indexer.Get(hash)
  23. require.Nil(t, err)
  24. assert.Equal(t, txResult, loadedTxResult)
  25. }
  26. func benchmarkTxIndex(txsCount int, b *testing.B) {
  27. tx := types.Tx("HELLO WORLD")
  28. txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}}
  29. dir, err := ioutil.TempDir("", "tx_index_db")
  30. if err != nil {
  31. b.Fatal(err)
  32. }
  33. defer os.RemoveAll(dir)
  34. store := db.NewDB("tx_index", "leveldb", dir)
  35. indexer := &TxIndex{store: store}
  36. batch := txindex.NewBatch(txsCount)
  37. for i := 0; i < txsCount; i++ {
  38. txResult.Index += 1
  39. batch.Add(*txResult)
  40. }
  41. b.ResetTimer()
  42. for n := 0; n < b.N; n++ {
  43. err = indexer.AddBatch(batch)
  44. }
  45. }
  46. func BenchmarkTxIndex1(b *testing.B) { benchmarkTxIndex(1, b) }
  47. func BenchmarkTxIndex500(b *testing.B) { benchmarkTxIndex(500, b) }
  48. func BenchmarkTxIndex1000(b *testing.B) { benchmarkTxIndex(1000, b) }
  49. func BenchmarkTxIndex2000(b *testing.B) { benchmarkTxIndex(2000, b) }
  50. func BenchmarkTxIndex10000(b *testing.B) { benchmarkTxIndex(10000, b) }