-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathbase_test.go
183 lines (166 loc) · 4.99 KB
/
base_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
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package execinfra
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// Test the behavior of Run in the presence of errors that switch to the drain
// and forward metadata mode.
func TestRunDrain(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
// A source with no rows and 2 ProducerMetadata messages.
src := &RowChannel{}
src.InitWithBufSizeAndNumSenders(nil, 10, 1)
src.Push(nil /* row */, &execinfrapb.ProducerMetadata{Err: fmt.Errorf("test")})
src.Push(nil /* row */, nil /* meta */)
src.Start(ctx)
// A receiver that is marked as done consuming rows so that Run will
// immediately move from forwarding rows and metadata to draining metadata.
buf := &RowChannel{}
buf.InitWithBufSizeAndNumSenders(nil, 10, 1)
buf.ConsumerDone()
Run(ctx, src, buf)
if src.ConsumerStatus != DrainRequested {
t.Fatalf("expected DrainRequested, but found %d", src.ConsumerStatus)
}
}
// Benchmark a pipeline of RowChannels.
func BenchmarkRowChannelPipeline(b *testing.B) {
for _, length := range []int{1, 2, 3, 4} {
b.Run(fmt.Sprintf("length=%d", length), func(b *testing.B) {
rc := make([]RowChannel, length)
var wg sync.WaitGroup
wg.Add(len(rc))
for i := range rc {
rc[i].InitWithNumSenders(types.OneIntCol, 1)
go func(i int) {
defer wg.Done()
cur := &rc[i]
var next *RowChannel
if i+1 != len(rc) {
next = &rc[i+1]
}
for {
row, meta := cur.Next()
if row == nil {
if next != nil {
next.ProducerDone()
}
break
}
if next != nil {
_ = next.Push(row, meta)
}
}
}(i)
}
row := rowenc.EncDatumRow{
rowenc.DatumToEncDatum(types.Int, tree.NewDInt(tree.DInt(1))),
}
b.SetBytes(int64(8 * 1 * 1))
for i := 0; i < b.N; i++ {
_ = rc[0].Push(row, nil /* meta */)
}
rc[0].ProducerDone()
wg.Wait()
})
}
}
func BenchmarkMultiplexedRowChannel(b *testing.B) {
numRows := 1 << 16
row := rowenc.EncDatumRow{randgen.IntEncDatum(0)}
for _, senders := range []int{2, 4, 8} {
b.Run(fmt.Sprintf("senders=%d", senders), func(b *testing.B) {
b.SetBytes(int64(senders * numRows * 8))
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
wg.Add(senders + 1)
mrc := &RowChannel{}
mrc.InitWithNumSenders(types.OneIntCol, senders)
go func() {
for {
if r, _ := mrc.Next(); r == nil {
break
}
}
wg.Done()
}()
for j := 0; j < senders; j++ {
go func() {
for k := 0; k < numRows; k++ {
mrc.Push(row, nil /* meta */)
}
mrc.ProducerDone()
wg.Done()
}()
}
wg.Wait()
}
})
}
}
// BenchmarkRenderPlanning is similar to BenchmarkRenderPlanning, but
// varies the number of columns available in the input to the render
// expressions.
func BenchmarkRenderPlanning(b *testing.B) {
defer leaktest.AfterTest(b)()
defer log.Scope(b).Close(b)
ctx := context.Background()
s, db, _ := serverutils.StartServer(b, base.TestServerArgs{SQLMemoryPoolSize: 10 << 30})
defer s.Stopper().Stop(ctx)
sqlDB := sqlutils.MakeSQLRunner(db)
sqlDB.Exec(b, "SET CLUSTER SETTING sql.defaults.vectorize = off")
for _, numCols := range []int{1 << 4, 1 << 6, 1 << 9} {
sqlDB.Exec(b, "DROP TABLE IF EXISTS bench")
var sb strings.Builder
sb.WriteString("CREATE TABLE bench (id INT PRIMARY KEY")
for i := 0; i < numCols; i++ {
sb.WriteString(", ")
sb.WriteString(fmt.Sprintf("col%d STRING DEFAULT 'foobarbaz'", i))
}
sb.WriteString(")")
createTable := sb.String()
sqlDB.Exec(b, createTable)
sqlDB.Exec(b, fmt.Sprintf(`INSERT INTO bench(id) SELECT i FROM generate_series(1, 256) AS g(i)`))
sqlDB.Exec(b, "ANALYZE bench")
for _, numRenders := range []int{1, 2 << 8, 1 << 12} {
var sb strings.Builder
sb.WriteString("SELECT ")
for i := 0; i < numRenders; i++ {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(fmt.Sprintf("col0 || 'x' AS test%d", i+1))
}
sb.WriteString(" FROM bench")
query := sb.String()
b.Run(fmt.Sprintf("col=%d/renders=%d", numCols, numRenders), func(b *testing.B) {
for i := 0; i < b.N; i++ {
sqlDB.Exec(b, query)
}
})
}
}
}