forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemory_test.go
116 lines (103 loc) · 3.32 KB
/
memory_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
115
116
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package executor_test
import (
"context"
"fmt"
"runtime"
"runtime/debug"
"testing"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)
func TestPBMemoryLeak(t *testing.T) {
debug.SetGCPercent(1000)
defer debug.SetGCPercent(100)
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database test_mem")
tk.MustExec("use test_mem")
// prepare data
totalSize := uint64(256 << 20) // 256MB
blockSize := uint64(8 << 10) // 8KB
delta := totalSize / 5
numRows := totalSize / blockSize
tk.MustExec(fmt.Sprintf("create table t (c varchar(%v))", blockSize))
sql := fmt.Sprintf("insert into t values (space(%v))", blockSize)
for i := uint64(0); i < numRows; i++ {
tk.MustExec(sql)
}
// read data
runtime.GC()
allocatedBegin, inUseBegin := readMem()
records, err := tk.Session().Execute(context.Background(), "select * from t")
require.NoError(t, err)
record := records[0]
rowCnt := 0
chk := record.NewChunk(nil)
for {
require.NoError(t, record.Next(context.Background(), chk))
rowCnt += chk.NumRows()
if chk.NumRows() == 0 {
break
}
}
require.Equal(t, int(numRows), rowCnt)
// check memory before close
runtime.GC()
allocatedAfter, inUseAfter := readMem()
require.GreaterOrEqual(t, allocatedAfter-allocatedBegin, totalSize)
require.Less(t, memDiff(inUseAfter, inUseBegin), delta)
runtime.GC()
allocatedFinal, inUseFinal := readMem()
require.Less(t, allocatedFinal-allocatedAfter, delta)
require.Less(t, memDiff(inUseFinal, inUseAfter), delta)
}
// nolint:unused
func readMem() (allocated, heapInUse uint64) {
var stat runtime.MemStats
runtime.ReadMemStats(&stat)
return stat.TotalAlloc, stat.HeapInuse
}
// nolint:unused
func memDiff(m1, m2 uint64) uint64 {
if m1 > m2 {
return m1 - m2
}
return m2 - m1
}
func TestGlobalMemoryTrackerOnCleanUp(t *testing.T) {
// TODO: assert the memory consume has happened in another way
originConsume := executor.GlobalMemoryUsageTracker.BytesConsumed()
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int)")
// assert insert
tk.MustExec("insert t (id) values (1)")
tk.MustExec("insert t (id) values (2)")
tk.MustExec("insert t (id) values (3)")
afterConsume := executor.GlobalMemoryUsageTracker.BytesConsumed()
require.Equal(t, afterConsume, originConsume)
// assert update
tk.MustExec("update t set id = 4 where id = 1")
tk.MustExec("update t set id = 5 where id = 2")
tk.MustExec("update t set id = 6 where id = 3")
afterConsume = executor.GlobalMemoryUsageTracker.BytesConsumed()
require.Equal(t, afterConsume, originConsume)
}