-
Notifications
You must be signed in to change notification settings - Fork 198
/
image_benchmark_test.go
74 lines (70 loc) · 1.43 KB
/
image_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
package imageserver_test
import (
"testing"
. "github.com/pierrre/imageserver"
"github.com/pierrre/imageserver/testdata"
)
func BenchmarkImageMarshalBinary(b *testing.B) {
for _, tc := range []struct {
name string
im *Image
}{
{"Small", testdata.Small},
{"Medium", testdata.Medium},
{"Large", testdata.Large},
{"Huge", testdata.Huge},
} {
b.Run(tc.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := tc.im.MarshalBinary()
if err != nil {
b.Fatal(err)
}
}
b.SetBytes(int64(len(tc.im.Data)))
})
}
}
func BenchmarkImageUnmarshalBinary(b *testing.B) {
for _, tcnc := range []struct {
name string
nocopy bool
}{
{"Normal", false},
{"NoCopy", true},
} {
b.Run(tcnc.name, func(b *testing.B) {
for _, tcim := range []struct {
name string
im *Image
}{
{"Small", testdata.Small},
{"Medium", testdata.Medium},
{"Large", testdata.Large},
{"Huge", testdata.Huge},
} {
b.Run(tcim.name, func(b *testing.B) {
data, err := tcim.im.MarshalBinary()
if err != nil {
b.Fatal(err)
}
imNew := new(Image)
var m func([]byte) error
if tcnc.nocopy {
m = imNew.UnmarshalBinaryNoCopy
} else {
m = imNew.UnmarshalBinary
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := m(data)
if err != nil {
b.Fatal(err)
}
}
b.SetBytes(int64(len(tcim.im.Data)))
})
}
})
}
}