-
Notifications
You must be signed in to change notification settings - Fork 271
/
memory_usage_test.go
56 lines (44 loc) · 1.51 KB
/
memory_usage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package gorocksdb
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/facebookgo/ensure"
)
func TestMemoryUsage(t *testing.T) {
// create database with cache
cache := NewLRUCache(8 * 1024 * 1024)
bbto := NewDefaultBlockBasedTableOptions()
bbto.SetBlockCache(cache)
defer cache.Destroy()
applyOpts := func(opts *Options) {
opts.SetBlockBasedTableFactory(bbto)
}
db := newTestDB(t, "TestMemoryUsage", applyOpts)
defer db.Close()
// take first memory usage snapshot
mu1, err := GetApproximateMemoryUsageByType([]*DB{db}, []*Cache{cache})
ensure.Nil(t, err)
// perform IO operations that will affect in-memory tables (and maybe cache as well)
wo := NewDefaultWriteOptions()
defer wo.Destroy()
ro := NewDefaultReadOptions()
defer ro.Destroy()
key := []byte("key")
value := make([]byte, 1024)
_, err = rand.Read(value)
ensure.Nil(t, err)
err = db.Put(wo, key, value)
ensure.Nil(t, err)
_, err = db.Get(ro, key)
ensure.Nil(t, err)
// take second memory usage snapshot
mu2, err := GetApproximateMemoryUsageByType([]*DB{db}, []*Cache{cache})
ensure.Nil(t, err)
// the amount of memory used by memtables should increase after write/read;
// cache memory usage is not likely to be changed, perhaps because requested key is kept by memtable
assert.True(t, mu2.MemTableTotal > mu1.MemTableTotal)
assert.True(t, mu2.MemTableUnflushed > mu1.MemTableUnflushed)
assert.True(t, mu2.CacheTotal >= mu1.CacheTotal)
assert.True(t, mu2.MemTableReadersTotal >= mu1.MemTableReadersTotal)
}