Initial hacky version of inspect source command

This commit is contained in:
2018-07-13 01:56:17 +01:00
parent 7cf43c59dd
commit eff9aa36f6
10 changed files with 309 additions and 635 deletions

View File

@@ -1,39 +1,25 @@
package cmd
import (
"fmt"
"github.com/jimeh/rbheap/inspect"
"github.com/spf13/cobra"
)
// inspectCmd represents the inspect command
var inspectCmd = &cobra.Command{
Use: "inspect [flags] <dump-file>",
Use: "inspect",
Short: "Inspect ObjectSpace dumps from Ruby proceees",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
usage_er(cmd, fmt.Sprintf("requires 1 arg, received %d", len(args)))
}
inspector := inspect.New(args[0])
inspector.Verbose = inspectOpts.Verbose
inspector.Process()
inspector.PrintCountByFileAndLines()
},
}
var inspectOpts = struct {
Verbose bool
Output string
}{}
func init() {
rootCmd.AddCommand(inspectCmd)
inspectCmd.PersistentFlags().BoolVarP(
&inspectOpts.Verbose,
inspectCmd.PersistentFlags().BoolVarP(&inspectOpts.Verbose,
"verbose", "v", false,
"print verbose information",
"print verbose information to STDERR",
)
}

60
cmd/inspectSource.go Normal file
View File

@@ -0,0 +1,60 @@
package cmd
import (
"fmt"
"os"
"github.com/jimeh/rbheap/inspect"
"github.com/spf13/cobra"
)
// inspectSourceCmd represents the inspectSource command
var inspectSourceCmd = &cobra.Command{
Use: "source [flags] <dump-file>",
Short: "Group objects by source filename and line number",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
usage_er(cmd, fmt.Sprintf("requires 1 arg, received %d", len(args)))
}
inspector := inspect.NewSourceInspector(args[0])
inspector.Verbose = inspectOpts.Verbose
inspector.SortBy = inspectSourceOpts.SortBy
inspector.Limit = inspectSourceOpts.Limit
inspector.Load()
switch inspectSourceOpts.Breakdown {
case "file":
inspector.ByFile(os.Stdout)
case "line":
inspector.ByLine(os.Stdout)
default:
usage_er(cmd, "Invalid --breakdown option")
}
},
}
var inspectSourceOpts = struct {
Breakdown string
SortBy string
Limit int
}{}
func init() {
inspectCmd.AddCommand(inspectSourceCmd)
inspectSourceCmd.Flags().StringVarP(&inspectSourceOpts.Breakdown,
"breakdown", "b", "line",
"Breakdown sources by \"line\" or \"file\"",
)
inspectSourceCmd.Flags().StringVarP(&inspectSourceOpts.SortBy,
"sort", "s", "count",
"Sort by \"count\", \"memsize\", or \"bytesize\"",
)
inspectSourceCmd.Flags().IntVarP(&inspectSourceOpts.Limit,
"limit", "l", 0,
"Limit number of results to show",
)
}