Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: Benchmark has_schema_privilege, has_sequence_privilege, has_table_privilege #75855

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 59 additions & 5 deletions pkg/bench/rttanalysis/orm_queries_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

package rttanalysis

import "testing"
import (
"fmt"
"strings"
"testing"
)

func BenchmarkORMQueries(b *testing.B) { reg.Run(b) }
func init() {
Expand Down Expand Up @@ -162,17 +166,59 @@ WHERE
},

{
Name: "has_table_privilege real table",
Setup: `CREATE TABLE t(a int primary key, b int)`,
Stmt: `SELECT has_table_privilege('t', 'SELECT')`,
Name: "has_schema_privilege 1",
Setup: `CREATE SCHEMA s`,
Stmt: `SELECT has_schema_privilege('s', 'CREATE')`,
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add more tests to see how it scales with number of databases/schemas/sequences?

basically, in the setup, it can create multiple entities, and the Stmt to test can be something like

SELECT has_database_privilege(name, 'CREATE') FROM crdb_internal.databases;

for schemas, something like

SELECT has_schema_privilege(nspame, 'CREATE') FROM pg_catalog.pg_namespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked about the tests not supporting has_database_privilege offline. Updated the others.


{
Name: "has_table_privilege virtual table",
Name: "has_schema_privilege 3",
Setup: repeat("CREATE SCHEMA s%d_3", 3, "; "),
Stmt: "SELECT " + repeat("has_schema_privilege('s%d_3', 'CREATE')", 3, ", "),
},

{
Name: "has_schema_privilege 5",
Setup: repeat("CREATE SCHEMA s%d_5", 5, "; "),
Stmt: "SELECT " + repeat("has_schema_privilege('s%d_5', 'CREATE')", 5, ", "),
},

{
Name: "has_sequence_privilege 1",
Setup: `CREATE SEQUENCE seq`,
Stmt: `SELECT has_sequence_privilege('seq', 'SELECT')`,
},

{
Name: "has_sequence_privilege 3",
Setup: repeat("CREATE SEQUENCE seq%d_3", 3, "; "),
Stmt: "SELECT " + repeat("has_sequence_privilege('seq%d_3', 'SELECT')", 3, ", "),
},

{
Name: "has_sequence_privilege 5",
Setup: repeat("CREATE SEQUENCE seq%d_5", 5, ";"),
Stmt: "SELECT " + repeat("has_sequence_privilege('seq%d_5', 'SELECT')", 5, ", "),
},

{
Name: "has_table_privilege 1",
Setup: `CREATE TABLE t(a int primary key, b int)`,
Stmt: `SELECT has_table_privilege('t', 'SELECT')`,
},

{
Name: "has_table_privilege 3",
Setup: repeat("CREATE TABLE t%d_3(a int primary key, b int)", 3, "; "),
Stmt: "SELECT " + repeat("has_table_privilege('t%d_3', 'SELECT')", 3, ", "),
},

{
Name: "has_table_privilege 5",
Setup: repeat("CREATE TABLE t%d_5(a int primary key, b int)", 5, "; "),
Stmt: "SELECT " + repeat("has_table_privilege('t%d_5', 'SELECT')", 5, ", "),
},

{
Name: "has_column_privilege using attnum",
Setup: `CREATE TABLE t(a int primary key, b int)`,
Expand Down Expand Up @@ -241,3 +287,11 @@ ORDER BY relname DESC, input`,
},
})
}

func repeat(format string, times int, sep string) string {
formattedStrings := make([]string, times)
for i := 0; i < times; i++ {
formattedStrings[i] = fmt.Sprintf(format, i)
}
return strings.Join(formattedStrings, sep)
}
11 changes: 9 additions & 2 deletions pkg/bench/rttanalysis/testdata/benchmark_expectations
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,15 @@ exp,benchmark
2,ORMQueries/django_table_introspection_8_tables
2,ORMQueries/has_column_privilege_using_attnum
2,ORMQueries/has_column_privilege_using_column_name
2,ORMQueries/has_table_privilege_real_table
2,ORMQueries/has_table_privilege_virtual_table
1,ORMQueries/has_schema_privilege_1
1,ORMQueries/has_schema_privilege_3
1,ORMQueries/has_schema_privilege_5
2,ORMQueries/has_sequence_privilege_1
4,ORMQueries/has_sequence_privilege_3
6,ORMQueries/has_sequence_privilege_5
2,ORMQueries/has_table_privilege_1
4,ORMQueries/has_table_privilege_3
6,ORMQueries/has_table_privilege_5
Comment on lines +63 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤮 😢

15,ORMQueries/information_schema._pg_index_position
2,ORMQueries/pg_attribute
2,ORMQueries/pg_class
Expand Down
2 changes: 1 addition & 1 deletion pkg/bench/rttanalysis/validate_benchmark_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func runBenchmarkExpectationTests(t *testing.T, r *Registry) {

var results resultSet
var wg sync.WaitGroup
concurrency := ((system.NumCPU()*4 - 1) / r.numNodes) + 1 // arbitrary
concurrency := ((system.NumCPU() - 1) / r.numNodes) + 1 // arbitrary
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spoke with @ajwerner on slack about this. Removing the *4 makes the tests run much faster with larger (>4) rewrite iterations.

limiter := quotapool.NewIntPool("rttanalysis", uint64(concurrency))
isRewrite := *rewriteFlag
for b, cases := range r.r {
Expand Down