forked from HDT3213/rdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
121 lines (111 loc) · 2.85 KB
/
cmd.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
package main
import (
"flag"
"fmt"
"github.com/kuzznya/rdb/helper"
"os"
"strings"
)
const help = `
This is a tool to parse Redis' RDB files
Options:
-c command, including: json/memory/aof/bigkey/flamegraph
-o output file path
-n number of result, using in
-port listen port for flame graph web service
-sep separator for flamegraph, rdb will separate key by it, default value is ":".
supporting multi separators: -sep sep1 -sep sep2
-regex using regex expression filter keys
-no-expired filter expired keys
Examples:
parameters between '[' and ']' is optional
1. convert rdb to json
rdb -c json -o dump.json dump.rdb
2. generate memory report
rdb -c memory -o memory.csv dump.rdb
3. convert to aof file
rdb -c aof -o dump.aof dump.rdb
4. get largest keys
rdb -c bigkey [-o dump.aof] [-n 10] dump.rdb
5. draw flamegraph
rdb -c flamegraph [-port 16379] [-sep :] dump.rdb
`
type separators []string
func (s *separators) String() string {
return strings.Join(*s, " ")
}
func (s *separators) Set(value string) error {
*s = append(*s, value)
return nil
}
func main() {
flagSet := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
var cmd string
var output string
var n int
var port int
var seps separators
var regexExpr string
var noExpired bool
flagSet.StringVar(&cmd, "c", "", "command for rdb: json")
flagSet.StringVar(&output, "o", "", "output file path")
flagSet.IntVar(&n, "n", 0, "")
flagSet.IntVar(&port, "port", 0, "listen port for web")
flagSet.Var(&seps, "sep", "separator for flame graph")
flagSet.StringVar(®exExpr, "regex", "", "regex expression")
flagSet.BoolVar(&noExpired, "no-expired", false, "filter expired keys")
_ = flagSet.Parse(os.Args[1:]) // ExitOnError
src := flagSet.Arg(0)
if cmd == "" {
println(help)
return
}
if src == "" {
println("src file is required")
return
}
var options []interface{}
if regexExpr != "" {
options = append(options, helper.WithRegexOption(regexExpr))
}
if noExpired {
options = append(options, helper.WithNoExpiredOption())
}
var err error
switch cmd {
case "json":
err = helper.ToJsons(src, output, options...)
case "memory":
err = helper.MemoryProfile(src, output, options...)
case "aof":
err = helper.ToAOF(src, output, options)
case "bigkey":
if output == "" {
err = helper.FindBiggestKeys(src, n, os.Stdout, options...)
} else {
var outputFile *os.File
outputFile, err = os.Create(output)
if err != nil {
fmt.Printf("open output faild: %v", err)
}
defer func() {
_ = outputFile.Close()
}()
err = helper.FindBiggestKeys(src, n, outputFile, options...)
}
case "flamegraph":
_, err = helper.FlameGraph(src, port, seps, options...)
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
<-make(chan struct{})
default:
println("unknown command")
return
}
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
}