enable memory auditing in most tests/benchmarks using -tags=libc.memgrind

This commit is contained in:
Jan Mercl
2021-01-16 16:46:44 +01:00
parent 3c4fb59e0f
commit bfeff02653
4 changed files with 93 additions and 13 deletions

View File

@@ -24,6 +24,7 @@ import (
"testing" "testing"
"time" "time"
"modernc.org/libc"
"modernc.org/mathutil" "modernc.org/mathutil"
) )
@@ -129,11 +130,16 @@ func tempDB(t testing.TB) (string, *sql.DB) {
} }
func TestScalar(t *testing.T) { func TestScalar(t *testing.T) {
libc.MemAuditStart()
dir, db := tempDB(t) dir, db := tempDB(t)
defer func() { defer func() {
db.Close() db.Close()
os.RemoveAll(dir) os.RemoveAll(dir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}() }()
t1 := time.Date(2017, 4, 20, 1, 2, 3, 56789, time.UTC) t1 := time.Date(2017, 4, 20, 1, 2, 3, 56789, time.UTC)
@@ -197,11 +203,15 @@ func TestScalar(t *testing.T) {
} }
func TestBlob(t *testing.T) { func TestBlob(t *testing.T) {
libc.MemAuditStart()
dir, db := tempDB(t) dir, db := tempDB(t)
defer func() { defer func() {
db.Close() db.Close()
os.RemoveAll(dir) os.RemoveAll(dir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}() }()
b1 := []byte(time.Now().String()) b1 := []byte(time.Now().String())
@@ -249,6 +259,7 @@ func TestBlob(t *testing.T) {
} }
func benchmarkInsertMemory(b *testing.B, n int) { func benchmarkInsertMemory(b *testing.B, n int) {
libc.MemAuditStart()
db, err := sql.Open(driverName, "file::memory:") db, err := sql.Open(driverName, "file::memory:")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
@@ -256,6 +267,9 @@ func benchmarkInsertMemory(b *testing.B, n int) {
defer func() { defer func() {
db.Close() db.Close()
if err := libc.MemAuditReport(); err != nil {
b.Error(err)
}
}() }()
b.ReportAllocs() b.ReportAllocs()
@@ -300,6 +314,7 @@ func BenchmarkInsertMemory(b *testing.B) {
var staticInt int var staticInt int
func benchmarkNextMemory(b *testing.B, n int) { func benchmarkNextMemory(b *testing.B, n int) {
libc.MemAuditStart()
db, err := sql.Open(driverName, "file::memory:") db, err := sql.Open(driverName, "file::memory:")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
@@ -307,6 +322,9 @@ func benchmarkNextMemory(b *testing.B, n int) {
defer func() { defer func() {
db.Close() db.Close()
if err := libc.MemAuditReport(); err != nil {
b.Error(err)
}
}() }()
if _, err := db.Exec(` if _, err := db.Exec(`
@@ -368,12 +386,16 @@ func BenchmarkNextMemory(b *testing.B) {
// https://gitlab.com/cznic/sqlite/issues/11 // https://gitlab.com/cznic/sqlite/issues/11
func TestIssue11(t *testing.T) { func TestIssue11(t *testing.T) {
libc.MemAuditStart()
const N = 6570 const N = 6570
dir, db := tempDB(t) dir, db := tempDB(t)
defer func() { defer func() {
db.Close() db.Close()
os.RemoveAll(dir) os.RemoveAll(dir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}() }()
if _, err := db.Exec(` if _, err := db.Exec(`
@@ -396,6 +418,7 @@ func TestIssue11(t *testing.T) {
// https://gitlab.com/cznic/sqlite/issues/12 // https://gitlab.com/cznic/sqlite/issues/12
func TestMemDB(t *testing.T) { func TestMemDB(t *testing.T) {
libc.MemAuditStart()
// Verify we can create out-of-the heap memory DB instance. // Verify we can create out-of-the heap memory DB instance.
db, err := sql.Open(driverName, "file::memory:") db, err := sql.Open(driverName, "file::memory:")
if err != nil { if err != nil {
@@ -404,6 +427,9 @@ func TestMemDB(t *testing.T) {
defer func() { defer func() {
db.Close() db.Close()
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}() }()
v := strings.Repeat("a", 1024) v := strings.Repeat("a", 1024)
@@ -431,6 +457,7 @@ func TestMemDB(t *testing.T) {
} }
func TestConcurrentGoroutines(t *testing.T) { func TestConcurrentGoroutines(t *testing.T) {
libc.MemAuditStart()
const ( const (
ngoroutines = 8 ngoroutines = 8
nrows = 5000 nrows = 5000
@@ -441,7 +468,12 @@ func TestConcurrentGoroutines(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(dir) defer func() {
os.RemoveAll(dir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}()
db, err := sql.Open(driverName, filepath.Join(dir, "test.db")) db, err := sql.Open(driverName, filepath.Join(dir, "test.db"))
if err != nil { if err != nil {
@@ -537,12 +569,18 @@ func TestConcurrentGoroutines(t *testing.T) {
} }
func TestConcurrentProcesses(t *testing.T) { func TestConcurrentProcesses(t *testing.T) {
libc.MemAuditStart()
dir, err := ioutil.TempDir("", "sqlite-test-") dir, err := ioutil.TempDir("", "sqlite-test-")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(dir) defer func() {
os.RemoveAll(dir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}()
m, err := filepath.Glob(filepath.FromSlash("internal/mptest/*")) m, err := filepath.Glob(filepath.FromSlash("internal/mptest/*"))
if err != nil { if err != nil {
@@ -629,6 +667,7 @@ outer:
// https://gitlab.com/cznic/sqlite/issues/19 // https://gitlab.com/cznic/sqlite/issues/19
func TestIssue19(t *testing.T) { func TestIssue19(t *testing.T) {
libc.MemAuditStart()
const ( const (
drop = ` drop = `
drop table if exists products; drop table if exists products;
@@ -660,7 +699,12 @@ INSERT INTO "products" ("id", "user_id", "name", "description", "created_at", "c
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(dir) defer func() {
os.RemoveAll(dir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}()
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
@@ -749,6 +793,7 @@ func mustExec(t *testing.T, db *sql.DB, sql string, args ...interface{}) sql.Res
// https://gitlab.com/cznic/sqlite/issues/20 // https://gitlab.com/cznic/sqlite/issues/20
func TestIssue20(t *testing.T) { func TestIssue20(t *testing.T) {
libc.MemAuditStart()
const TablePrefix = "gosqltest_" const TablePrefix = "gosqltest_"
tempDir, err := ioutil.TempDir("", "") tempDir, err := ioutil.TempDir("", "")
@@ -756,7 +801,12 @@ func TestIssue20(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(tempDir) defer func() {
os.RemoveAll(tempDir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}()
db, err := sql.Open("sqlite", filepath.Join(tempDir, "foo.db")) db, err := sql.Open("sqlite", filepath.Join(tempDir, "foo.db"))
if err != nil { if err != nil {
@@ -809,12 +859,18 @@ func TestIssue20(t *testing.T) {
} }
func TestNoRows(t *testing.T) { func TestNoRows(t *testing.T) {
libc.MemAuditStart()
tempDir, err := ioutil.TempDir("", "") tempDir, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(tempDir) defer func() {
os.RemoveAll(tempDir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}()
db, err := sql.Open("sqlite", filepath.Join(tempDir, "foo.db")) db, err := sql.Open("sqlite", filepath.Join(tempDir, "foo.db"))
if err != nil { if err != nil {
@@ -835,12 +891,18 @@ func TestNoRows(t *testing.T) {
// https://gitlab.com/cznic/sqlite/-/issues/28 // https://gitlab.com/cznic/sqlite/-/issues/28
func TestIssue28(t *testing.T) { func TestIssue28(t *testing.T) {
libc.MemAuditStart()
tempDir, err := ioutil.TempDir("", "") tempDir, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(tempDir) defer func() {
os.RemoveAll(tempDir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}()
db, err := sql.Open("sqlite", filepath.Join(tempDir, "test.db")) db, err := sql.Open("sqlite", filepath.Join(tempDir, "test.db"))
if err != nil { if err != nil {
@@ -862,12 +924,18 @@ func TestIssue28(t *testing.T) {
// https://gitlab.com/cznic/sqlite/-/issues/30 // https://gitlab.com/cznic/sqlite/-/issues/30
func TestIssue30(t *testing.T) { func TestIssue30(t *testing.T) {
libc.MemAuditStart()
tempDir, err := ioutil.TempDir("", "") tempDir, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.RemoveAll(tempDir) defer func() {
os.RemoveAll(tempDir)
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}()
db, err := sql.Open("sqlite", filepath.Join(tempDir, "test.db")) db, err := sql.Open("sqlite", filepath.Join(tempDir, "test.db"))
if err != nil { if err != nil {
@@ -926,6 +994,7 @@ Col 3: DatabaseTypeName "DATE", DecimalSize 0 0 false, Length 922337203685477580
// https://gitlab.com/cznic/sqlite/-/issues/35 // https://gitlab.com/cznic/sqlite/-/issues/35
func TestTime(t *testing.T) { func TestTime(t *testing.T) {
libc.MemAuditStart()
types := []string{ types := []string{
"DATE", "DATE",
"DATETIME", "DATETIME",
@@ -944,6 +1013,9 @@ func TestTime(t *testing.T) {
defer func() { defer func() {
db.Close() db.Close()
if err := libc.MemAuditReport(); err != nil {
t.Error(err)
}
}() }()
for _, typ := range types { for _, typ := range types {

View File

@@ -39,6 +39,7 @@ func makename(inMemory bool, driver string, e int) string {
} }
func benchmarkRead(b *testing.B, drivername, file string, n int) { func benchmarkRead(b *testing.B, drivername, file string, n int) {
libc.MemAuditStart()
os.Remove(file) os.Remove(file)
db, err := sql.Open(drivername, file) db, err := sql.Open(drivername, file)
if err != nil { if err != nil {
@@ -47,6 +48,9 @@ func benchmarkRead(b *testing.B, drivername, file string, n int) {
defer func() { defer func() {
db.Close() db.Close()
if err := libc.MemAuditReport(); err != nil {
b.Error(err)
}
}() }()
if _, err := db.Exec(` if _, err := db.Exec(`
@@ -126,6 +130,7 @@ func BenchmarkReading1(b *testing.B) {
} }
func benchmarkInsertComparative(b *testing.B, drivername, file string, n int) { func benchmarkInsertComparative(b *testing.B, drivername, file string, n int) {
libc.MemAuditStart()
os.Remove(file) os.Remove(file)
db, err := sql.Open(drivername, file) db, err := sql.Open(drivername, file)
if err != nil { if err != nil {
@@ -134,6 +139,9 @@ func benchmarkInsertComparative(b *testing.B, drivername, file string, n int) {
defer func() { defer func() {
db.Close() db.Close()
if err := libc.MemAuditReport(); err != nil {
b.Error(err)
}
}() }()
if _, err := db.Exec(` if _, err := db.Exec(`

4
go.mod
View File

@@ -4,7 +4,7 @@ go 1.15
require ( require (
github.com/mattn/go-sqlite3 v1.14.6 github.com/mattn/go-sqlite3 v1.14.6
modernc.org/libc v0.0.0-20210112160811-243e027176f2 modernc.org/libc v0.0.0-20210116154030-fac1e8a80fa8
modernc.org/mathutil v1.2.1 modernc.org/mathutil v1.2.1
modernc.org/tcl v0.0.0-20210112162618-d02ee57da865 modernc.org/tcl v0.0.0-20210116154350-d39acab84afa
) )

8
go.sum
View File

@@ -9,13 +9,13 @@ golang.org/x/sys v0.0.0-20201126233918-771906719818 h1:f1CIuDlJhwANEC2MM87MBEVMr
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
modernc.org/httpfs v1.0.2 h1:4aw8F68gTwx7FWL/vEMjm/XaPwPL16MItkF/P9ziEPY= modernc.org/httpfs v1.0.2 h1:4aw8F68gTwx7FWL/vEMjm/XaPwPL16MItkF/P9ziEPY=
modernc.org/httpfs v1.0.2/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM= modernc.org/httpfs v1.0.2/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM=
modernc.org/libc v0.0.0-20210112160811-243e027176f2 h1:p4QkGWPO/XZek2VrYiAA5k6UewZNr31xyROkyesZvy0= modernc.org/libc v0.0.0-20210116154030-fac1e8a80fa8 h1:VgMrmNSL/v8CFhC5H3IYpXqh/rG/vE9XJP6oFcMH2Y8=
modernc.org/libc v0.0.0-20210112160811-243e027176f2/go.mod h1:IR66laG5b3bONN1tfix3Gpy8xk/6WDf+Rtc4NqNczls= modernc.org/libc v0.0.0-20210116154030-fac1e8a80fa8/go.mod h1:IR66laG5b3bONN1tfix3Gpy8xk/6WDf+Rtc4NqNczls=
modernc.org/mathutil v1.1.1 h1:FeylZSVX8S+58VsyJlkEj2bcpdytmp9MmDKZkKx8OIE= modernc.org/mathutil v1.1.1 h1:FeylZSVX8S+58VsyJlkEj2bcpdytmp9MmDKZkKx8OIE=
modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/mathutil v1.2.1 h1:PSIN4RdyeB6MbFsNLSkFCzDjnEVEMS3H/hFHcJtAJ9g= modernc.org/mathutil v1.2.1 h1:PSIN4RdyeB6MbFsNLSkFCzDjnEVEMS3H/hFHcJtAJ9g=
modernc.org/mathutil v1.2.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= modernc.org/mathutil v1.2.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.0.1 h1:bhVo78NAdgvRD4N+b2hGnAwL5RP2+QyiEJDsX3jpeDA= modernc.org/memory v1.0.1 h1:bhVo78NAdgvRD4N+b2hGnAwL5RP2+QyiEJDsX3jpeDA=
modernc.org/memory v1.0.1/go.mod h1:NSjvC08+g3MLOpcAxQbdctcThAEX4YlJ20WWHYEhvRg= modernc.org/memory v1.0.1/go.mod h1:NSjvC08+g3MLOpcAxQbdctcThAEX4YlJ20WWHYEhvRg=
modernc.org/tcl v0.0.0-20210112162618-d02ee57da865 h1:lr5cbOiQUQ3aKfyl9M8YdqIeINa23tiM85mkFatGD9Q= modernc.org/tcl v0.0.0-20210116154350-d39acab84afa h1:ilAz/AYRgGYZegGa9Ejqs+zjGwr1t7QxI06IReiw1OM=
modernc.org/tcl v0.0.0-20210112162618-d02ee57da865/go.mod h1:XuYnprNRD5ClgYC5JEGk+MSXwWqHdj5zFqvO88ooMoE= modernc.org/tcl v0.0.0-20210116154350-d39acab84afa/go.mod h1:xEeGkvTg7q54vNvl7HiEDNtl3jeV1x7A5pGttMkAKkc=