forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_buffer_test.go
More file actions
86 lines (81 loc) · 1.81 KB
/
write_buffer_test.go
File metadata and controls
86 lines (81 loc) · 1.81 KB
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
package clickhouse
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_WriteBuffer(t *testing.T) {
{
if wb := wb(10); assert.Equal(t, int(0), wb.len()) {
if assert.Len(t, wb.bytes(), 0) {
var buf bytes.Buffer
if err := wb.writeTo(&buf); assert.NoError(t, err) {
assert.Len(t, buf.Bytes(), 0)
}
}
}
}
{
wb := wb(10)
copy(wb.alloc(5), []byte{1, 2, 3, 4, 5})
copy(wb.alloc(5), []byte{6, 7, 8, 9, 10})
copy(wb.alloc(10), []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
if assert.Len(t, wb.chunks, 2) {
if assert.Equal(t, int(20), wb.len()) {
for n, chunck := range wb.chunks {
t.Logf("chunk[%d]: %v", n, chunck)
}
wb.free()
{
if assert.Len(t, wb.chunks, 1) {
if assert.Equal(t, int(0), wb.len()) {
assert.Equal(t, int(10), cap(wb.chunks[0]))
}
}
}
}
}
}
{
wb := wb(10)
wb.Write([]byte{1, 2, 3, 4, 5})
wb.Write([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})
if assert.Len(t, wb.chunks, 2) {
if assert.Equal(t, int(16), wb.len()) {
for n, chunck := range wb.chunks {
t.Logf("chunk[%d]: %v", n, chunck)
}
wb.free()
{
if assert.Len(t, wb.chunks, 1) {
if assert.Equal(t, int(0), wb.len()) {
assert.Equal(t, int(10), cap(wb.chunks[0]))
}
}
}
}
}
}
}
func Benchmark_WriteBuffer(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf := wb(1000)
for n := 0; n < 10000; n++ {
copy(buf.alloc(4), []byte{1, 2, 3, 4})
buf.Write([]byte{12, 3, 4, 5, 6, 7, 8, 9})
}
buf.free()
}
}
func Benchmark_BytesBuffer(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf := bytes.NewBuffer(make([]byte, 0, 1000))
for n := 0; n < 10000; n++ {
buf.Write([]byte{1, 2, 3, 4})
buf.Write([]byte{12, 3, 4, 5, 6, 7, 8, 9})
}
buf.Reset()
}
}