-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsum_test.go
231 lines (192 loc) · 5.25 KB
/
sum_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) Efficient Go Authors
// Licensed under the Apache License 2.0.
package sum
import (
"fmt"
"os"
"path/filepath"
"testing"
"unsafe"
"github.com/efficientgo/core/errcapture"
"github.com/efficientgo/core/errors"
"github.com/efficientgo/core/testutil"
"github.com/efficientgo/examples/pkg/sum/sumtestutil"
"github.com/felixge/fgprof"
)
func createTestInput(fn string, numLen int) error {
_, err := createTestInputWithExpectedResult(fn, numLen)
return err
}
// lazyCreateTestInput creates test input on the fly if not cached.
// Read more in "Efficient Go"; Example 8-13.
func lazyCreateTestInput(tb testing.TB, numLines int) string {
tb.Helper()
fn := fmt.Sprintf("testdata/test.%v.txt", numLines)
if _, err := os.Stat(fn); errors.Is(err, os.ErrNotExist) {
testutil.Ok(tb, createTestInput(fn, numLines))
} else {
testutil.Ok(tb, err)
}
return fn
}
// BenchmarkSum benchmarks `Sum` function.
// NOTE(bwplotka): Test it with maximum of 4 CPU cores, given we don't allocate
// more in our production containers.
//
// Recommended run options:
/*
export ver=v1 && go test \
-run '^$' -bench '^BenchmarkSum$' \
-benchtime 10s -count 6 -cpu 4 -benchmem \
-memprofile=${ver}.mem.pprof -cpuprofile=${ver}.cpu.pprof \
| tee ${ver}.txt
*/
// Read more in "Efficient Go"; Example 8-1, 8-2, 8-10, 8-12
func BenchmarkSum(b *testing.B) {
fn := lazyCreateTestInput(b, 2e6)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Sum(fn)
testutil.Ok(b, err)
}
}
func createTestInputWithExpectedResult(fn string, numLen int) (sum int64, err error) {
if err := os.MkdirAll(filepath.Dir(fn), os.ModePerm); err != nil {
return 0, err
}
f, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return 0, errors.Wrap(err, "open")
}
defer errcapture.Do(&err, f.Close, "close file")
return sumtestutil.CreateTestInputWithExpectedResult(f, numLen)
}
// TestSum tests all sum implementations.
// Read more in "Efficient Go"; Example 8-9.
func TestSum(t *testing.T) {
testFile := filepath.Join(t.TempDir(), "input.txt")
expectedSum, err := createTestInputWithExpectedResult(testFile, 2e6)
testutil.Ok(t, err)
t.Run("simple", func(t *testing.T) {
for _, tcase := range []struct {
f func(string) (int64, error)
}{
{f: Sum}, {f: Sum2}, {f: Sum2_scanner}, {f: ConcurrentSum1}, {f: Sum3},
{f: Sum4}, {f: Sum4_atoi}, {f: Sum5}, {f: Sum5_line}, {f: Sum6}, {f: Sum7},
} {
t.Run("", func(t *testing.T) {
ret, err := tcase.f(testFile)
testutil.Ok(t, err)
testutil.Equals(t, expectedSum, ret)
})
}
})
t.Run("workers", func(t *testing.T) {
for _, tcase := range []struct {
f func(string, int) (int64, error)
}{
{f: ConcurrentSum2}, {f: ConcurrentSum3}, {f: ConcurrentSum4},
} {
t.Run("", func(t *testing.T) {
ret, err := tcase.f(testFile, 4)
testutil.Ok(t, err)
testutil.Equals(t, expectedSum, ret)
ret, err = tcase.f(testFile, 11)
testutil.Ok(t, err)
testutil.Equals(t, expectedSum, ret)
})
}
})
}
// TestBenchSum tests the benchmark (!).
// Read more in "Efficient Go"; Example 8-11.
func TestBenchSum(t *testing.T) {
benchmarkSum(testutil.NewTB(t))
}
func BenchmarkSum_tested(b *testing.B) {
benchmarkSum(testutil.NewTB(b))
}
func benchmarkSum(tb testutil.TB) {
fn := lazyCreateTestInput(tb, 2e6)
tb.ResetTimer()
for i := 0; i < tb.N(); i++ {
ret, err := Sum(fn)
testutil.Ok(tb, err)
if !tb.IsBenchmark() {
// More expensive result checks can be here.
testutil.Equals(tb, int64(6221600000), ret)
}
}
}
// BenchmarkSum_fgprof recommended run options:
// $ export ver=v1fg && go test -run '^$' -bench '^BenchmarkSum_fgprof' \
// -benchtime 60s -cpu 1 | tee ${ver}.txt
// Read more in "Efficient Go"; Example 10-2.
func BenchmarkSum_fgprof(b *testing.B) {
f, err := os.Create("fgprof.pprof")
testutil.Ok(b, err)
defer func() { testutil.Ok(b, f.Close()) }()
closeFn := fgprof.Start(f, fgprof.FormatPprof)
BenchmarkSum(b)
testutil.Ok(b, closeFn())
}
// BenchmarkSum_AcrossInputs benchmarks the sum in "table test" fashion.
// Read more in "Efficient Go"; Example 8-14.
func BenchmarkSum_AcrossInputs(b *testing.B) {
for _, tcase := range []struct {
numLines int
}{
{numLines: 0},
{numLines: 1e2},
{numLines: 1e4},
{numLines: 1e6},
{numLines: 2e6},
} {
b.Run(fmt.Sprintf("lines-%d", tcase.numLines), func(b *testing.B) {
b.ReportAllocs()
fn := lazyCreateTestInput(b, tcase.numLines)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Sum(fn)
testutil.Ok(b, err)
}
})
}
}
var sink string
func BenchmarkStringConv(b *testing.B) {
b.Run("string", func(b *testing.B) {
b.ReportAllocs()
s := make([]byte, 1e6)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = string(s)
}
})
b.Run("zero", func(b *testing.B) {
b.ReportAllocs()
s := make([]byte, 1e6)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = zeroCopyToString(s)
}
})
b.Run("zero?", func(b *testing.B) {
b.ReportAllocs()
s := make([]byte, 1e6)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = string(zeroCopyToString(s))
}
})
b.Run("copy", func(b *testing.B) {
b.ReportAllocs()
s := make([]byte, 1e6)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c := make([]byte, 1e6)
copy(c, s)
sink = *((*string)(unsafe.Pointer(&c)))
}
})
}