How to profile memory usage in Golang - cloud-barista/cb-spider GitHub Wiki
[๋ชฉ์ฐจ]
- ํ๋กํ์ผ๋ง ๊ฐ์
- ํ๋กํ์ผ๋ง ํ๋กํ์ผ๋ง ํ ์คํธ ์ผ์ด์ค ๋ฐ ๋ฐฉ๋ฒ
- ํ๋กํ์ผ๋ง ๊ฒฐ๊ณผ ์์ฝ
- ์ํ ์ผ์ด์ค๋ณ ํ๋กํ์ผ๋ง ๊ฒฐ๊ณผ Snapshot
- ํ๋กํ์ผ๋ง ๋ถ์ ๊ฒฐ๊ณผ
- ํ๋กํ์ผ๋ง ๋ถ์ ๊ฒฐ๋ก ๋ฐ ๊ณํ
1. ํ๋กํ์ผ๋ง ๊ฐ์
- CB-Spider๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณผํ๊ฒ ์ฌ์ฉํ๋ค๋ ๋ฆฌํฌํธ๊ฐ ์์์
- CB-Spider์ ์ฃผ์ Operation๋ค๊ณผ Memory๋ฅผ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ Operation์ ๋ํ์ฌ Test Case๋ฅผ ์ ์ํ๊ณ Memory Leak ์์ฃผ์ ๋ถ์์ ์คํํจ.
- ๋์ CB-Spider Version: v0.4.0-5-ga6b2ae9
- Go Profiling ๋ฐฉ๋ฒ Reference
2. ํ๋กํ์ผ๋ง ํ ์คํธ ์ผ์ด์ค ๋ฐ ๋ฐฉ๋ฒ
- ํ๋กํ์ผ๋ง์ ์ํ pprof ์ฝ๋ ์ ์ฉ ํ์
-
์ ์ฉ ๋์ ์์ค: cb-spider/api-runtime/apiserver.go
-
์ ์ฉ ์ฝ๋ ์์
11 import ( ... ์๋ต 24 25 ///////////////// for profiling 26 "log" 27 "net/http" 28 _ "net/http/pprof" 29 ) 30 31 func main() { 32 ///////////////// for profiling 33 go func() { 34 log.Println(http.ListenAndServe("1.2.3.4:8282", nil)) 35 }() 36 wg := new(sync.WaitGroup) 37 wg.Add(1) 38 ///////////////// for profiling 39 40 rootCmd := NewRootCmd() 41 if err := rootCmd.Execute(); err != nil { 42 fmt.Printf("cb-spider terminated with error: %v\n", err) 43 } 44 ///////////////// for profiling 45 wg.Wait() 46 } ... ์ดํ ์๋ต
-
- ์ค๋น ํ๊ฒฝ
- ๋ณธ ์ํ ํ๊ฒฝ: Ubuntu 18.04, go 1.16.5
- Graph ์ถ๋ ฅ ์ง์ ๋๊ตฌ dot, gv ์ค์น
sudo apt-get install -y graphviz gv
- ํ๋กํ์ผ๋ง ์ ์ฐจ
- pprof๋ฅผ ์ ์ฉํ CB-Spider ์๋ฒ ๊ฐ๋
- Test Case ์คํ์ผ๋ก ์๋ฒ ๋ฉ๋ชจ๋ฆฌ ์ํ ๋ณํ ์ ๋
- Browser ์ด์ฉํ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ํํฉ ์ฐธ๊ณ
- ๋ฉ๋ชจ๋ฆฌ ํํฉ snapshot ์ด๋ฏธ์ง ๋ฝ๋ ๋ฐฉ๋ฒ
go tool pprof -png http://1.2.3.4:8282/debug/pprof/heap > data.png
-
ํ๋กํ์ผ๋ง ํ ์คํธ ์ผ์ด์ค
- [1] Spider Server start ํ ์ธก์
- [2] AWS:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐ ์์ฑ, Azure:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐ ์์ฑ ํ ์ธก์
- [3] AWS:ListImage ์คํ ํ ์ธก์
- [4] Azure:ListImage ์คํ ํ ์ธก์
- [5] Azure:ListImage ์คํ ํ ์ธก์
- [6] Azure:ListImage ์คํ ํ ์ธก์
- [7] ๋ชจ๋ ์์์ ์ญ์ ํ ์ธก์
- [8] drop caches ํ ์ธก์
- [9] Spider Server stop ํ ์ธก์
โป [2], [7]: CB-Spider REST API(Echo) ํ์ฉ โป [3] ~ [6]: CB-Spider Go API(gRPC) ํ์ฉ โป ์์ ๊ฐ์ ์์๋ก ์ํ ์ผ์ด์ค ์คํ์ ๋ง๋ค ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ํํฉ ํ๋กํ์ผ๋ง
3. ํ๋กํ์ผ๋ง ๊ฒฐ๊ณผ ์์ฝ
[1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | |
---|---|---|---|---|---|---|---|---|
Mem Usages | 11.7MB | 13.416MB | 63MB | 37MB | 13MB | 38MB | 13.8MB | 13.8MB |
Profile | Count | Count | Count | Count | Count | Count | Count | Count |
allocs | 41 | 41 | 1532 | 2236 | 2469 | 2596 | 2614 | 2614 |
block | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
cmdline | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
goroutine | 12 | 12 | 17 | 15 | 11 | 16 | 9 | 10 |
heap | 41 | 41 | 1532 | 2236 | 2469 | 2596 | 2614 | 2614 |
mutex | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
profile | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
threadcreate | 20 | 20 | 20 | 20 | 20 | 20 | 20 | 20 |
trace | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Profile Description:
- allocs: A sampling of all past memory allocations block: Stack traces that led to blocking on synchronization primitives
- cmdline: The command line invocation of the current program
- goroutine: Stack traces of all current goroutines
- heap: A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.
- mutex: Stack traces of holders of contended mutexes
- profile: CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.
- threadcreate: Stack traces that led to the creation of new OS threads
- trace: A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.
4. ์ํ ์ผ์ด์ค๋ณ ํ๋กํ์ผ๋ง ๊ฒฐ๊ณผ Snapshot
- [1] Spider Server start ํ ์ธก์
- [2] AWS:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐ ์์ฑ, Azure:VPC/Subnet, SecurityGroup, KeyPair ๋ฐ VM 1๊ฐ ์์ฑ ํ ์ธก์
- [3] AWS:ListImage ์คํ ํ ์ธก์
- [4] Azure:ListImage ์คํ ํ ์ธก์
- [5] Azure:ListImage ์คํ ํ ์ธก์
- [6] Azure:ListImage ์คํ ํ ์ธก์
- [7] ๋ชจ๋ ์์์ ์ญ์ ํ ์ธก์
-
[8] drop caches ํ ์ธก์
-
[1] ~ [7] ๋์ ์์คํ ๋ฉ๋ชจ๋ฆฌ ๋ณํ: ๋ชจ๋ ์ํ ๋์ GB ๋จ์์ ๋ฉ๋ชจ๋ฆฌ ๋ณํ๋ ์์, ์ปค๋ ํ์ผ ์บ์๋ ๋ฒํผ๋ก ์ด๋ฏธ 26GB๊ฐ ์ฌ์ฉ์ค์ด๋ฉฐ, ํ์ฌ ์ํ๊ณผ๋ ์ง์ ์ ์ธ ์ํฅ์ ์์.
-
์ด๋ฅผ ํ์ธํ๊ธฐ ์ํด cache๋ฅผ drop ์ํจ ํ ๋ค์ ์ธก์ ํด๋ด.
- โป drop caches ํ cache๋ ๋ฐํ ๋๊ณ , Spider ํ๋ก์ธ์ค๋ GC ๋ฑ์ ๋ณํ ์์ด ๊ทธ๋๋ก 13.8MB๊ฐ ์ ์ง๋จ(์๋ ํ๋กํ์ผ๋ง snapshot ์ฐธ๊ณ ).
-
ํ๋กํ์ผ๋ง snapshot์ ๋ค์๊ณผ ๊ฐ์
-
-
[9] Spider Server stop ํ ์ธก์
-
Spider ์ข ๋ฃ์ profile ์๋ฒ ํจ๊ป ์ข ๋ฃ๋ก ํ๋กํ์ผ๋ง ์ธก์ ๋ถ๊ฐ
-
Spider Server ์ข ๋ฅ ์ -ํ ๋ฉ๋ชจ๋ฆฌ ์ฐจ์ด: 49MB
-
5. ํ๋กํ์ผ๋ง ๋ถ์ ๊ฒฐ๊ณผ
- ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ฅ ๋ง์ด(63MB) ์ฌ์ฉํ๋ [3] ์๋๋ฆฌ์ค ํํฉ(์๋ ๊ทธ๋ฆผ, [3] ํ๋กํ์ผ๋ง Snapshot ์ฐธ๊ณ )์ด ์๋์ ๊ฐ์ด AdminWeb์ ImageList ์น์๋น์ค์์ ์๋ชจ๋๊ณ ์์. (Image ๋ชฉ๋ก: 9,465๊ฐ)
- [3] ์๋๋ฆฌ์ค ์ํ ์ดํ ImageList ๊ด๋ จ ์ฌ์ฉํ๋ 50MB ๋ฉ๋ชจ๋ฆฌ ์ฒด์ธ์ด ์ ์์ ์ผ๋ก ์ฌ๋ผ์ง.([4]-[8] ํ๋กํ์ผ๋ง snapshot ์ฐธ๊ณ )
โป ์ฐธ๊ณ : AWS:Oregon Region์ Image ๋ชฉ๋ก ์ถ๋ ฅ ํ ์๋ฒ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์ํ
(Operation ์คํ ํ ๊ฒฐ๊ณผ์ด๊ธฐ๋ ํ๊ณ , GC ์ํฅ์ผ๋ก ์ต๊ณ ์ ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ํํฉ์ ์๋ ์ ์์)
โป ๊ด๋ จ ์ ๋ณด: Image ๋ชฉ๋ก 9,465๊ฐ ==> HTML source ํฌ๊ธฐ๋ง: 10.4MB
=> 10.4MB ๋ฐ์ดํฐ ๊ท๋ชจ์ 50MB ํ์ฉ์ ๊ฐ์ ํฌ์ธํธ๋ ์๊ฒ ์ง๋ง, GB ๋จ์ ์ดํ์ด๋ฏ๋ก ํ์ฌ๋ Pass
... ์ค๊ฐ ์๋ต
6. ํ๋กํ์ผ๋ง ๋ถ์ ๊ฒฐ๋ก ๋ฐ ๊ณํ
- ๊ฐ์ฅ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ง์ด ํ์ฉํ๋ ์๋๋ฆฌ์ค๋ก ์ฌํ์ ์๋ํ์ง๋ง, Criticalํ ๋ฉ๋ชจ๋ฆฌ ๋์๋ ์ฌํ๋์ง ์์.
- ๊ฐ๋ฅ์ฑ์ ํฌ๋ฐํ์ง๋ง, ๊ทธ๊ฐ ๊ธฐ๋ฅ ์ถ๊ฐ ๋ฐ ๊ฐ์ ์ ์ํด ์์ฐ ํด๊ฒฐ ๋์์ ์๋...
- ์๊ธฐ์ ๊ธฐ๋ฅ ๊ฐ๋ฐ/๊ฐ์ ์ฐ์ ์ถ์ง, ์ถ๊ฐ ๋ถ์ ์งํํ์ง ์๊ณ ๋ง๋ฌด๋ฆฌํจ.
- ํฅํ ์ฐ๊ด ์ด์ ๋ฐ์์ ์ถ๊ฐ ์์ธ ๋ถ์ ๋ฐ ๊ฐ์ ์ถ์ง, ๋๋, ์์ ํ ์์ ์ ํ์์ ์ฌ๋ถ์
โป ์ฐธ๊ณ : Go GC(Garbage Collector) ์ ์ฑ ์ ์ํด ๋ฉ๋ชจ๋ฆฌ ์ํ๋ ์ ๋์ ์ผ ์ ์์ผ๋ฉฐ, ์ด๋ก ์ธํ์ฌ [1] ~ [8] ์ํ ํ snapshot์ด ๊ฐ ์ํ ์ผ์ด์ค์ ์๋ฒ ๋ฉ๋ชจ๋ฆฌ ์ํ๋ฅผ ๋ํํ ์๋ ์์