/
benchmark_test.go
140 lines (113 loc) · 2.96 KB
/
benchmark_test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package mosaic
import (
"context"
"encoding/binary"
"fmt"
"math"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/disintegration/imaging"
)
func BenchmarkIndexLarge(b *testing.B) {
tmp := makeTestImages(b, 0, 0)
b.ResetTimer()
cfg := Config{
Workers: runtime.GOMAXPROCS(0),
}
err := NewIndex(cfg).AddPath(context.Background(), tmp)
if err != nil {
b.Fatal(err)
}
}
func BenchmarkIndexTiny(b *testing.B) {
tmp := makeTestImages(b, 100, 100)
b.ResetTimer()
cfg := Config{
Workers: runtime.GOMAXPROCS(0),
}
err := NewIndex(cfg).AddPath(context.Background(), tmp)
if err != nil {
b.Fatal(err)
}
}
// makeTestImages creates b.N test images in a temporary directory.
// If width and height are non-zero, then the images will be resized.
// A cleanup handler is registered to remove the temporary directory.
func makeTestImages(b *testing.B, width, height int) string {
src := loadTestImage(b, "testfiles/cat.jpg")
if width != 0 || height != 0 {
src = imaging.Fill(src, width, height, imaging.Center, imaging.Lanczos)
}
tmp, err := os.MkdirTemp("", "mosaic-benchmark")
if err != nil {
b.Fatal(err)
}
b.Cleanup(func() {
os.RemoveAll(tmp)
})
for i := 0; i < b.N; i++ {
func() {
out, err := os.Create(filepath.Join(tmp, fmt.Sprintf("%d.jpg", i)))
if err != nil {
b.Fatal(err)
}
defer out.Close()
err = imaging.Encode(out, src, imaging.JPEG)
if err != nil {
b.Fatal(err)
}
}()
}
return tmp
}
func BenchmarkSwap(b *testing.B) {
cfg := Config{
Workers: runtime.GOMAXPROCS(0),
}
idx := benchmarkIndex(b)
/*
idx := NewIndex(cfg)
err := idx.AddPath(context.Background(), "/home/user/lorempicsum")
if err != nil {
b.Fatal(err)
}
*/
src := loadTestImage(b, "testfiles/cat.jpg")
passes := 1
// Calculate the area required for each tile to get b.N tiles.
area := float64(src.Bounds().Dx() * src.Bounds().Dy())
tileArea := float64(src.Bounds().Dx()*src.Bounds().Dy()) / float64(b.N)
// If the area is less than 100 pixels, then we need to do multiple passes.
if tileArea < 50 {
tileArea = 50
tileCount := area / tileArea
passes = b.N / int(math.Ceil(tileCount))
}
tileWidth := math.Sqrt(tileArea)
cfg.TileWidth = int(tileWidth)
cfg.TileHeight = int(math.Ceil(tileArea / tileWidth))
//fmt.Printf("passes=%d tileArea=%.02f tileWidth=%d tileHeight=%d\n", passes, tileArea, cfg.TileWidth, cfg.TileHeight)
//fmt.Printf("n=%d a=%d\n", b.N, passes*int(area/tileArea))
generator := NewGenerator(cfg, idx)
b.ResetTimer()
for i := 0; i < passes; i++ {
generator.Generate(context.Background(), src)
}
}
func benchmarkIndex(b *testing.B) *Index {
// colors is a file containing a list of colors encoded as 32-bit integers.
buf, err := os.ReadFile("testfiles/colors")
if err != nil {
b.Fatal(err)
}
index := NewIndex(Config{})
for i := 0; i < len(buf); i += 4 {
err = index.insert(binary.LittleEndian.Uint32(buf[i:]), "testfiles/cat.jpg")
if err != nil {
b.Fatal(err)
}
}
return index
}