-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler.go
46 lines (38 loc) · 914 Bytes
/
profiler.go
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
package main
import (
"fmt"
"github.com/pkg/profile"
)
// run 10 MM cache operations (between Set, Get - with hit and miss cases)
func runCacheSamples() {
cache, _ := NewLRUCache(1000)
hit := true
executions := 10000000 // 10MM
for i := 0; i < executions; i++ {
if i%2 == 0 {
cache.Set(fmt.Sprint("key", i), i)
} else {
if hit {
cache.Get(fmt.Sprint("key", i-1)) // there are only even keys on cache
hit = false
} else {
cache.Get(fmt.Sprint("key", i))
hit = true
}
}
}
}
func profileCPU() {
defer profile.Start(profile.CPUProfile, profile.ProfilePath("/tmp")).Stop()
runCacheSamples()
}
func profileMemory() {
defer profile.Start(profile.MemProfile, profile.ProfilePath("/tmp")).Stop()
runCacheSamples()
}
func main() {
fmt.Println("Profiling LRU Cache via pprof (CPU)")
profileCPU()
fmt.Println("Profiling LRU Cache via pprof (Memory)")
profileMemory()
}