mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-11 21:49:13 +00:00
Benchmarks.
This commit is contained in:
86
bench/crawshaw_test.go
Normal file
86
bench/crawshaw_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package bench_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"crawshaw.io/sqlite"
|
||||
)
|
||||
|
||||
func BenchmarkCrawshaw(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
crawshawTest()
|
||||
}
|
||||
}
|
||||
|
||||
func crawshawTest() {
|
||||
db, err := sqlite.OpenConn(":memory:", 0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
exec := func(sql string) error {
|
||||
stmt, _, err := db.PrepareTransient(sql)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = stmt.Step()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return stmt.Finalize()
|
||||
}
|
||||
|
||||
err = exec(`CREATE TABLE users (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR, age INTEGER, rating REAL)`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
func() {
|
||||
const N = 1_000_000
|
||||
|
||||
exec(`BEGIN`)
|
||||
defer exec(`END`)
|
||||
|
||||
stmt, _, err := db.PrepareTransient(`INSERT INTO users (id, name, age, rating) VALUES (?, ?, ?, ?)`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer stmt.Finalize()
|
||||
|
||||
for i := 0; i < N; i++ {
|
||||
id := i + 1
|
||||
name := "user " + strconv.Itoa(id)
|
||||
age := 33 + id
|
||||
rating := 0.13 * float64(id)
|
||||
stmt.BindInt64(1, int64(id))
|
||||
stmt.BindText(2, name)
|
||||
stmt.BindInt64(3, int64(age))
|
||||
stmt.BindFloat(4, rating)
|
||||
}
|
||||
}()
|
||||
|
||||
func() {
|
||||
stmt, _, err := db.PrepareTransient(`SELECT id, name, age, rating FROM users ORDER BY id`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer stmt.Finalize()
|
||||
|
||||
for {
|
||||
if row, err := stmt.Step(); err != nil {
|
||||
panic(err)
|
||||
} else if !row {
|
||||
break
|
||||
}
|
||||
id := stmt.ColumnInt(0)
|
||||
name := stmt.ColumnText(1)
|
||||
age := stmt.ColumnInt64(2)
|
||||
rating := stmt.ColumnFloat(3)
|
||||
if id < 1 || len(name) < 5 || age < 33 || rating < 0.13 {
|
||||
panic("wrong row values")
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
86
bench/wasm_test.go
Normal file
86
bench/wasm_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package bench_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/ncruces/go-sqlite3"
|
||||
_ "github.com/ncruces/go-sqlite3/embed"
|
||||
)
|
||||
|
||||
func init() {
|
||||
db, err := sqlite3.Open(":memory:")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = db.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkWasm(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
wasmTest()
|
||||
}
|
||||
}
|
||||
|
||||
func wasmTest() {
|
||||
db, err := sqlite3.Open(":memory:")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
err = db.Exec(`CREATE TABLE users (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR, age INTEGER, rating REAL)`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
func() {
|
||||
const N = 1_000_000
|
||||
|
||||
db.Exec(`BEGIN`)
|
||||
defer db.Exec(`END`)
|
||||
|
||||
stmt, _, err := db.Prepare(`INSERT INTO users (id, name, age, rating) VALUES (?, ?, ?, ?)`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for i := 0; i < N; i++ {
|
||||
id := i + 1
|
||||
name := "user " + strconv.Itoa(id)
|
||||
age := 33 + id
|
||||
rating := 0.13 * float64(id)
|
||||
stmt.BindInt(1, id)
|
||||
stmt.BindText(2, name)
|
||||
stmt.BindInt64(3, int64(age))
|
||||
stmt.BindFloat(4, rating)
|
||||
}
|
||||
}()
|
||||
|
||||
func() {
|
||||
stmt, _, err := db.Prepare(`SELECT id, name, age, rating FROM users ORDER BY id`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
for {
|
||||
if row, err := stmt.Step(); err != nil {
|
||||
panic(err)
|
||||
} else if !row {
|
||||
break
|
||||
}
|
||||
id := stmt.ColumnInt(0)
|
||||
name := stmt.ColumnText(1)
|
||||
age := stmt.ColumnInt64(2)
|
||||
rating := stmt.ColumnFloat(3)
|
||||
if id < 1 || len(name) < 5 || age < 33 || rating < 0.13 {
|
||||
panic("wrong row values")
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module github.com/ncruces/go-sqlite3
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/tetratelabs/wazero v1.0.0-pre.7.0.20230114234926-2ce8988ab763
|
||||
require (
|
||||
crawshaw.io/sqlite v0.3.2
|
||||
github.com/tetratelabs/wazero v1.0.0-pre.7.0.20230117161130-15889085a5ca
|
||||
)
|
||||
|
||||
5
go.sum
5
go.sum
@@ -1,2 +1,7 @@
|
||||
crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797/go.mod h1:sXBiorCo8c46JlQV3oXPKINnZ8mcqnye1EkVkqsectk=
|
||||
crawshaw.io/sqlite v0.3.2 h1:N6IzTjkiw9FItHAa0jp+ZKC6tuLzXqAYIv+ccIWos1I=
|
||||
crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
|
||||
github.com/tetratelabs/wazero v1.0.0-pre.7.0.20230114234926-2ce8988ab763 h1:cuXa6OWMd3L3+nNrX9SfT6GxS6ykUAvx8/eegN6KeNo=
|
||||
github.com/tetratelabs/wazero v1.0.0-pre.7.0.20230114234926-2ce8988ab763/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo=
|
||||
github.com/tetratelabs/wazero v1.0.0-pre.7.0.20230117161130-15889085a5ca h1:cvbRnzLE99QnA6HT3yoqXjWEB2JMiyTs3GSLpQZg45A=
|
||||
github.com/tetratelabs/wazero v1.0.0-pre.7.0.20230117161130-15889085a5ca/go.mod h1:u8wrFmpdrykiFK0DFPiFm5a4+0RzsdmXYVtijBKqUVo=
|
||||
|
||||
Reference in New Issue
Block a user