-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiocpb_test.go
72 lines (57 loc) · 1.09 KB
/
iocpb_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
package iocpb
import (
"io"
"sync"
"testing"
)
type writer struct {
io.Writer
}
func (w *writer) Write(p []byte) (n int, err error) {
return len(p), nil
}
type reader struct {
pos int
len int
io.Reader
}
func (r *reader) Read(p []byte) (n int, err error) {
if r.pos == r.len {
return 0, io.EOF
}
read := r.len - r.pos
if read > len(p) {
read = len(p)
}
r.pos += read
return read, nil
}
func BenchmarkIoCopy(b *testing.B) {
for i := 0; i < b.N; i++ {
w := &writer{}
r := &reader{len: 1024 * 1024}
io.Copy(w, r)
}
}
func BenchmarkIoCopyBufferWithPool(b *testing.B) {
pool := sync.Pool{
New: func() interface{} {
return make([]byte, 32*1024)
},
}
for i := 0; i < b.N; i++ {
w := &writer{}
r := &reader{len: 1024 * 1024}
buffer := pool.Get().([]byte)
io.CopyBuffer(w, r, buffer)
pool.Put(buffer)
}
}
func BenchmarkIoCopyBuffer(b *testing.B) {
buffer := make([]byte, 32*1024)
for i := 0; i < b.N; i++ {
w := &writer{}
r := &reader{len: 1024 * 1024}
io.CopyBuffer(w, r, buffer)
}
}