Commit e2ff8991 authored by gwenn's avatar gwenn

Benchmark Stmt cache.

parent 9bc7c55b
package sqlite_test
import (
. "github.com/gwenn/gosqlite"
"testing"
)
func checkCacheSize(t *testing.T, db *Conn, expectedSize, expectedMaxSize int) {
if size, maxSize := db.CacheSize(); size != expectedSize || maxSize != expectedMaxSize {
t.Errorf("%d <> %d || %d <> %d", expectedSize, size, expectedMaxSize, maxSize)
}
}
func TestDisabledCache(t *testing.T) {
db := open(t)
defer db.Close()
db.SetCacheSize(0)
if size, maxSize := db.CacheSize(); size != 0 || maxSize != 0 {
t.Errorf("%d <> %d || %d <> %d", 0, size, 0, maxSize)
}
checkCacheSize(t, db, 0, 0)
s, err := db.CacheOrPrepare("SELECT 1")
checkNoError(t, err, "couldn't prepare stmt: %#v")
......@@ -21,10 +26,7 @@ func TestDisabledCache(t *testing.T) {
err = s.Finalize()
checkNoError(t, err, "couldn't finalize stmt: %#v")
if size, maxSize := db.CacheSize(); size != 0 || maxSize != 0 {
t.Errorf("%d <> %d || %d <> %d", 0, size, 0, maxSize)
}
checkCacheSize(t, db, 0, 0)
}
func TestEnabledCache(t *testing.T) {
......@@ -32,9 +34,7 @@ func TestEnabledCache(t *testing.T) {
defer db.Close()
db.SetCacheSize(10)
if size, maxSize := db.CacheSize(); size != 0 || maxSize != 10 {
t.Errorf("%d <> %d || %d <> %d", 0, size, 10, maxSize)
}
checkCacheSize(t, db, 0, 10)
s, err := db.CacheOrPrepare("SELECT 1")
checkNoError(t, err, "couldn't prepare stmt: %#v")
......@@ -44,24 +44,48 @@ func TestEnabledCache(t *testing.T) {
err = s.Finalize()
checkNoError(t, err, "couldn't finalize stmt: %#v")
if size, maxSize := db.CacheSize(); size != 1 || maxSize != 10 {
t.Errorf("%d <> %d || %d <> %d", 1, size, 10, maxSize)
}
checkCacheSize(t, db, 1, 10)
ns, err := db.CacheOrPrepare("SELECT 1")
checkNoError(t, err, "couldn't prepare stmt: %#v")
if size, maxSize := db.CacheSize(); size != 0 || maxSize != 10 {
t.Errorf("%d <> %d || %d <> %d", 0, size, 10, maxSize)
}
checkCacheSize(t, db, 0, 10)
err = ns.Finalize()
checkNoError(t, err, "couldn't finalize stmt: %#v")
if size, maxSize := db.CacheSize(); size != 1 || maxSize != 10 {
t.Errorf("%d <> %d || %d <> %d", 1, size, 10, maxSize)
}
checkCacheSize(t, db, 1, 10)
db.SetCacheSize(0)
checkCacheSize(t, db, 0, 0)
}
func BenchmarkDisabledCache(b *testing.B) {
db, _ := Open("")
defer db.Close()
db.SetCacheSize(0)
b.StartTimer()
for i := 0; i < b.N; i++ {
s, _ := db.CacheOrPrepare("SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71")
s.Finalize()
}
if size, maxSize := db.CacheSize(); size != 0 || maxSize != 0 {
t.Errorf("%d <> %d || %d <> %d", 0, size, 0, maxSize)
b.Errorf("%d <> %d || %d <> %d", 0, size, 0, maxSize)
}
}
func BenchmarkEnabledCache(b *testing.B) {
db, _ := Open("")
defer db.Close()
db.SetCacheSize(10)
b.StartTimer()
for i := 0; i < b.N; i++ {
s, _ := db.CacheOrPrepare("SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71")
s.Finalize()
}
if size, maxSize := db.CacheSize(); size != 1 || maxSize != 10 {
b.Errorf("%d <> %d || %d <> %d", 1, size, 10, maxSize)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment