tendermint/state/txindex/kv/kv_test.go

67 lines
1.7 KiB
Go
Raw Normal View History

2017-04-18 16:29:02 -07:00
package kv
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/abci/types"
2017-04-18 16:29:02 -07:00
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
db "github.com/tendermint/tmlibs/db"
)
2017-04-18 16:29:02 -07:00
func TestTxIndex(t *testing.T) {
indexer := &TxIndex{store: db.NewMemDB()}
tx := types.Tx("HELLO WORLD")
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}}
hash := tx.Hash()
batch := txindex.NewBatch(1)
2017-04-18 17:10:02 -07:00
batch.Add(*txResult)
2017-04-18 16:29:02 -07:00
err := indexer.AddBatch(batch)
require.Nil(t, err)
2017-04-18 16:29:02 -07:00
loadedTxResult, err := indexer.Get(hash)
require.Nil(t, err)
assert.Equal(t, txResult, loadedTxResult)
}
2017-04-18 16:29:02 -07:00
func benchmarkTxIndex(txsCount int, b *testing.B) {
tx := types.Tx("HELLO WORLD")
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}}
2017-04-18 17:55:40 -07:00
dir, err := ioutil.TempDir("", "tx_index_db")
if err != nil {
b.Fatal(err)
}
defer os.RemoveAll(dir)
2017-04-18 17:55:40 -07:00
store := db.NewDB("tx_index", "leveldb", dir)
2017-04-18 16:29:02 -07:00
indexer := &TxIndex{store: store}
batch := txindex.NewBatch(txsCount)
for i := 0; i < txsCount; i++ {
2017-04-18 17:10:02 -07:00
batch.Add(*txResult)
2017-06-27 13:05:21 -07:00
txResult.Index += 1
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
2017-06-27 13:05:21 -07:00
err = indexer.AddBatch(batch)
}
if err != nil {
b.Fatal(err)
}
}
2017-04-18 16:29:02 -07:00
func BenchmarkTxIndex1(b *testing.B) { benchmarkTxIndex(1, b) }
func BenchmarkTxIndex500(b *testing.B) { benchmarkTxIndex(500, b) }
func BenchmarkTxIndex1000(b *testing.B) { benchmarkTxIndex(1000, b) }
func BenchmarkTxIndex2000(b *testing.B) { benchmarkTxIndex(2000, b) }
func BenchmarkTxIndex10000(b *testing.B) { benchmarkTxIndex(10000, b) }