-
Notifications
You must be signed in to change notification settings - Fork 53
/
insert_bench_test.go
122 lines (112 loc) · 2.25 KB
/
insert_bench_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
package ch
import (
"context"
"fmt"
"strings"
"testing"
"github.com/go-faster/errors"
"github.com/ClickHouse/ch-go/cht"
"github.com/ClickHouse/ch-go/proto"
)
func BenchmarkInsert(b *testing.B) {
cht.Skip(b)
srv := cht.New(b)
bench := func(data proto.ColInput) func(b *testing.B) {
return func(b *testing.B) {
ctx := context.Background()
c, err := Dial(ctx, Options{
Address: srv.TCP,
Compression: CompressionDisabled,
})
if err != nil {
b.Fatal(errors.Wrap(err, "dial"))
}
b.Cleanup(func() {
if err := c.Do(ctx, Query{
Body: "DROP TABLE IF EXISTS test_table",
}); err != nil {
b.Logf("Cleanup failed: %+v", err)
}
_ = c.Close()
})
if err := c.Do(ctx, Query{
Body: fmt.Sprintf("CREATE TABLE IF NOT EXISTS test_table (row %s) ENGINE = Null", data.Type()),
}); err != nil {
b.Fatal(err)
}
var tmp proto.Buffer
data.EncodeColumn(&tmp)
b.SetBytes(int64(len(tmp.Buf)))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := c.Do(ctx, Query{
Body: "INSERT INTO test_table VALUES",
Input: []proto.InputColumn{
{Name: "row", Data: data},
},
}); err != nil {
b.Fatal(err)
}
}
}
}
for _, gen := range []struct {
name string
getData func(rows int) proto.ColInput
maxRows int
}{
{
"ColInt64",
func(rows int) proto.ColInput {
var data proto.ColInt64
for i := 0; i < rows; i++ {
data.Append(int64(i))
}
return data
},
-1,
},
{
"SmallColStr",
func(rows int) proto.ColInput {
var data proto.ColStr
for i := 0; i < rows; i++ {
data.Append(fmt.Sprintf("%016x", i))
}
return data
},
1_000_000,
},
{
"BigColStr",
func(rows int) proto.ColInput {
var (
data proto.ColStr
scratch = strings.Repeat("abcd", 1024)
)
for i := 0; i < rows; i++ {
data.Append(scratch)
}
return data
},
100_000,
},
} {
b.Run(gen.name, func(b *testing.B) {
for _, rows := range []int{
10_000,
100_000,
1_000_000,
10_000_000,
100_000_000,
} {
if gen.maxRows > 0 && rows > gen.maxRows {
continue
}
data := gen.getData(rows)
b.Run(fmt.Sprintf("Rows%d", rows), bench(data))
}
})
}
}