Created
December 8, 2022 09:27
-
-
Save markuswustenberg/8be63c6e95dc5bb50fff6e14cc26f5eb to your computer and use it in GitHub Desktop.
SQLite benchmark with Go mutex
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
module sqlite | |
go 1.19 | |
require github.com/mattn/go-sqlite3 v1.14.16 |
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
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= | |
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= |
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
.PHONY: benchmark | |
benchmark: | |
go test -bench=. |
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
package sqlite | |
import ( | |
"database/sql" | |
"sync" | |
) | |
func Setup(db *sql.DB) error { | |
_, err := db.Exec(` | |
create table posts ( | |
id integer primary key, | |
title text not null, | |
content text not null, | |
created text not null default (strftime('%Y-%m-%dT%H:%M:%fZ')) | |
)`) | |
return err | |
} | |
func WriteBlogPost(db *sql.DB, title, content string) error { | |
_, err := db.Exec(`insert into posts (title, content) values (?, ?)`, title, content) | |
return err | |
} | |
var m sync.Mutex | |
func WriteBlogPostMutexed(db *sql.DB, title, content string) error { | |
m.Lock() | |
defer m.Unlock() | |
_, err := db.Exec(`insert into posts (title, content) values (?, ?)`, title, content) | |
return 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
package sqlite_test | |
import ( | |
"database/sql" | |
"path" | |
"testing" | |
_ "github.com/mattn/go-sqlite3" | |
"sqlite" | |
) | |
func BenchmarkWriteBlogPost(b *testing.B) { | |
b.Run("write blog post without WAL", func(b *testing.B) { | |
db := setupDB(b, "?_timeout=5000&_fk=true") | |
for i := 0; i < b.N; i++ { | |
if err := sqlite.WriteBlogPost(db, "Some title", "Some content"); err != nil { | |
b.Fatal(err) | |
} | |
} | |
}) | |
b.Run("write blog post with WAL", func(b *testing.B) { | |
db := setupDB(b, "?_journal=WAL&_timeout=5000&_fk=true") | |
for i := 0; i < b.N; i++ { | |
if err := sqlite.WriteBlogPost(db, "Some title", "Some content"); err != nil { | |
b.Fatal(err) | |
} | |
} | |
}) | |
b.Run("write blog post with WAL and Go mutex", func(b *testing.B) { | |
db := setupDB(b, "?_journal=WAL&_timeout=5000&_fk=true") | |
for i := 0; i < b.N; i++ { | |
if err := sqlite.WriteBlogPostMutexed(db, "Some title", "Some content"); err != nil { | |
b.Fatal(err) | |
} | |
} | |
}) | |
} | |
func setupDB(b *testing.B, options string) *sql.DB { | |
dbPath := path.Join(b.TempDir(), "benchmark.db") | |
db, err := sql.Open("sqlite3", dbPath+options) | |
if err != nil { | |
b.Fatal(err) | |
} | |
if err := sqlite.Setup(db); err != nil { | |
b.Fatal(err) | |
} | |
return db | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment