From 77426154d5469500277f91e6c9d64c93b50afc6d Mon Sep 17 00:00:00 2001 From: ahh Date: Sat, 9 Mar 2019 11:36:59 -0500 Subject: [PATCH] Add option to disable browser in HTTP mode (#462) --- CONTRIBUTORS | 1 + internal/driver/cli.go | 38 +++++++++++++++++++++-------------- internal/driver/driver.go | 2 +- internal/driver/webui.go | 10 ++++++--- internal/driver/webui_test.go | 2 +- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 0ef5e2f24..8c8c37d2c 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -13,3 +13,4 @@ Tipp Moseley Hyoun Kyu Cho Martin Spier Taco de Wolff +Andrew Hunter diff --git a/internal/driver/cli.go b/internal/driver/cli.go index 345af417e..9fc1eea1f 100644 --- a/internal/driver/cli.go +++ b/internal/driver/cli.go @@ -32,11 +32,12 @@ type source struct { DiffBase bool Normalize bool - Seconds int - Timeout int - Symbolize string - HTTPHostport string - Comment string + Seconds int + Timeout int + Symbolize string + HTTPHostport string + HTTPDisableBrowser bool + Comment string } // parseFlags parses the command lines through the specified flags package @@ -65,7 +66,8 @@ func parseFlags(o *plugin.Options) (*source, []string, error) { flagMeanDelay := flag.Bool("mean_delay", false, "Display mean delay at each region") flagTools := flag.String("tools", os.Getenv("PPROF_TOOLS"), "Path for object tool pathnames") - flagHTTP := flag.String("http", "", "Present interactive web based UI at the specified http host:port") + flagHTTP := flag.String("http", "", "Present interactive web UI at the specified http host:port") + flagNoBrowser := flag.Bool("no_browser", false, "Skip opening a browswer for the interactive web UI") // Flags used during command processing installedFlags := installFlags(flag) @@ -118,6 +120,10 @@ func parseFlags(o *plugin.Options) (*source, []string, error) { return nil, nil, errors.New("-http is not compatible with an output format on the command line") } + if *flagNoBrowser && *flagHTTP == "" { + return nil, nil, errors.New("-no_browser only makes sense with -http") + } + si := pprofVariables["sample_index"].value si = sampleIndex(flagTotalDelay, si, "delay", "-total_delay", o.UI) si = sampleIndex(flagMeanDelay, si, "delay", "-mean_delay", o.UI) @@ -133,14 +139,15 @@ func parseFlags(o *plugin.Options) (*source, []string, error) { } source := &source{ - Sources: args, - ExecName: execName, - BuildID: *flagBuildID, - Seconds: *flagSeconds, - Timeout: *flagTimeout, - Symbolize: *flagSymbolize, - HTTPHostport: *flagHTTP, - Comment: *flagAddComment, + Sources: args, + ExecName: execName, + BuildID: *flagBuildID, + Seconds: *flagSeconds, + Timeout: *flagTimeout, + Symbolize: *flagSymbolize, + HTTPHostport: *flagHTTP, + HTTPDisableBrowser: *flagNoBrowser, + Comment: *flagAddComment, } if err := source.addBaseProfiles(*flagBase, *flagDiffBase); err != nil { @@ -327,9 +334,10 @@ var usageMsgSrc = "\n\n" + var usageMsgVars = "\n\n" + " Misc options:\n" + - " -http Provide web based interface at host:port.\n" + + " -http Provide web interface at host:port.\n" + " Host is optional and 'localhost' by default.\n" + " Port is optional and a randomly available port by default.\n" + + " -no_browser Skip opening a browser for the interactive web UI.\n" + " -tools Search path for object tools\n" + "\n" + " Legacy convenience options:\n" + diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 45f184674..1be749aa3 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -54,7 +54,7 @@ func PProf(eo *plugin.Options) error { } if src.HTTPHostport != "" { - return serveWebInterface(src.HTTPHostport, p, o) + return serveWebInterface(src.HTTPHostport, p, o, src.HTTPDisableBrowser) } return interactive(p, o) } diff --git a/internal/driver/webui.go b/internal/driver/webui.go index 9bf1d70f1..5c7f449e4 100644 --- a/internal/driver/webui.go +++ b/internal/driver/webui.go @@ -82,7 +82,7 @@ type webArgs struct { FlameGraph template.JS } -func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) error { +func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, disableBrowser bool) error { host, port, err := getHostAndPort(hostport) if err != nil { return err @@ -117,8 +117,12 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) e }, } - if o.UI.WantBrowser() { - go openBrowser("http://"+args.Hostport, o) + url := "http://" + args.Hostport + + o.UI.Print("Serving web UI on ", url) + + if o.UI.WantBrowser() && !disableBrowser { + go openBrowser(url, o) } return server(args) } diff --git a/internal/driver/webui_test.go b/internal/driver/webui_test.go index 58681bea8..6d288b31f 100644 --- a/internal/driver/webui_test.go +++ b/internal/driver/webui_test.go @@ -58,7 +58,7 @@ func TestWebInterface(t *testing.T) { Obj: fakeObjTool{}, UI: &proftest.TestUI{}, HTTPServer: creator, - }) + }, false) <-serverCreated defer server.Close()