Browse Source

Update benchmarks to focus on conversion performance.

Benchmark each interpolation function for RGBA and YCbCr images.
Reduce the image size used when benchmarking to reduce the influence of
memory performance on results.
Charlie Vieth 10 years ago
parent
commit
b09281bd36
1 changed files with 79 additions and 30 deletions
  1. 79
    30
      resize_test.go

+ 79
- 30
resize_test.go View File

@@ -85,56 +85,105 @@ func Test_SameSizeReturnsOriginal(t *testing.T) {
85 85
 	}
86 86
 }
87 87
 
88
-func Benchmark_BigResizeLanczos3(b *testing.B) {
89
-	var m image.Image
88
+const (
89
+	// Use a small image size for benchmarks. We don't want memory performance
90
+	// to affect the benchmark results.
91
+	benchMaxX = 250
92
+	benchMaxY = 250
93
+
94
+	// Resize values near the original size require increase the amount of time
95
+	// resize spends converting the image.
96
+	benchWidth  = 200
97
+	benchHeight = 200
98
+)
99
+
100
+func benchRGBA(b *testing.B, interp InterpolationFunction) {
101
+	m := image.NewRGBA(image.Rect(0, 0, benchMaxX, benchMaxY))
102
+	// Initialize m's pixels to create a non-uniform image.
103
+	for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
104
+		for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
105
+			i := m.PixOffset(x, y)
106
+			m.Pix[i+0] = uint8(y + 4*x)
107
+			m.Pix[i+1] = uint8(y + 4*x)
108
+			m.Pix[i+2] = uint8(y + 4*x)
109
+			m.Pix[i+3] = uint8(4*y + x)
110
+		}
111
+	}
112
+
113
+	var out image.Image
114
+	b.ResetTimer()
90 115
 	for i := 0; i < b.N; i++ {
91
-		m = Resize(1000, 1000, img, Lanczos3)
116
+		out = Resize(benchWidth, benchHeight, m, interp)
92 117
 	}
93
-	m.At(0, 0)
118
+	out.At(0, 0)
94 119
 }
95 120
 
96
-func Benchmark_Reduction(b *testing.B) {
97
-	largeImg := image.NewRGBA(image.Rect(0, 0, 1000, 1000))
121
+// The names of some interpolation functions are truncated so that the columns
122
+// of 'go test -bench' line up.
123
+func Benchmark_Nearest_RGBA(b *testing.B) {
124
+	benchRGBA(b, NearestNeighbor)
125
+}
98 126
 
99
-	var m image.Image
100
-	for i := 0; i < b.N; i++ {
101
-		m = Resize(300, 300, largeImg, Bicubic)
102
-	}
103
-	m.At(0, 0)
127
+func Benchmark_Bilinear_RGBA(b *testing.B) {
128
+	benchRGBA(b, Bilinear)
129
+}
130
+
131
+func Benchmark_Bicubic_RGBA(b *testing.B) {
132
+	benchRGBA(b, Bicubic)
133
+}
134
+
135
+func Benchmark_Mitchell_RGBA(b *testing.B) {
136
+	benchRGBA(b, MitchellNetravali)
137
+}
138
+
139
+func Benchmark_Lanczos2_RGBA(b *testing.B) {
140
+	benchRGBA(b, Lanczos2)
104 141
 }
105 142
 
106
-// Benchmark resize of 16 MPix jpeg image to 800px width.
107
-func jpegThumb(b *testing.B, interp InterpolationFunction) {
108
-	input := image.NewYCbCr(image.Rect(0, 0, 4896, 3264), image.YCbCrSubsampleRatio422)
143
+func Benchmark_Lanczos3_RGBA(b *testing.B) {
144
+	benchRGBA(b, Lanczos3)
145
+}
109 146
 
110
-	var output image.Image
147
+func benchYCbCr(b *testing.B, interp InterpolationFunction) {
148
+	m := image.NewYCbCr(image.Rect(0, 0, benchMaxX, benchMaxY), image.YCbCrSubsampleRatio422)
149
+	// Initialize m's pixels to create a non-uniform image.
150
+	for y := m.Rect.Min.Y; y < m.Rect.Max.Y; y++ {
151
+		for x := m.Rect.Min.X; x < m.Rect.Max.X; x++ {
152
+			yi := m.YOffset(x, y)
153
+			ci := m.COffset(x, y)
154
+			m.Y[yi] = uint8(16*y + x)
155
+			m.Cb[ci] = uint8(y + 16*x)
156
+			m.Cr[ci] = uint8(y + 16*x)
157
+		}
158
+	}
159
+	var out image.Image
160
+	b.ResetTimer()
111 161
 	for i := 0; i < b.N; i++ {
112
-		output = Resize(800, 0, input, interp)
162
+		out = Resize(benchWidth, benchHeight, m, interp)
113 163
 	}
114
-
115
-	output.At(0, 0)
164
+	out.At(0, 0)
116 165
 }
117 166
 
118
-func Benchmark_LargeJpegThumbNearestNeighbor(b *testing.B) {
119
-	jpegThumb(b, NearestNeighbor)
167
+func Benchmark_Nearest_YCC(b *testing.B) {
168
+	benchYCbCr(b, NearestNeighbor)
120 169
 }
121 170
 
122
-func Benchmark_LargeJpegThumbBilinear(b *testing.B) {
123
-	jpegThumb(b, Bilinear)
171
+func Benchmark_Bilinear_YCC(b *testing.B) {
172
+	benchYCbCr(b, Bilinear)
124 173
 }
125 174
 
126
-func Benchmark_LargeJpegThumbBicubic(b *testing.B) {
127
-	jpegThumb(b, Bicubic)
175
+func Benchmark_Bicubic_YCC(b *testing.B) {
176
+	benchYCbCr(b, Bicubic)
128 177
 }
129 178
 
130
-func Benchmark_LargeJpegThumbMitchellNetravali(b *testing.B) {
131
-	jpegThumb(b, MitchellNetravali)
179
+func Benchmark_Mitchell_YCC(b *testing.B) {
180
+	benchYCbCr(b, MitchellNetravali)
132 181
 }
133 182
 
134
-func Benchmark_LargeJpegThumbLanczos2(b *testing.B) {
135
-	jpegThumb(b, Lanczos2)
183
+func Benchmark_Lanczos2_YCC(b *testing.B) {
184
+	benchYCbCr(b, Lanczos2)
136 185
 }
137 186
 
138
-func Benchmark_LargeJpegThumbLanczos3(b *testing.B) {
139
-	jpegThumb(b, Lanczos3)
187
+func Benchmark_Lanczos3_YCC(b *testing.B) {
188
+	benchYCbCr(b, Lanczos3)
140 189
 }