-
Notifications
You must be signed in to change notification settings - Fork 17
/
test-debug-prof.slide
283 lines (167 loc) · 5.67 KB
/
test-debug-prof.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
Testing, Debugging and Profiling
23 Apr 2013
Tags: go test profile debug
Fabrizio Milo, Miki Tebeka
@fabmilo
@tebeka
* Testing
* Testing Recipe
- Write `test_<module>.go` (discovery based test system)
- With the same package as the one being tested
- import [[http://golang.org/pkg/testing/][testing]]
- Write `TestFoo(t`*testing.T)` functions
- Use `init` for setup
- Use `defer` for poor mans teardown (in *every* test function)
- Run `go`test`-v`
- Run `go`test`-run='.*Mul.*'`-v` to select tests
- Read `go`help`testflag` output for more options
* math.go
.code test-debug-prof/math/math.go
* math_test.go
.code test-debug-prof/math/math_test.go /START1/,/END1/
.code test-debug-prof/math/math_test.go /START2/,/END2/
* Table Driven Testing
.code test-debug-prof/table/math_test.go /START1/,/END1/
* Parallel Execution
.code test-debug-prof/parallel/math_test.go /START1/,/END1/
- `time`go`test` (~2sec)
- `time`go`test`-parallel`2` (~1sec)
- If you have CPU intensive tests try `go`test`-parallel`2`-cpu`2`
* Benchmarks
.code test-debug-prof/bench/math_test.go /START1/,/END1/
- `go`test`-bench`'.*'`
* Debugging
* GDB 7.5 on Mac
- You need GDB 7.5 OS X has a branch of 6.X
$gdb --version
GNU gdb (GDB) 7.5
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin11.4.2".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
* If you need to upgrade on OS X
- Download latest stable release source code from
http://sourceware.org/gdb/download/
- Unpack and compile:
tar xjvf gdb-7.4.tar.bz2
cd gdb-7.4
less gdb/README
./configure
make
- Sign GDB (more info in the link)
.link http://sourceware.org/gdb/wiki/BuildingOnDarwin
* Compiling go programs for debugging
- you can build a development version of dependent pkgs.
go install -a -v -gcflags "-N -l" <pkgs-list>
Load GDB golang extensions:
(gdb)source http://golang.org/src/pkg/runtime/runtime-gdb.py
* Gdb Configuration
(gdb)source <go-installation>/src/pkg/runtime/runtime-gdb.py
(gdb)skip malloc.goc:436
you can add commands in your .gdbinit inside your GOPATH
(GOPATH)$cat .gdbinit
source runtime-gdb.py
skip malloc.goc:436
* If GDB is properly configured for go if you read these lines:
.code test-debug-prof/debug/gdbout
* Let's use gdb on geodns:
mkdir gdbex
cd gdbex
mkdir bin lib src
export GOPATH=`pwd`
brew install geoip # OSX
yum install geoip-devel # RedHat / CentOs
go get github.com/abh/geodns
go build -a -v -gcflags "-N -l" github.com/abh/geodns
.link https://github.com/abh/geodns
* Configure Environment
- How to set an environemnt variable:
set environment GOGCTRACE=1
set environmnet GOMAXPROCS=1
set environment GOTRACEBACK=2
note that we can add these variables to the `.gdbinit` file as well
- How to set arguments:
set args -port=5300
* Breakpoints
- Breakpoint on module function:
(gdb) b main.main
- Breakpoint on file line:
(gdb) b serve.go:212
- Breakpoint on library function
(gdb) b net/http.ListenAndServe
* How to inspect variables
- info locals // locals only
- info variables // all variables including globals / static
- info args
- whatis <variablename>
- info variables <regexp>
- p <variablename>
* GDB Commands
- list
- list line
- list file.go:line
- break line
- break file.go:line
- disas
* Go Extensions (2)
- bt
- frame n
- p var
- p $len(var)
- p $dtype(var)
- iface var
- info goroutines
- goroutine n cmd
- help goroutine
* NOTE: You can't call a Go function
(gdb) call 'main.greet'("hello")
infrun.c:5777: internal-error: normal_stop: Assertion `get_frame_type (frame) == DUMMY_FRAME' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session? (y or n) y
This is due to the differences on how Go calls a function and the way GDB does
* User Interfaces for GDB
- gdb -tui
- ddd
- Affinic's
* Hopwatch
.play test-debug-prof/hopwatch.go
* Go Manual
- import "runtime/debug"
.link http://golang.org/pkg/runtime/debug/#PrintStack
func PrintStack()
.link http://golang.org/pkg/runtime/#Stack
func Stack(buf []byte, all bool) int
- import "runtime"
.link http://golang.org/pkg/runtime/#Breakpoint
func Breakpoint()
* Example
.code test-debug-prof/debug/break.go
* Custom Exceptions: Extending Error's interface
.link https://code.google.com/p/biogo/source/browse/errors/errors.go
* Profiling
* Running the Memory Profiler
* Notes
in pre Go 1.0 the memory profiler requires that the garbage collection has run at least once.
To force the GC use runtime.GC() i.e:
DumpHeapProfile("pre-gc-mem.prof")
runtime.GC() // force Garbage Collection
DumpHeapProfile("post-gc-mem.prof")
The value of runtime.MemProfileRate is important to tweak as it adjusts the granularity of the stats, the default is to only consider allocations of 512kb or bigger.
.link http://golang.org/pkg/runtime/#pkg-variables
go tool pprof <Executable> executable.prof
install graphviz
.link http://www.graphviz.org/
* Testing References
- [[http://golang.org/pkg/testing/][testing]]
- [[http://golang.org/doc/code.html#Testing][Testing]] section in "How To Write Go Code"
- [[http://labix.org/gocheck][gocheck]]
- [[https://bitbucket.org/tebeka/go2xunit][go2xunit]] Jenkins integration
* Debugging References
- [[http://golang.org/doc/gdb][gdb]]
- [[http://www.affinic.com/?page_id=109][Visual GDB Debugger]]
- [[https://github.com/emicklei/hopwatch][HopWatch]]