jlelse
/
GoSqliteBench
Archived
1
Fork 0
This repository has been archived on 2024-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
GoSqliteBench/SqliteBench_test.go

94 lines
2.2 KiB
Go

package main
import (
"context"
"database/sql"
"math/rand"
"path/filepath"
"strconv"
"testing"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)
func Benchmark_Zombiezen(b *testing.B) {
// Create and open database
dbpool, err := sqlitex.Open(filepath.Join(b.TempDir(), "db.db"), sqlite.OpenCreate|sqlite.OpenReadWrite|sqlite.OpenWAL|sqlite.OpenNoMutex, 10)
require.NoError(b, err)
// Create table
conn := dbpool.Get(context.Background())
err = sqlitex.Exec(conn, "create table test(a text not null primary key, b text not null, c text not null default 'd');", nil)
dbpool.Put(conn)
require.NoError(b, err)
// Insert 1000 entries
for i := 0; i < 1000; i++ {
str := strconv.Itoa(i)
conn := dbpool.Get(context.Background())
_ = sqlitex.Exec(conn, "insert into test(a, b) values (?, ?);", nil, str, str)
dbpool.Put(conn)
}
// Queries
b.Run("Queries", func(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
conn := dbpool.Get(context.Background())
_ = sqlitex.Exec(conn, "select a, b, c from test where a = ?;", func(stmt *sqlite.Stmt) error {
_ = stmt.ColumnText(0)
_ = stmt.ColumnText(1)
_ = stmt.ColumnText(2)
return nil
}, strconv.Itoa(rand.Intn(1000)))
dbpool.Put(conn)
}
})
})
}
func Benchmark_Mattn(b *testing.B) {
// Create and open database
db, err := sql.Open("sqlite3", filepath.Join(b.TempDir(), "db.db")+"?mode=rwc&_journal_mode=WAL&cache=shared")
require.NoError(b, err)
db.SetMaxOpenConns(10)
// Create table
_, err = db.Exec("create table test(a text not null primary key, b text not null, c text not null default 'd');")
require.NoError(b, err)
// Insert 1000 entries
for i := 0; i < 1000; i++ {
str := strconv.Itoa(i)
_, _ = db.Exec("insert into test(a, b) values (?, ?);", str, str)
}
// Queries
b.Run("Queries", func(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
var a, b, c string
rows, _ := db.Query("select a, b, c from test where a = ?;", strconv.Itoa(rand.Intn(1000)))
for rows.Next() {
rows.Scan(&a, &b, &c)
}
}
})
})
}