mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
add test
This commit is contained in:
parent
0f528fcb70
commit
80b69b266b
1 changed files with 53 additions and 0 deletions
|
|
@ -15,3 +15,56 @@
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package rawdb
|
package rawdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupRangeDB(b *testing.B, db ethdb.KeyValueStore, numEntries int) ([]byte, []byte) {
|
||||||
|
b.Helper()
|
||||||
|
var firstKey, lastKey []byte
|
||||||
|
for i := 0; i < numEntries; i++ {
|
||||||
|
key := make([]byte, 32)
|
||||||
|
val := make([]byte, 64)
|
||||||
|
if _, err := rand.Read(key); err != nil {
|
||||||
|
b.Fatalf("Failed to generate random key: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := rand.Read(val); err != nil {
|
||||||
|
b.Fatalf("Failed to generate random value: %v", err)
|
||||||
|
}
|
||||||
|
hashKey := crypto.Keccak256Hash(key)
|
||||||
|
if err := db.Put(hashKey.Bytes(), val); err != nil {
|
||||||
|
b.Fatalf("Failed to put data: %v", err)
|
||||||
|
}
|
||||||
|
if i == 0 {
|
||||||
|
firstKey = hashKey.Bytes()
|
||||||
|
}
|
||||||
|
if i == numEntries-1 {
|
||||||
|
lastKey = hashKey.Bytes()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bytes.Compare(firstKey, lastKey) > 0 {
|
||||||
|
firstKey, lastKey = lastKey, firstKey
|
||||||
|
}
|
||||||
|
return firstKey, append(lastKey, 0) // append 0 to make range exclusive for lastKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkSafeDeleteRange(b *testing.B) {
|
||||||
|
db := NewMemoryDatabase()
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
const numEntries = 10000
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
start, end := setupRangeDB(b, db, numEntries)
|
||||||
|
if err := SafeDeleteRange(db, start, end, true, func(bool) bool { return false }); err != nil {
|
||||||
|
b.Fatalf("Failed to delete range: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue