Skip to content

Commit 169b973

Browse files
committed
more slide content
1 parent b2352eb commit 169b973

File tree

1 file changed

+95
-5
lines changed

1 file changed

+95
-5
lines changed

perf.slide

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Demo: word length count
9393
# -benchmem, b.ReportAllocs
9494
# b.Errorf, b.Logf, -v
9595
# danger: these skew benchmarking! demo with ReportAllocs
96+
# and wow, reflect.DeepEqual is expensive!
9697
# b.ResetTimer, b.StopTimer, b.StartTimer
9798
# show Go 1.4 vs Go tip (1.5)
9899

@@ -141,6 +142,8 @@ Demo: ngram
141142
# cpu is efficient, can run on live production server, memory less so
142143
# different kinds of profiling interfere with each other
143144

145+
# NetBSD also has (had?) broken profiling
146+
144147

145148
* CPU profiling
146149

@@ -187,13 +190,104 @@ Demo: ascii
187190
- Don't run tests when profiling.
188191
- If the output doesn't make sense, poke around or ask for help.
189192

190-
# pprof has a crappy UI
193+
# pprof has a crappy UI. Live with it. :(
191194

192195

193196
* Block profiling
194197

195198
Demo: ngram
196199

200+
# easy demo: -blockprofile=
201+
# discuss -blockprofilerate, -memprofilerate
202+
# there is a way to change cpu profile rate in runtime package but too fast can't happen (OS support, expense of walking the stack) and the default is pretty good
203+
# discuss ok/expected blocking: time.Ticker, sync.WaitGroup
204+
205+
206+
* Other kinds of profiling
207+
208+
In package `runtime/pprof`:
209+
210+
- `goroutine`: helpful for finding sources of leaking goroutines
211+
- `threadcreate` helpful for debugging runaway thread creation (usually syscalls or cgo)
212+
213+
# TODO: Expand?
214+
215+
Basic memory stats available in package `runtime`: `ReadMemStats`
216+
217+
.link https://golang.org/pkg/runtime/#MemStats
218+
219+
220+
* Whole program profiling
221+
222+
Set up first thing in `func`main`.
223+
224+
Use `runtime` and `runtime/pprof`...but it is a pain.
225+
226+
# Mention gotchas like flushing and closing the files,
227+
# calling runtime.GC before exit, etc.
228+
229+
Dave Cheney made a nice helper package:
230+
231+
go get -u github.com/pkg/profile
232+
233+
.link https://godoc.org/github.com/pkg/profile
234+
235+
236+
* Monitoring live servers
237+
238+
Cheap enough to do in production!
239+
240+
And easy, using `net/http/pprof`.
241+
242+
import _ "net/http/pprof"
243+
244+
Use pprof to view CPU:
245+
246+
go tool pprof -pdf http://localhost:4001/debug/pprof/profile > o.pdf && open o.pdf
247+
248+
Heap:
249+
250+
go tool pprof http://localhost:4001/debug/pprof/heap
251+
252+
Goroutines:
253+
254+
go tool pprof http://localhost:4001/debug/pprof/goroutine
255+
256+
See `net/http/pprof` docs.
257+
258+
259+
* Monitoring live servers
260+
261+
Demo: present
262+
263+
go tool pprof -pdf http://localhost:4001/debug/pprof/goroutine > o.pdf && open o.pdf
264+
265+
Oh goodness!
266+
267+
.link https://github.com/golang/go/issues/11507
268+
269+
270+
* Protecting the net/http/pprof endpoints
271+
272+
`net/http/pprof` registers endpoints with `http.DefaultServeMux`.
273+
274+
So don't use `http.DefaultServeMux`.
275+
276+
serveMux := http.NewServeMux()
277+
// use serveMux to serve your regular website
278+
279+
pprofMux := http.NewServeMux()
280+
pprofMux.HandleFunc("/debug/pprof/", pprof.Index)
281+
pprofMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
282+
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
283+
pprofMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
284+
// use pprofMux to serve the pprof handles
285+
286+
Or use a single non-default `ServeMux` but insert http handler middleware.
287+
288+
# TODO: Expand? Demo?
289+
290+
197291
* END OF DEVELOPED SLIDES
198292

199293
* More content...
@@ -203,10 +297,6 @@ Demo: ngram
203297
net/http/pprof, expvar packages.
204298
Dave Cheney's package.
205299
Run runtime.GC before exit.
206-
0306 Profiling concurrent code
207-
-blockprofile, -blockprofilerate
208-
Matters for scaling to more CPUs.
209-
What kind of blocking is ok? time.Ticker, sync.WaitGroup.
210300
0307 Execution tracing
211301
Tour of tracing.
212302
Tracing is brand new, and I'm not super familiar with it.

0 commit comments

Comments
 (0)