|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/spf13/cobra" |
| 6 | + "github.com/vespa-engine/vespa/client/go/internal/vespa/slime" |
| 7 | + "os" |
| 8 | +) |
| 9 | + |
| 10 | +type inspectProfileOptions struct { |
| 11 | + profileFile string |
| 12 | +} |
| 13 | + |
| 14 | +func inspectProfile(cli *CLI, opts *inspectProfileOptions) error { |
| 15 | + file, err := os.Open(opts.profileFile) |
| 16 | + if err != nil { |
| 17 | + return fmt.Errorf("failed to open profile file '%s': %w", opts.profileFile, err) |
| 18 | + } |
| 19 | + defer file.Close() |
| 20 | + root := slime.DecodeJson(file) |
| 21 | + if !root.Valid() { |
| 22 | + return fmt.Errorf("profile file '%s' does not contain valid JSON", opts.profileFile) |
| 23 | + } |
| 24 | + trace := root.Field("trace") |
| 25 | + slime.EncodeJson(trace, false, cli.Stdout) |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +func newInspectProfileCmd(cli *CLI) *cobra.Command { |
| 30 | + opts := inspectProfileOptions{} |
| 31 | + |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "profile", |
| 34 | + Short: "Inspect profiling results", |
| 35 | + Long: `Inspect profiling results previously obtained by vespa query --profile |
| 36 | +
|
| 37 | +Note: this feature is experimental and currently under development |
| 38 | +profiling results can also be analyzed with vespa-query-analyzer (part of vespa installation)`, |
| 39 | + |
| 40 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 41 | + return inspectProfile(cli, &opts) |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + cmd.Flags().StringVarP(&opts.profileFile, "profile-file", "f", "vespa_query_profile_result.json", "Name of the profile file to inspect") |
| 46 | + return cmd |
| 47 | +} |
| 48 | + |
| 49 | +func newInspectCmd(cli *CLI) *cobra.Command { |
| 50 | + cmd := &cobra.Command{ |
| 51 | + Use: "inspect", |
| 52 | + Short: "Provides insight", |
| 53 | + Long: "Provides subcommands to inspect various things in more detail", |
| 54 | + } |
| 55 | + cmd.AddCommand(newInspectProfileCmd(cli)) |
| 56 | + return cmd |
| 57 | +} |
0 commit comments