Skip to content

Commit

Permalink
Merge pull request #16 from LawrencePeng/master
Browse files Browse the repository at this point in the history
添加Dump到STDOUT的功能
  • Loading branch information
Dong Mingxin authored Sep 9, 2018
2 parents 3a5cdcc + 7666352 commit b194126
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/elazarl/go-bindata-assetfs"
"github.com/julienschmidt/httprouter"
"github.com/xueqiu/rdr/static"
"encoding/json"
)

//go:generate go-bindata -prefix "static/" -o=static/static.go -pkg=static -ignore static.go static/...
Expand All @@ -50,6 +51,71 @@ func decode(c *cli.Context, decoder *Decoder, filepath string) {

var counters = NewSafeMap()

func getData(filename string, cnt *Counter) map[string]interface{} {
data := make(map[string]interface{})
data["CurrentInstance"] = filename
data["LargestKeys"] = cnt.GetLargestEntries(100)

largestKeyPrefixesByType := map[string][]*PrefixEntry{}
for _, entry := range cnt.GetLargestKeyPrefixes() {
// if mem usage is less than 1M, and the list is long enough, then it's unnecessary to add it.
if entry.Bytes < 1000*1000 && len(largestKeyPrefixesByType[entry.Type]) > 50 {
continue
}
largestKeyPrefixesByType[entry.Type] = append(largestKeyPrefixesByType[entry.Type], entry)
}
data["LargestKeyPrefixes"] = largestKeyPrefixesByType

data["TypeBytes"] = cnt.typeBytes
data["TypeNum"] = cnt.typeNum
totalNum := uint64(0)
for _, v := range cnt.typeNum {
totalNum += v
}
totalBytes := uint64(0)
for _, v := range cnt.typeBytes {
totalBytes += v
}
data["TotleNum"] = totalNum
data["TotleBytes"] = totalBytes

lenLevelCount := map[string][]*PrefixEntry{}
for _, entry := range cnt.GetLenLevelCount() {
lenLevelCount[entry.Type] = append(lenLevelCount[entry.Type], entry)
}
data["LenLevelCount"] = lenLevelCount
return data
}

// dump rdb file statistical information to STDOUT.
func dump(cli *cli.Context) {
if cli.NArg() < 1 {
fmt.Fprintln(cli.App.ErrWriter, " requires at least 1 argument")
return
}

// parse rdbfile
fmt.Fprintln(cli.App.Writer, "[")
nargs := cli.NArg()
for i := 0; i < nargs; i++ {
file := cli.Args().Get(i)
decoder := NewDecoder()
go decode(cli, decoder, file)
cnt := NewCounter()
cnt.Count(decoder.Entries)
filename := filepath.Base(file)
data := getData(filename, cnt)
jsonBytes, _ := json.MarshalIndent(data, "", " ")
fmt.Fprint(cli.App.Writer, string(jsonBytes))
if i == nargs-1 {
fmt.Fprintln(cli.App.Writer)
} else {
fmt.Fprintln(cli.App.Writer, ",")
}
}
fmt.Fprintln(cli.App.Writer, "]")
}

// show parse rdbfile(s) and show statistical information by html
func show(c *cli.Context) {
if c.NArg() < 1 {
Expand Down Expand Up @@ -119,6 +185,12 @@ func main() {
app.Writer = os.Stdout
app.ErrWriter = os.Stderr
app.Commands = []cli.Command{
cli.Command{
Name: "dump",
Usage: "dump statistical information of rdbfile to STDOUT",
ArgsUsage: "FILE1 [FILE2] [FILE3]...",
Action: dump,
},
cli.Command{
Name: "show",
Usage: "show statistical information of rdbfile by webpage",
Expand All @@ -145,3 +217,4 @@ func main() {
}
app.Run(os.Args)
}

0 comments on commit b194126

Please sign in to comment.