Commit 220759b9 authored by gwenn's avatar gwenn

Benchmark IntArray module.

parent e802ee80
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite
import (
......@@ -287,13 +285,13 @@ func (s *Stmt) ExportToCSV(nullvalue string, headers bool, w *yacr.Writer) error
type ImportConfig struct {
Name string // the name of the input; used only for error reports
Separator byte // CSV separator
Quoted bool // CSV field are quoted or not
Quoted bool // CSV fields are quoted or not
Guess bool // guess separator
Trim bool // trim spaces
Comment byte // comment marker
Trim bool // optional, trim spaces
Comment byte // optinal, comment marker
Headers bool // skip headers (first line)
Types []Affinity // optional, when target table does not exist, specify columns type
Log io.Writer // optional, used to tace lines in error
Log io.Writer // optional, used to trace lines in error
}
func (ic ImportConfig) getType(i int) string {
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite_test
import (
......
......@@ -2,11 +2,12 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite
import "fmt"
import (
"errors"
"fmt"
)
// IntArray is the Go-language interface definition for the "intarray" or
// integer array virtual table for SQLite.
......@@ -69,9 +70,12 @@ import "fmt"
// action to free the intarray objects (except if connections are pooled...).
type IntArray interface {
Bind(elements []int64)
Drop() error
}
type intArray struct {
c *Conn
name string
content []int64
}
......@@ -141,12 +145,12 @@ func (vc *intArrayVTabCursor) Rowid() (int64, error) {
// explicitly by the application, the virtual table will be dropped implicitly
// by the system when the database connection is closed.
func (c *Conn) CreateIntArray(name string) (IntArray, error) {
module := new(intArray)
module := &intArray{c: c, name: name}
if err := c.CreateModule(name, module); err != nil {
return nil, err
}
name = escapeQuote(name)
if err := c.FastExec(fmt.Sprintf("CREATE VIRTUAL TABLE temp.%s USING %s", name, name)); err != nil {
if err := c.FastExec(fmt.Sprintf(`CREATE VIRTUAL TABLE temp."%s" USING "%s"`, name, name)); err != nil {
return nil, err
}
return module, nil
......@@ -160,3 +164,19 @@ func (c *Conn) CreateIntArray(name string) (IntArray, error) {
func (m *intArray) Bind(elements []int64) {
m.content = elements
}
// Drop underlying virtual table.
func (m *intArray) Drop() error {
if m == nil {
return errors.New("nil sqlite intarray")
}
if m.c == nil {
return nil
}
err := m.c.FastExec(fmt.Sprintf(`DROP TABLE temp."%s"`, escapeQuote(m.name)))
if err != nil {
return err
}
m.c = nil
return nil
}
......@@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite_test
import (
"fmt"
"math/rand"
"strings"
"github.com/bmizerany/assert"
. "github.com/gwenn/gosqlite"
......@@ -70,4 +72,82 @@ func TestIntArrayModule(t *testing.T) {
p2.Bind([]int64{3, 4, 5})
p3.Bind([]int64{0, -5})
assert.T(t, !checkStep(t, s))
checkNoError(t, p1.Drop(), "%s")
checkNoError(t, p2.Drop(), "%s")
checkNoError(t, p3.Drop(), "%s")
}
func BenchmarkNoIntArray(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
panicOnError(b, db.Exec("CREATE TABLE rand (r INT)"))
values := rand.Perm(1000)
for _, v := range values {
panicOnError(b, db.Exec("INSERT INTO rand (r) VALUES (?)", v))
}
b.StartTimer()
for i := 0; i < b.N; i++ {
l := rand.Intn(999) + 1 // at least one value
sql := fmt.Sprintf("SELECT * FROM rand WHERE r IN (?%s)", strings.Repeat(", ?", l-1))
s, err := db.Prepare(sql)
panicOnError(b, err)
for j := 0; j < l; j++ {
panicOnError(b, s.BindByIndex(j+1, values[j]))
}
nr := 0
err = s.Select(func(s *Stmt) error {
_, _, err = s.ScanInt32(0)
nr++
return err
})
panicOnError(b, err)
if nr != l {
b.Fatalf("Only %d values; %d expected", nr, l)
}
panicOnError(b, s.Finalize())
}
}
func BenchmarkIntArray(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
panicOnError(b, db.Exec("CREATE TABLE rand (r INT)"))
perms := rand.Perm(1000)
values := make([]int64, len(perms))
for i, v := range perms {
panicOnError(b, db.Exec("INSERT INTO rand (r) VALUES (?)", v))
values[i] = int64(v)
}
b.StartTimer()
p, err := db.CreateIntArray("myia")
panicOnError(b, err)
defer p.Drop()
s, err := db.Prepare("SELECT * FROM rand WHERE r IN myia")
panicOnError(b, err)
defer s.Finalize()
for i := 0; i < b.N; i++ {
l := rand.Intn(999) + 1 // at least one value
p.Bind(values[0:l])
nr := 0
err = s.Select(func(s *Stmt) error {
_, _, err = s.ScanInt32(0)
nr++
return err
})
panicOnError(b, err)
if nr != l {
b.Fatalf("Only %d values; %d expected", nr, l)
}
}
}
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite
/*
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build all
package sqlite_test
import (
......
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