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
Песнь Хорьков и Гоферов
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
140
Cooking gRPC
aleksi
1
810
Profiling and Optimizing Go Programs
aleksi
1
1.7k
Go: начало
aleksi
0
94
Other Decks in Programming
See All in Programming
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
230
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
380
ML.NETで始める機械学習
ymd65536
0
130
負債になりにくいCSSをデザイナとつくるには?
fsubal
10
2.5k
2024年のWebフロントエンドのふりかえりと2025年
sakito
3
260
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
150
Lottieアニメーションをカスタマイズしてみた
tahia910
0
130
第3回 Snowflake 中部ユーザ会- dbt × Snowflake ハンズオン
hoto17296
4
370
責務と認知負荷を整える! 抽象レベルを意識した関心の分離
yahiru
7
860
Amazon Bedrock Multi Agentsを試してきた
tm2
1
290
Conform を推す - Advocating for Conform
mizoguchicoji
3
700
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
130
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
461
33k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
The Invisible Side of Design
smashingmag
299
50k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
The Cult of Friendly URLs
andyhume
78
6.2k
BBQ
matthewcrist
87
9.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
4 Signs Your Business is Dying
shpigford
182
22k
Unsuck your backbone
ammeep
669
57k
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