forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
- Loading branch information
1 parent
9383943
commit c0d29fe
Showing
7 changed files
with
241 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.