浏览代码

Add option to disable browser in HTTP mode (#462)

ahh 6 年前
父节点
当前提交
77426154d5
共有 5 个文件被更改,包括 33 次插入20 次删除
  1. 1
    0
      CONTRIBUTORS
  2. 23
    15
      internal/driver/cli.go
  3. 1
    1
      internal/driver/driver.go
  4. 7
    3
      internal/driver/webui.go
  5. 1
    1
      internal/driver/webui_test.go

+ 1
- 0
CONTRIBUTORS 查看文件

@@ -13,3 +13,4 @@ Tipp Moseley <tipp@google.com>
13 13
 Hyoun Kyu Cho <netforce@google.com>
14 14
 Martin Spier <spiermar@gmail.com>
15 15
 Taco de Wolff <tacodewolff@gmail.com>
16
+Andrew Hunter <andrewhhunter@gmail.com>

+ 23
- 15
internal/driver/cli.go 查看文件

@@ -32,11 +32,12 @@ type source struct {
32 32
 	DiffBase  bool
33 33
 	Normalize bool
34 34
 
35
-	Seconds      int
36
-	Timeout      int
37
-	Symbolize    string
38
-	HTTPHostport string
39
-	Comment      string
35
+	Seconds            int
36
+	Timeout            int
37
+	Symbolize          string
38
+	HTTPHostport       string
39
+	HTTPDisableBrowser bool
40
+	Comment            string
40 41
 }
41 42
 
42 43
 // parseFlags parses the command lines through the specified flags package
@@ -65,7 +66,8 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
65 66
 	flagMeanDelay := flag.Bool("mean_delay", false, "Display mean delay at each region")
66 67
 	flagTools := flag.String("tools", os.Getenv("PPROF_TOOLS"), "Path for object tool pathnames")
67 68
 
68
-	flagHTTP := flag.String("http", "", "Present interactive web based UI at the specified http host:port")
69
+	flagHTTP := flag.String("http", "", "Present interactive web UI at the specified http host:port")
70
+	flagNoBrowser := flag.Bool("no_browser", false, "Skip opening a browswer for the interactive web UI")
69 71
 
70 72
 	// Flags used during command processing
71 73
 	installedFlags := installFlags(flag)
@@ -118,6 +120,10 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
118 120
 		return nil, nil, errors.New("-http is not compatible with an output format on the command line")
119 121
 	}
120 122
 
123
+	if *flagNoBrowser && *flagHTTP == "" {
124
+		return nil, nil, errors.New("-no_browser only makes sense with -http")
125
+	}
126
+
121 127
 	si := pprofVariables["sample_index"].value
122 128
 	si = sampleIndex(flagTotalDelay, si, "delay", "-total_delay", o.UI)
123 129
 	si = sampleIndex(flagMeanDelay, si, "delay", "-mean_delay", o.UI)
@@ -133,14 +139,15 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
133 139
 	}
134 140
 
135 141
 	source := &source{
136
-		Sources:      args,
137
-		ExecName:     execName,
138
-		BuildID:      *flagBuildID,
139
-		Seconds:      *flagSeconds,
140
-		Timeout:      *flagTimeout,
141
-		Symbolize:    *flagSymbolize,
142
-		HTTPHostport: *flagHTTP,
143
-		Comment:      *flagAddComment,
142
+		Sources:            args,
143
+		ExecName:           execName,
144
+		BuildID:            *flagBuildID,
145
+		Seconds:            *flagSeconds,
146
+		Timeout:            *flagTimeout,
147
+		Symbolize:          *flagSymbolize,
148
+		HTTPHostport:       *flagHTTP,
149
+		HTTPDisableBrowser: *flagNoBrowser,
150
+		Comment:            *flagAddComment,
144 151
 	}
145 152
 
146 153
 	if err := source.addBaseProfiles(*flagBase, *flagDiffBase); err != nil {
@@ -327,9 +334,10 @@ var usageMsgSrc = "\n\n" +
327 334
 
328 335
 var usageMsgVars = "\n\n" +
329 336
 	"  Misc options:\n" +
330
-	"   -http              Provide web based interface at host:port.\n" +
337
+	"   -http              Provide web interface at host:port.\n" +
331 338
 	"                      Host is optional and 'localhost' by default.\n" +
332 339
 	"                      Port is optional and a randomly available port by default.\n" +
340
+	"   -no_browser        Skip opening a browser for the interactive web UI.\n" +
333 341
 	"   -tools             Search path for object tools\n" +
334 342
 	"\n" +
335 343
 	"  Legacy convenience options:\n" +

+ 1
- 1
internal/driver/driver.go 查看文件

@@ -54,7 +54,7 @@ func PProf(eo *plugin.Options) error {
54 54
 	}
55 55
 
56 56
 	if src.HTTPHostport != "" {
57
-		return serveWebInterface(src.HTTPHostport, p, o)
57
+		return serveWebInterface(src.HTTPHostport, p, o, src.HTTPDisableBrowser)
58 58
 	}
59 59
 	return interactive(p, o)
60 60
 }

+ 7
- 3
internal/driver/webui.go 查看文件

@@ -82,7 +82,7 @@ type webArgs struct {
82 82
 	FlameGraph  template.JS
83 83
 }
84 84
 
85
-func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) error {
85
+func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options, disableBrowser bool) error {
86 86
 	host, port, err := getHostAndPort(hostport)
87 87
 	if err != nil {
88 88
 		return err
@@ -117,8 +117,12 @@ func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) e
117 117
 		},
118 118
 	}
119 119
 
120
-	if o.UI.WantBrowser() {
121
-		go openBrowser("http://"+args.Hostport, o)
120
+	url := "http://" + args.Hostport
121
+
122
+	o.UI.Print("Serving web UI on ", url)
123
+
124
+	if o.UI.WantBrowser() && !disableBrowser {
125
+		go openBrowser(url, o)
122 126
 	}
123 127
 	return server(args)
124 128
 }

+ 1
- 1
internal/driver/webui_test.go 查看文件

@@ -58,7 +58,7 @@ func TestWebInterface(t *testing.T) {
58 58
 		Obj:        fakeObjTool{},
59 59
 		UI:         &proftest.TestUI{},
60 60
 		HTTPServer: creator,
61
-	})
61
+	}, false)
62 62
 	<-serverCreated
63 63
 	defer server.Close()
64 64