forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmemory_usage_test.go
114 lines (89 loc) · 3.27 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 bbto.Destroy()
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)
// A single Put is not enough to increase approximate memtable usage.
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)
}
func TestMemoryUsageTransactionDB(t *testing.T) {
// create database with cache
cache := NewLRUCache(8 * 1024 * 1024)
bbto := NewDefaultBlockBasedTableOptions()
bbto.SetBlockCache(cache)
defer bbto.Destroy()
defer cache.Destroy()
applyOpts := func(opts *Options, transactionDBOpts *TransactionDBOptions) {
opts.SetBlockBasedTableFactory(bbto)
}
db := newTestTransactionDB(t, "TestMemoryUsage", applyOpts)
defer db.Close()
// take first memory usage snapshot
mu1, err := GetApproximateMemoryUsageByTypeNativeDB([]NativeDB{db}, []*Cache{cache})
ensure.Nil(t, err)
// perforx`m 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)
// A single Put is not enough to increase approximate memtable usage.
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 := GetApproximateMemoryUsageByTypeNativeDB([]NativeDB{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)
}