-
Notifications
You must be signed in to change notification settings - Fork 35
/
bench_test.go
192 lines (169 loc) · 3.59 KB
/
bench_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package magick
import (
"bytes"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/nfnt/resize"
)
var (
wizard = "test_data/wizard.png"
newton = "test_data/Newtons_cradle_animation_book_2.gif"
lenna = "test_data/lenna.jpg"
)
func BenchmarkResizePng(b *testing.B) {
im := decodeFile(b, "wizard.png")
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
_, _ = im.Resize(240, 180, FLanczos)
}
}
func BenchmarkResizePngGo(b *testing.B) {
f, err := os.Open(wizard)
if err != nil {
b.Fatal(err)
}
defer f.Close()
im, _, err := image.Decode(f)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
_ = resize.Resize(240, 180, im, resize.Lanczos2)
}
}
func benchmarkDecode(b *testing.B, file string) {
data, err := ioutil.ReadFile(file)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
if _, err := DecodeData(data); err != nil {
b.Fatal(err)
}
}
}
func benchmarkDecodeGo(b *testing.B, file string) {
data, err := ioutil.ReadFile(file)
if err != nil {
b.Fatal(err)
}
r := bytes.NewReader(data)
if filepath.Ext(file) == ".gif" {
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
if _, err := gif.DecodeAll(r); err != nil {
b.Fatal(err)
}
r.Seek(0, 0)
}
} else {
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
if _, _, err := image.Decode(r); err != nil {
b.Fatal(err)
}
r.Seek(0, 0)
}
}
}
func benchmarkEncode(b *testing.B, file string, format string, quality uint) {
data, err := ioutil.ReadFile(file)
if err != nil {
b.Fatal(err)
}
im, err := DecodeData(data)
if err != nil {
b.Fatal(err)
}
info := NewInfo()
info.SetFormat(format)
info.SetQuality(quality)
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
if err := im.Encode(ioutil.Discard, info); err != nil {
b.Fatal(err)
}
}
}
func benchmarkEncodeGo(b *testing.B, file string, format string, quality uint) {
f, err := os.Open(file)
if err != nil {
b.Fatal(err)
}
defer f.Close()
im, _, err := image.Decode(f)
if err != nil {
b.Fatal(err)
}
switch format {
case "jpeg":
opts := &jpeg.Options{
Quality: int(quality),
}
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
if err := jpeg.Encode(ioutil.Discard, im, opts); err != nil {
b.Fatal(err)
}
}
case "png":
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
if err := png.Encode(ioutil.Discard, im); err != nil {
b.Fatal(err)
}
}
default:
b.Fatalf("format %s is not supported", format)
}
}
func BenchmarkDecodePng(b *testing.B) {
benchmarkDecode(b, wizard)
}
func BenchmarkDecodePngGo(b *testing.B) {
benchmarkDecodeGo(b, wizard)
}
func BenchmarkDecodeGif(b *testing.B) {
benchmarkDecode(b, newton)
}
func BenchmarkDecodeGifGo(b *testing.B) {
benchmarkDecodeGo(b, newton)
}
func BenchmarkDecodeJpeg(b *testing.B) {
benchmarkDecode(b, lenna)
}
func BenchmarkDecodeJpegGo(b *testing.B) {
benchmarkDecodeGo(b, lenna)
}
func BenchmarkEncodePng(b *testing.B) {
benchmarkEncode(b, lenna, "png", 0)
}
func BenchmarkEncodePngGo(b *testing.B) {
benchmarkEncodeGo(b, lenna, "png", 0)
}
func BenchmarkEncodeJpeg(b *testing.B) {
benchmarkEncode(b, wizard, "jpeg", 60)
}
func BenchmarkEncodeJpegGo(b *testing.B) {
benchmarkEncodeGo(b, wizard, "jpeg", 60)
}
func BenchmarkEncodeJpegHQ(b *testing.B) {
benchmarkEncode(b, wizard, "jpeg", 100)
}
func BenchmarkEncodeJpegHQGo(b *testing.B) {
benchmarkEncodeGo(b, wizard, "jpeg", 100)
}
func BenchmarkEncodeJpegLQ(b *testing.B) {
benchmarkEncode(b, wizard, "jpeg", 10)
}
func BenchmarkEncodeJpegLQGo(b *testing.B) {
benchmarkEncodeGo(b, wizard, "jpeg", 10)
}