-
Notifications
You must be signed in to change notification settings - Fork 4
/
argwriter_test.go
108 lines (92 loc) · 2.16 KB
/
argwriter_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
package main
import (
"bytes"
"flag"
"io"
"testing"
)
// The different versions:
// Indirect = uses the writePr func with a closure
// Direct = doesn't use the writePtr func
// Val suffix = uses a value receiver
// Ptr suffix = uses a pointer receiver
// Val_Val: Creates a Val, and uses the Val method
// Val_Ptr: Creates a Val, but uses the Ptr method.
var (
dataToWrite []byte
numBytes = flag.Int("numBytes", 50, "Number of bytes to write")
)
func init() {
flag.Parse()
const source = "hello world"
lenSource := len(source)
for i := 0; i < *numBytes; i++ {
dataToWrite = append(dataToWrite, source[i%lenSource])
}
}
type addClose struct {
io.Writer
}
func (addClose) Close() error {
return nil
}
func writerCloser(writer io.Writer) io.WriteCloser {
return addClose{writer}
}
func Writer(writer io.Writer) (io.WriteCloser, error) {
return writerCloser(writer), nil
}
func BenchmarkWriteIndirectVal_Val(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
newArgWriterVal(Writer(&buf)).WriteIndirectVal(dataToWrite)
buf.Reset()
}
}
func BenchmarkWriteIndirectVal_Ptr(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
newArgWriterPtr(Writer(&buf)).WriteIndirectVal(dataToWrite)
buf.Reset()
}
}
func BenchmarkWriteIndirectPtr(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
newArgWriterPtr(Writer(&buf)).WriteIndirectPtr(dataToWrite)
buf.Reset()
}
}
func BenchmarkWriteDirectVal_Val(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
newArgWriterVal(Writer(&buf)).WriteDirectVal(dataToWrite)
buf.Reset()
}
}
func BenchmarkWriteDirectVal_Ptr(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
newArgWriterPtr(Writer(&buf)).WriteDirectVal(dataToWrite)
buf.Reset()
}
}
func BenchmarkWriteDirectPtr(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
newArgWriterPtr(Writer(&buf)).WriteDirectPtr(dataToWrite)
buf.Reset()
}
}
func BenchmarkNoWriter(b *testing.B) {
var buf bytes.Buffer
for i := 0; i < b.N; i++ {
writer, err := Writer(&buf)
if err == nil {
_, err = writer.Write(dataToWrite)
if err == nil {
err = writer.Close()
}
}
}
}