Skip to content

Commit

Permalink
benchmark: prepared statement flow
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Dec 30, 2024
1 parent 9383943 commit c0d29fe
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 80 deletions.
108 changes: 108 additions & 0 deletions go/test/endtoend/preparestmt/bechmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package preparestmt

import (
"math/rand/v2"
"testing"

"github.com/icrowley/fake"
)

/*
export ver=v1 p=~/benchmark && go test \
-run '^$' -bench '^BenchmarkPreparedStmt' \
-benchtime 10s -count 6 -cpu 4 \
| tee $p/${ver}.txt
*/
func BenchmarkPreparedStmt(b *testing.B) {
dbo := Connect(b)
defer dbo.Close()

// prepare statement
insertStmt := `insert into sks.t1 (name, age, email, created_at, is_active) values(?, ?, ?, current_timestamp, ?)`
selectStmt := `select id, name, age, email from sks.t1 where age between ? and ? and is_active = ?`
updateStmt := `update sks.t1 set is_active = ? where id = ?`
deleteStmt := `delete from sks.t1 where is_active = ? and age = ?`

iStmt, err := dbo.Prepare(insertStmt)
if err != nil {
b.Fatal(err)
}
defer iStmt.Close()

b.Run("Insert", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := iStmt.Exec(fake.FirstName(), rand.IntN(100), fake.EmailAddress(), rand.IntN(2))
if err != nil {
b.Fatal(err)
}
}
})

sStmt, err := dbo.Prepare(selectStmt)
if err != nil {
b.Fatal(err)
}
defer sStmt.Close()

b.Run("Select", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
age := rand.IntN(80)
r, err := sStmt.Query(age, age+20, rand.IntN(2))
if err != nil {
b.Fatal(err)
}
r.Close()
}
})

uStmt, err := dbo.Prepare(updateStmt)
if err != nil {
b.Fatal(err)
}
defer sStmt.Close()

b.Run("Update", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = uStmt.Exec(rand.IntN(2), rand.IntN(2000))
if err != nil {
b.Fatal(err)
}
}
})

dStmt, err := dbo.Prepare(deleteStmt)
if err != nil {
b.Fatal(err)
}
defer sStmt.Close()

b.Run("Delete", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = dStmt.Exec(rand.IntN(2), rand.IntN(100))
if err != nil {
b.Fatal(err)
}
}
})

}
90 changes: 25 additions & 65 deletions go/test/endtoend/preparestmt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package preparestmt

import (
"database/sql"
_ "embed"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -51,7 +52,7 @@ type DBInfo struct {
}

func init() {
dbInfo.KeyspaceName = keyspaceName
dbInfo.KeyspaceName = uks
dbInfo.Username = "testuser1"
dbInfo.Password = "testpassword1"
dbInfo.Params = []string{
Expand All @@ -65,9 +66,9 @@ var (
clusterInstance *cluster.LocalProcessCluster
dbInfo DBInfo
hostname = "localhost"
keyspaceName = "test_keyspace"
uks = "uks"
sks = "sks"
testingID = 1
tableName = "vt_prepare_stmt_test"
cell = "zone1"
mysqlAuthServerStatic = "mysql_auth_server_static.json"
jsonExample = `{
Expand Down Expand Up @@ -108,57 +109,18 @@ var (
}
}
}`
sqlSchema = `create table ` + tableName + ` (
id bigint auto_increment,
msg varchar(64),
keyspace_id bigint(20) unsigned NOT NULL,
tinyint_unsigned TINYINT,
bool_signed BOOL,
smallint_unsigned SMALLINT,
mediumint_unsigned MEDIUMINT,
int_unsigned INT,
float_unsigned FLOAT(10,2),
double_unsigned DOUBLE(16,2),
decimal_unsigned DECIMAL,
t_date DATE,
t_datetime DATETIME,
t_datetime_micros DATETIME(6),
t_time TIME,
t_timestamp TIMESTAMP,
c8 bit(8) DEFAULT NULL,
c16 bit(16) DEFAULT NULL,
c24 bit(24) DEFAULT NULL,
c32 bit(32) DEFAULT NULL,
c40 bit(40) DEFAULT NULL,
c48 bit(48) DEFAULT NULL,
c56 bit(56) DEFAULT NULL,
c63 bit(63) DEFAULT NULL,
c64 bit(64) DEFAULT NULL,
json_col JSON,
text_col TEXT,
data longblob,
tinyint_min TINYINT,
tinyint_max TINYINT,
tinyint_pos TINYINT,
tinyint_neg TINYINT,
smallint_min SMALLINT,
smallint_max SMALLINT,
smallint_pos SMALLINT,
smallint_neg SMALLINT,
medint_min MEDIUMINT,
medint_max MEDIUMINT,
medint_pos MEDIUMINT,
medint_neg MEDIUMINT,
int_min INT,
int_max INT,
int_pos INT,
int_neg INT,
bigint_min BIGINT,
bigint_max BIGINT,
bigint_pos BIGINT,
bigint_neg BIGINT,
primary key (id)
) Engine=InnoDB`

//go:embed uSchema.sql
uSQLSchema string

//go:embed uVschema.json
uVschema string

//go:embed sSchema.sql
sSQLSchema string

//go:embed sVschema.json
sVschema string
)

func TestMain(m *testing.M) {
Expand All @@ -185,15 +147,13 @@ func TestMain(m *testing.M) {
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: keyspaceName,
SchemaSQL: sqlSchema,
}
uks := &cluster.Keyspace{Name: "uks"}
if err := clusterInstance.StartUnshardedKeyspace(*keyspace, 1, false); err != nil {
ks := cluster.Keyspace{Name: uks, SchemaSQL: uSQLSchema, VSchema: uVschema}
if err := clusterInstance.StartUnshardedKeyspace(ks, 1, false); err != nil {
return 1, err
}
if err := clusterInstance.StartUnshardedKeyspace(*uks, 0, false); err != nil {

ks = cluster.Keyspace{Name: sks, SchemaSQL: sSQLSchema, VSchema: sVschema}
if err := clusterInstance.StartKeyspace(ks, []string{"-"}, 0, false); err != nil {
return 1, err
}

Expand All @@ -203,7 +163,7 @@ func TestMain(m *testing.M) {
vtgateInstance.ExtraArgs = []string{
"--mysql_server_query_timeout", "1s",
"--mysql_auth_server_static_file", clusterInstance.TmpDirectory + "/" + mysqlAuthServerStatic,
"--mysql_server_version", "8.0.16-7",
"--pprof-http",
}

// Start vtgate
Expand Down Expand Up @@ -251,7 +211,7 @@ func createConfig(name, data string) error {
}

// Connect will connect the vtgate through mysql protocol.
func Connect(t *testing.T, params ...string) *sql.DB {
func Connect(t testing.TB, params ...string) *sql.DB {
dbo, err := sql.Open("mysql", dbInfo.ConnectionString(params...))
require.Nil(t, err)
return dbo
Expand Down Expand Up @@ -285,7 +245,7 @@ func execErr(dbo *sql.DB, stmt string, params ...any) *mysql.MySQLError {
func selectWhere(t *testing.T, dbo *sql.DB, where string, params ...any) []tableData {
var out []tableData
// prepare query
qry := "SELECT msg, data, text_col, t_datetime, t_datetime_micros FROM " + tableName
qry := "SELECT msg, data, text_col, t_datetime, t_datetime_micros FROM vt_prepare_stmt_test"
if where != "" {
qry += " WHERE (" + where + ")"
}
Expand All @@ -307,7 +267,7 @@ func selectWhere(t *testing.T, dbo *sql.DB, where string, params ...any) []table
func selectWhereWithTx(t *testing.T, tx *sql.Tx, where string, params ...any) []tableData {
var out []tableData
// prepare query
qry := "SELECT msg, data, text_col, t_datetime, t_datetime_micros FROM " + tableName
qry := "SELECT msg, data, text_col, t_datetime, t_datetime_micros FROM vt_prepare_stmt_test"
if where != "" {
qry += " WHERE (" + where + ")"
}
Expand Down
10 changes: 10 additions & 0 deletions go/test/endtoend/preparestmt/sSchema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE t1
(
id BIGINT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT,
email VARCHAR(100),
created_at DATETIME,
is_active BOOLEAN,
INDEX age_idx (age)
);
22 changes: 22 additions & 0 deletions go/test/endtoend/preparestmt/sVschema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"sharded": true,
"vindexes": {
"xxhash": {
"type": "xxhash"
}
},
"tables": {
"t1": {
"column_vindexes": [
{
"column": "id",
"name": "xxhash"
}
],
"auto_increment":{
"column" : "id",
"sequence" : "uks.t1_seq"
}
}
}
}
Loading

0 comments on commit c0d29fe

Please sign in to comment.