Skip to content

Commit

Permalink
rttanalysis: test virtual table descriptors cache
Browse files Browse the repository at this point in the history
This commit adds just tests without the caching changes, so we will see
round-trip reductions in a subsequent commit. For test cases that
execute multiple statements, the rttanalysis framework now aggregates
the round-trip count of each statement.

Release note: None
  • Loading branch information
Jason Chan committed Jul 18, 2022
1 parent 9365878 commit 7d021d3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
1 change: 1 addition & 0 deletions pkg/bench/rttanalysis/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//pkg/base",
"//pkg/kv/kvclient/kvcoord",
"//pkg/sql",
"//pkg/sql/parser",
"//pkg/testutils",
"//pkg/testutils/skip",
"//pkg/testutils/sqlutils",
Expand Down
50 changes: 35 additions & 15 deletions pkg/bench/rttanalysis/rtt_analysis_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -117,32 +118,51 @@ func executeRoundTripTest(b testingB, tc RoundTripBenchTestCase, cc ClusterConst
b.StopTimer()
var r tracingpb.Recording

// The statement trace records individual statements, but we may want to
// execute multiple SQL statements. Note that multi-statement traces won't
// count round trips correctly if there are duplicate statements.
statements, err := parser.Parse(tc.Stmt)
if err != nil {
panic(err)
}

// Do an extra iteration and don't record it in order to deal with effects of
// running it the first time.
for i := 0; i < b.N()+1; i++ {
sql.Exec(b, "CREATE DATABASE bench;")
sql.Exec(b, tc.Setup)
cluster.clearStatementTrace(tc.Stmt)
for _, statement := range statements {
cluster.clearStatementTrace(statement.SQL)
}

b.StartTimer()
sql.Exec(b, tc.Stmt)
b.StopTimer()
var ok bool
r, ok = cluster.getStatementTrace(tc.Stmt)
if !ok {
b.Fatalf(
"could not find number of round trips for statement: %s",
tc.Stmt,
)
}

// If there's a retry error then we're just going to throw away this
// run.
rt, hasRetry := countKvBatchRequestsInRecording(r)
if hasRetry {
i--
} else if i > 0 { // skip the initial iteration
roundTrips += rt
total := 0
for _, statement := range statements {
r, ok = cluster.getStatementTrace(statement.SQL)
if !ok {
b.Fatalf(
"could not find number of round trips for statement: %s",
statement.SQL,
)
}

// If there's a retry error then we're just going to throw away this
// run.
rt, hasRetry := countKvBatchRequestsInRecording(r)
if hasRetry {
i--
ok = false
break
} else if i > 0 { // skip the initial iteration
total += rt
}
}
if ok {
roundTrips += total
}

sql.Exec(b, "DROP DATABASE bench;")
Expand Down
2 changes: 2 additions & 0 deletions pkg/bench/rttanalysis/testdata/benchmark_expectations
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ exp,benchmark
18,Truncate/truncate_2_column_2_rows
1,VirtualTableQueries/select_crdb_internal.invalid_objects_with_1_fk
1,VirtualTableQueries/select_crdb_internal.tables_with_1_fk
6,VirtualTableQueries/virtual_table_cache_with_point_lookups
21,VirtualTableQueries/virtual_table_cache_with_schema_change
27 changes: 27 additions & 0 deletions pkg/bench/rttanalysis/virtual_table_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,32 @@ CREATE TABLE t2 (i INT PRIMARY KEY, j INT REFERENCES t1(i));
`,
Stmt: `SELECT * FROM "".crdb_internal.invalid_objects`,
},
// This checks that descriptors are still cached after they are written. We
// expect the second and third selects not to go to KV because the
// descriptors were cached after the first lookup.
{
Name: "virtual table cache with schema change",
Setup: `
CREATE TABLE t1 (i INT PRIMARY KEY);
CREATE TABLE t2 (i INT PRIMARY KEY, j INT);`,
Stmt: `
SELECT * FROM crdb_internal.tables;
ALTER TABLE t1 ADD COLUMN j INT;
SELECT * FROM crdb_internal.table_columns;
CREATE INDEX idx ON t2 (j);
SELECT * FROM crdb_internal.index_columns;`,
},
// This checks that catalog point lookups following a virtual table scan
// access cached descriptors.
{
Name: "virtual table cache with point lookups",
Setup: `
CREATE TABLE t1 (i INT PRIMARY KEY);
CREATE TABLE t2 (i INT PRIMARY KEY, j INT);`,
Stmt: `
SELECT * FROM crdb_internal.tables;
SELECT * FROM t1;
SELECT * FROM t2;`,
},
})
}

0 comments on commit 7d021d3

Please sign in to comment.