Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Профилирование и оптимизация программ на Go
Search
Alexey Palazhchenko
April 14, 2017
Programming
3
450
Профилирование и оптимизация программ на Go
Доклад на конференции Стачка 2017
Alexey Palazhchenko
April 14, 2017
Tweet
Share
More Decks by Alexey Palazhchenko
See All by Alexey Palazhchenko
Using PostgreSQL's Background Worker Processes For Fun and Profit
aleksi
0
19
Песнь Хорьков и Гоферов
aleksi
0
320
Fuzzy generics
aleksi
0
120
On Ferrets and Gophers
aleksi
0
220
How to Go Wrong with Concurrency
aleksi
2
740
Adding context to existing code
aleksi
1
110
Зачем и как написать свой database/sql драйвер
aleksi
1
150
Cooking gRPC
aleksi
1
820
Profiling and Optimizing Go Programs
aleksi
1
1.7k
Other Decks in Programming
See All in Programming
ML.NETで始める機械学習
ymd65536
0
240
[JAWS DAYS 2025] 最近の DB の競合解決の仕組みが分かった気になってみた
maroon1st
0
140
機能が複雑化しても 頼りになる FactoryBotの話
tamikof
1
220
仕様変更に耐えるための"今の"DRY原則を考える
mkmk884
9
3.2k
責務と認知負荷を整える! 抽象レベルを意識した関心の分離
yahiru
8
1.5k
Visual StudioのGitHub Copilotでいろいろやってみる
tomokusaba
1
210
Webフレームワークとともに利用するWeb components / JSConf.jp おかわり
spring_raining
1
110
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
340
Jakarta EE meets AI
ivargrimstad
0
510
pylint custom ruleで始めるレビュー自動化
shogoujiie
0
160
ソフトウェアエンジニアの成長
masuda220
PRO
12
2.1k
良いコードレビューとは
danimal141
7
2.2k
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Optimising Largest Contentful Paint
csswizardry
34
3.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
The Language of Interfaces
destraynor
156
24k
Scaling GitHub
holman
459
140k
Statistics for Hackers
jakevdp
797
220k
Documentation Writing (for coders)
carmenintech
68
4.6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Mobile First: as difficult as doing things right
swwweet
223
9.5k
Transcript
Профилирование и оптимизация программ на Go Алексей Палажченко 14 апреля
2017, Стачка
Профилирование и оптимизация программ на Go Алексей Палажченко 14 апреля
2017, Стачка
None
None
None
None
None
None
type Cache interface { Get(id string) interface{} Set(id string, value
interface{}) Len() int }
func (s *Slice) Get(id string) interface{} { for _, it
:= range s.items { if it.id == id { return it.value } } return nil }
func (s *Slice) Set(id string, value i{}) { for i,
it := range s.items { if it.id == id { s.items[i].value = value return } } s.items = append(s.items, item{id, value}) }
func (s *Slice) Set(id string, value i{}) { for i,
it := range s.items { if it.id == id { s.items[i].value = value return } } s.items = append(s.items, item{id, value}) }
b.ResetTimer() for i := 0; i < b.N; i++ {
for _, id := range ids { Sink = c.Get(id) } }
100 200 300 400 0 1 2 3 4 5
6 7 8 9 10 Slice Map
100 200 300 400 0 1 2 3 4 5
6 7 8 9 10 Slice Map Fancy algorithms are slow when n is small, and n is usually small. - Rob Pike
Sink func popcnt(x uint64) int { var res uint64 for
; x > 0; x >>= 1 { res += x & 1 } return int(res) }
Sink const m1 = 0x5555555555555555 const m2 = 0x3333333333333333 const
m4 = 0x0f0f0f0f0f0f0f0f const h01 = 0x0101010101010101 func popcnt2(x uint64) int { x -= (x >> 1) & m1 x = (x & m2) + ((x >> 2) & m2) x = (x + (x >> 4)) & m4 return int((x * h01) >> 56) }
Sink func BenchmarkPopcnt(b *testing.B) { for i := 0; i
< b.N; i++ { popcnt(uint64(i)) } } func BenchmarkPopcnt2(b *testing.B) { for i := 0; i < b.N; i++ { popcnt2(uint64(i)) } }
Sink go test -v -bench=. BenchmarkPopcnt-4 100000000 15.5 ns/op BenchmarkPopcnt2-4
2000000000 0.34 ns/op
Sink go test -v -bench=. BenchmarkPopcnt-4 100000000 15.5 ns/op BenchmarkPopcnt2-4
2000000000 0.34 ns/op
Sink • go doc compile • go test -bench=. -gcflags
"-S"
Sink popcnt_test.go:14 MOVQ "".b+8(FP), AX popcnt_test.go:14 MOVQ $0, CX popcnt_test.go:14
MOVQ 200(AX), DX popcnt_test.go:14 CMPQ CX, DX popcnt_test.go:14 JGE $0, 34 popcnt_test.go:14 INCQ CX popcnt_test.go:14 MOVQ 200(AX), DX popcnt_test.go:14 CMPQ CX, DX popcnt_test.go:14 JLT $0, 19 popcnt_test.go:17 RET
Sink func BenchmarkPopcnt(b *testing.B) { for i := 0; i
< b.N; i++ { Sink = popcnt(uint64(i)) } } func BenchmarkPopcnt2(b *testing.B) { for i := 0; i < b.N; i++ { Sink = popcnt2(uint64(i)) } }
Sink go test -v -bench=. BenchmarkPopcnt-4 50000000 39.3 ns/op BenchmarkPopcnt2-4
50000000 26.8 ns/op
Sink env GOSSAFUNC=BenchmarkPopcnt2 go test -bench=.
Benchmarks • Не в виртуальной машине • Не трогать во
время работы • Выключить автоматическое управление питанием • rsc.io/benchstat
pprof • runtime/pprof • net/http/pprof • go test • Не
больше одного за раз!
pprof: CPU • setitimer(2), ITIMER_PROF, SIGPROF • До 500 Гц
(100 по умолчанию) • SetCPUProfileRate(hz) • go test -bench=XXX -cpuprofile=XXX.pprof • go tool pprof -svg -output=XXX.svg cache.test XXX.pprof
None
None
pprof: mem/block/mutex • pprof.MemProfileRate = bytes • pprof.SetBlockProfileRate(ns) • pprof.SetMutexProfileFraction(rate)
type Map struct { m sync.Mutex items map[string]interface{} } func
(m *Map) Get(id string) interface{} { m.m.Lock() defer m.m.Unlock() return m.items[id] }
pprof: block • go test -v -bench=XXX -blockprofile=XXX.pprof • go
tool pprof -svg -lines -output=XXX.svg ccache.test XXX.pprof
None
type Map struct { m sync.RWMutex items map[string]interface{} } func
(m *Map) Get(id string) interface{} { m.m.RLock() v := m.items[id] m.m.RUnlock() return v }
pprof: свои профили • Когда нужны stack traces • Интеграция
с go tool pprof • Пример: открытие и закрытие файлов • pprof.NewProfile, pprof.Lookup • Profile.Add, Remove
Execution tracer • Запуск, остановка, переключение горутин • Блокировки на
каналах, select • Блокировки на mutex’ах • Блокировки на сети, syscall’ах
Execution tracer • Все события с полным контекстом • Большие
файлы (со всеми символами) • Замедление ~25% • pprof CPU для throughput, tracer для latency
Execution tracer • import _ "net/http/pprof" • http://127.0.0.1:8080/debug/pprof • redis-benchmark
-r 100000 -e -l -t set,get • go tool trace trace.out
Execution tracer • go tool trace -pprof=TYPE trace.out > TYPE.pprof
• net • sync • syscall • sched
Linux • perf (perf_events) • SystemTap • BPF (eBPF)
Переменные окружения • GOGC (100, off) • GODEBUG • gctrace=1
• allocfreetrace=1 • schedtrace=1000
Оптимизации • struct{} • m[string(b)] • for i, c :=
range []byte(s) • for i := range s { a[i] = <zero value> }
Оптимизации type Key [64]byte type Value struct { Name [32]byte
Balance uint64 Timestamp int64 } m := make(map[Key]Value, 1e8)
• golang-ru @ Google Groups • 4gophers.ru/slack • golangshow.com •
github.com/AlekSi/ stachka-2017