-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
77 lines (67 loc) · 1.96 KB
/
debug.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package skiptro
import (
"fmt"
"image"
"image/color"
"image/png"
"log"
"os"
"runtime/pprof"
"runtime/trace"
)
func DebugImage(scene Scene, fps int) {
similarityMatrix := scene.Similarity.Matrix
img := image.NewRGBA(image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: len(similarityMatrix), Y: len(similarityMatrix[0])}})
// Draw matrix for visual debugging
for x := 0; x < len(similarityMatrix); x++ {
for y := 0; y < len(similarityMatrix[x]); y++ {
// Draw checkerboard
if (x+y)%2 == 0 {
img.Set(x, y, color.RGBA{R: 0xaa, G: 0xaa, B: 0xaa, A: 0xaa})
} else {
img.Set(x, y, color.White)
}
// Mark 5sec and 30sec with different colors
if x%(5*fps) == 0 || y%(5*fps) == 0 {
// #ffae98
img.Set(x, y, color.RGBA{R: 0xff, G: 0xae, B: 0x98, A: 0xff})
}
if x%(30*fps) == 0 || y%(30*fps) == 0 {
// #98bcff
img.Set(x, y, color.RGBA{R: 0x98, G: 0xbc, B: 0xff, A: 0xff})
}
// Override if there is a similar pixel
if similarityMatrix[x][y] {
// #a41caf
img.Set(x, y, color.RGBA{R: 0xa4, G: 0x1c, B: 0xaf, A: 0xff})
if y >= scene.Similarity.StartIndex && y <= scene.Similarity.EndIndex {
img.Set(x, y, color.RGBA{R: 0xff, A: 0xff})
}
}
}
}
imgOut, err := os.Create("debug.png")
if err != nil {
log.Fatal(err)
}
if err := png.Encode(imgOut, img); err != nil {
log.Fatal(err)
}
}
func ProfileAndTrace() (stopCpuFunc func(), stopTraceFunc func(), err error) {
ftrace, err := os.Create("debug.trace")
if err != nil {
return nil, nil, fmt.Errorf("could not create file: %w", err)
}
if err := trace.Start(ftrace); err != nil {
return nil, nil, fmt.Errorf("could not start trace: %w", err)
}
f, err := os.Create("debug.profile")
if err != nil {
return nil, nil, fmt.Errorf("could not create file: %w", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
return nil, nil, fmt.Errorf("could not start cpu profiler: %w", err)
}
return pprof.StopCPUProfile, trace.Stop, nil
}