Created
April 27, 2023 17:10
-
-
Save lzap/6c5c8bd7e580f8d656d83fbf95651723 to your computer and use it in GitHub Desktop.
Updated benchmark results for sqlite3 for article https://blog.jetbrains.com/go/2023/04/27/comparing-db-packages/
This file contains hidden or 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
diff --git a/go-db-comparison/benchmarks/benchmark.go b/go-db-comparison/benchmarks/benchmark.go | |
index b6dc9f9..17e4955 100644 | |
--- a/go-db-comparison/benchmarks/benchmark.go | |
+++ b/go-db-comparison/benchmarks/benchmark.go | |
@@ -6,30 +6,34 @@ import ( | |
"log" | |
"time" | |
- "gorm.io/driver/mysql" | |
"gorm.io/gorm" | |
- _ "github.com/go-sql-driver/mysql" | |
+ "github.com/glebarez/sqlite" | |
"github.com/jmoiron/sqlx" | |
sqlc "github.com/rexfordnyrk/go-db-comparison/benchmarks/sqlc_generated" | |
+ //_ "modernc.org/sqlite" | |
) | |
func init() { | |
var err error | |
- // Opening a database connection. | |
- db, err = sql.Open("mysql", "theuser:thepass@tcp(localhost:3306)/thedb?parseTime=true") | |
+ | |
+ db, err = sql.Open("sqlite", "file:/tmp/sql.db") | |
if err != nil { | |
panic(err) | |
} | |
- //sqlx connection using existing db connection | |
- dbx = sqlx.NewDb(db, "mysql") | |
+ dbx, err = sqlx.Open("sqlite", "file:/tmp/sql.db") | |
+ if err != nil { | |
+ panic(err) | |
+ } | |
- //sqlc connection using existing db connection | |
- dbc = sqlc.New(db) | |
+ db2, err2 := sql.Open("sqlite", "file:/tmp/sql.db") | |
+ if err2 != nil { | |
+ panic(err2) | |
+ } | |
+ dbc = sqlc.New(db2) | |
- //gorm connection using existing db connection | |
- gdb, err = gorm.Open(mysql.New(mysql.Config{Conn: db})) | |
+ gdb, err = gorm.Open(sqlite.Open("/tmp/sql.db"), &gorm.Config{}) | |
if err != nil { | |
panic(err) | |
} | |
@@ -45,14 +49,13 @@ var ( | |
func setup() { | |
clear() | |
table := `CREATE TABLE students ( | |
- id bigint NOT NULL AUTO_INCREMENT, | |
+ id integer primary key AUTOINCREMENT, | |
fname varchar(50) not null, | |
lname varchar(50) not null, | |
date_of_birth datetime not null, | |
email varchar(50) not null, | |
address varchar(50) not null, | |
- gender varchar(50) not null, | |
- PRIMARY KEY (id) | |
+ gender varchar(50) not null | |
);` | |
_, err := db.Exec(table) | |
if err != nil { | |
@@ -67,7 +70,7 @@ func setup() { | |
} | |
func clear() { | |
- _, err := db.Exec(`DROP TABLE IF EXISTS students`) | |
+ _, err := db.Exec(`DROP TABLE students`) | |
if err != nil { | |
panic(err) | |
} |
This file contains hidden or 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
% go test -bench=. ./benchmarks | |
================================== BENCHMARKING 1 RECORDS ====================================== | |
goos: darwin | |
goarch: arm64 | |
pkg: github.com/rexfordnyrk/go-db-comparison/benchmarks | |
Benchmark/Database/sql_limit:1_-10 75234 14841 ns/op | |
Benchmark/Sqlx_limit:1_-10 76561 15492 ns/op | |
Benchmark/Sqlc_limit:1_-10 77172 15449 ns/op | |
Benchmark/GORM_limit:1_-10 64780 18420 ns/op | |
================================================================================================= | |
================================== BENCHMARKING 10 RECORDS ====================================== | |
Benchmark/Database/sql_limit:10_-10 79626 14764 ns/op | |
Benchmark/Sqlx_limit:10_-10 76642 15526 ns/op | |
Benchmark/Sqlc_limit:10_-10 76710 15460 ns/op | |
Benchmark/GORM_limit:10_-10 64636 18449 ns/op | |
================================================================================================= | |
================================== BENCHMARKING 100 RECORDS ====================================== | |
Benchmark/Database/sql_limit:100_-10 79857 14811 ns/op | |
Benchmark/Sqlx_limit:100_-10 76436 15494 ns/op | |
Benchmark/Sqlc_limit:100_-10 76090 15549 ns/op | |
Benchmark/GORM_limit:100_-10 64732 18481 ns/op | |
================================================================================================= | |
================================== BENCHMARKING 1000 RECORDS ====================================== | |
Benchmark/Database/sql_limit:1000_-10 79842 14788 ns/op | |
Benchmark/Sqlx_limit:1000_-10 76423 15514 ns/op | |
Benchmark/Sqlc_limit:1000_-10 76650 15470 ns/op | |
Benchmark/GORM_limit:1000_-10 65814 18377 ns/op | |
================================================================================================= | |
================================== BENCHMARKING 10000 RECORDS ====================================== | |
Benchmark/Database/sql_limit:10000_-10 79634 14807 ns/op | |
Benchmark/Sqlx_limit:10000_-10 76440 15476 ns/op | |
Benchmark/Sqlc_limit:10000_-10 75955 15511 ns/op | |
Benchmark/GORM_limit:10000_-10 64882 18510 ns/op | |
================================================================================================= | |
================================== BENCHMARKING 15000 RECORDS ====================================== | |
Benchmark/Database/sql_limit:15000_-10 79530 14767 ns/op | |
Benchmark/Sqlx_limit:15000_-10 76888 15550 ns/op | |
Benchmark/Sqlc_limit:15000_-10 76395 15538 ns/op | |
Benchmark/GORM_limit:15000_-10 65167 18472 ns/op | |
================================================================================================= | |
PASS | |
ok github.com/rexfordnyrk/go-db-comparison/benchmarks 32.918s |
Updated benchmark: https://gist.github.com/lzap/752b324a5689ef57dd351be00ce23cbd
Please do not make any conclusions from this "benchmark", I was just researching a bug in the benchmark code. This is NOT how you should benchmark SQL libraries.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Executed on Macbook 16 Pro (M1 Pro) on Go 1.19.2.
I think the SQL changes are not needed, I was not sure about the SQLite3 syntax.