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
440
Профилирование и оптимизация программ на Go
Доклад на конференции Стачка 2017
Alexey Palazhchenko
April 14, 2017
Tweet
Share
More Decks by Alexey Palazhchenko
See All by Alexey Palazhchenko
Песнь Хорьков и Гоферов
aleksi
0
300
Fuzzy generics
aleksi
0
110
On Ferrets and Gophers
aleksi
0
210
How to Go Wrong with Concurrency
aleksi
2
710
Adding context to existing code
aleksi
1
110
Зачем и как написать свой database/sql драйвер
aleksi
1
130
Cooking gRPC
aleksi
1
780
Profiling and Optimizing Go Programs
aleksi
1
1.7k
Go: начало
aleksi
0
92
Other Decks in Programming
See All in Programming
flutterkaigi_2024.pdf
kyoheig3
0
150
Click-free releases & the making of a CLI app
oheyadam
2
120
Jakarta EE meets AI
ivargrimstad
0
640
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
3
210
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
最新TCAキャッチアップ
0si43
0
200
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
110
CSC509 Lecture 13
javiergs
PRO
0
110
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
380
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
110
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
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