-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathanalysis_rdb.go
221 lines (203 loc) · 4.9 KB
/
analysis_rdb.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package gorma
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/hhxsv5/go-redis-memory-analysis/storages"
"github.com/vrischmann/rdbtools"
)
type AnalysisRDB struct {
rdb *os.File
Reports DBReports
}
func NewAnalysisRDB(rdb string) (*AnalysisRDB, error) {
fp, err := os.Open(rdb)
if err != nil {
return nil, err
}
return &AnalysisRDB{fp, DBReports{}}, nil
}
func (analysis *AnalysisRDB) Close() {
if analysis.rdb != nil {
_ = analysis.rdb.Close()
}
}
func (analysis AnalysisRDB) Start(delimiters []string) {
fmt.Println("Starting analysis")
var (
r Report
sr SortByCountReports
mr KeyReports
cdb = -1
)
ctx := rdbtools.ParserContext{
DbCh: make(chan int),
StringObjectCh: make(chan rdbtools.StringObject),
ListMetadataCh: make(chan rdbtools.ListMetadata),
ListDataCh: make(chan interface{}),
SetMetadataCh: make(chan rdbtools.SetMetadata),
SetDataCh: make(chan interface{}),
HashMetadataCh: make(chan rdbtools.HashMetadata),
HashDataCh: make(chan rdbtools.HashEntry),
SortedSetMetadataCh: make(chan rdbtools.SortedSetMetadata),
SortedSetEntriesCh: make(chan rdbtools.SortedSetEntry),
}
p := rdbtools.NewParser(ctx)
go func() {
analyze := func(ko rdbtools.KeyObject) {
key := rdbtools.DataToString(ko.Key)
fd, fp, tmp, nk := "", 0, 0, ""
for _, delimiter := range delimiters {
tmp = strings.Index(key, delimiter)
if tmp != -1 && (tmp < fp || fp == 0) {
fd, fp = delimiter, tmp
}
}
if fp == 0 {
return
}
nk = key[0:fp] + fd + "*"
if _, ok := mr[nk]; ok {
r = mr[nk]
} else {
r = Report{nk, 0, 0, 0, 0}
}
if ko.ExpiryTime.IsZero() {
r.NeverExpire++
} else {
if ko.Expired() {
//Ignore
} else {
ff := float64(r.AvgTtl*(r.Count-r.NeverExpire)+uint64(ko.ExpiryTime.Unix()-time.Now().Unix())) / float64(r.Count+1-r.NeverExpire)
ttl, _ := strconv.ParseUint(fmt.Sprintf("%0.0f", ff), 10, 64)
r.AvgTtl = ttl
}
}
r.Count++
mr[nk] = r
}
for {
select {
case t, ok := <-ctx.DbCh:
if cdb >= 0 {
//Sort by size
sr = SortByCountReports{}
for _, report := range mr {
sr = append(sr, report)
}
sort.Sort(sr)
analysis.Reports[uint64(cdb)] = sr
}
if !ok {
ctx.DbCh = nil
break
}
fmt.Println("Analyzing db", t)
cdb = t
mr = KeyReports{}
case t, ok := <-ctx.StringObjectCh:
if !ok {
ctx.StringObjectCh = nil
break
}
analyze(t.Key)
//fmt.Println("string object", t)
case t, ok := <-ctx.ListMetadataCh:
if !ok {
ctx.ListMetadataCh = nil
break
}
analyze(t.Key)
//fmt.Println("list meta data", t)
case _, ok := <-ctx.ListDataCh:
if !ok {
ctx.ListDataCh = nil
break
}
//fmt.Println("list data", rdbtools.DataToString(t))
case t, ok := <-ctx.SetMetadataCh:
if !ok {
ctx.SetMetadataCh = nil
break
}
analyze(t.Key)
//fmt.Println("set meta data", t)
case _, ok := <-ctx.SetDataCh:
if !ok {
ctx.SetDataCh = nil
break
}
//fmt.Println("set data", rdbtools.DataToString(t))
case t, ok := <-ctx.HashMetadataCh:
if !ok {
ctx.HashMetadataCh = nil
break
}
analyze(t.Key)
//fmt.Println("hash meta data", t)
case _, ok := <-ctx.HashDataCh:
if !ok {
ctx.HashDataCh = nil
break
}
//fmt.Println("hash data", t)
case t, ok := <-ctx.SortedSetMetadataCh:
if !ok {
ctx.SortedSetMetadataCh = nil
break
}
analyze(t.Key)
//fmt.Println("sorted set meta data", t)
case _, ok := <-ctx.SortedSetEntriesCh:
if !ok {
ctx.SortedSetEntriesCh = nil
break
}
//fmt.Println("sorted set entries", t)
}
if ctx.Invalid() {
break
}
}
//fmt.Println(analysis.Reports)
}()
if err := p.Parse(analysis.rdb); err != nil {
panic(err)
}
}
func (analysis AnalysisRDB) SaveReports(folder string) error {
fmt.Println("Saving the results of the analysis into", folder)
if _, err := os.Stat(folder); os.IsNotExist(err) {
_ = os.MkdirAll(folder, os.ModePerm)
}
var (
str string
filename string
size float64
unit string
)
template := fmt.Sprintf("%s%sredis-analysis-%s%s", folder, string(os.PathSeparator), strings.Replace(analysis.rdb.Name(), "/", "-", -1), "-%d.csv")
for db, reports := range analysis.Reports {
filename = fmt.Sprintf(template, db)
fp, err := storages.NewFile(filename, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return err
}
_, _ = fp.Append([]byte("Key,Count,Size,NeverExpire,AvgTtl(excluded never expire)\n"))
for _, value := range reports {
size, unit = HumanSize(value.Size)
str = fmt.Sprintf("%s,%d,%s,%d,%d\n",
value.Key,
value.Count,
fmt.Sprintf("%0.3f %s", size, unit),
value.NeverExpire,
value.AvgTtl)
_, _ = fp.Append([]byte(str))
}
fp.Close()
}
return nil
}