Repository navigation
Expand file tree
/
Copy pathscan_test.go
More file actions
123 lines (117 loc) · 3.32 KB
/
Copy pathscan_test.go
File metadata and controls
123 lines (117 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2026 The liustack/sqlite Authors.
// SPDX-License-Identifier: MIT
// Use of this source code is governed by the MIT license in LICENSE at the
// root of this repository.
//go:build go1.27 && (((linux || darwin) && (amd64 || arm64)) || (windows && (amd64 || arm64)))
package sqlite_test
import (
"fmt"
"runtime"
"strings"
"testing"
)
// Numbers scanned into numbers are not put in an interface on the way: a
// row of them costs no allocation. It takes Go 1.27 (scan_go127.go).
func TestScanNumbersDoNotAllocate(t *testing.T) {
db := openDB(t, ":memory:")
db.SetMaxOpenConns(1)
if _, err := db.Exec(`CREATE TABLE t(a INTEGER, b REAL, c INTEGER);
WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM n WHERE i < 1000)
INSERT INTO t SELECT 100000 + i, i * 1.5, 1 << 40 FROM n`); err != nil {
t.Fatal(err)
}
rows, err := db.Query(`SELECT a, b, c FROM t`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
var (
a, c int64
b float64
sum int64
)
// Every allocation goes in the memory profile. Only those made on this
// goroutine count: the timers of the tests before can allocate on
// another at any time (trimIdle in internal/csqlite).
defer func(rate int) { runtime.MemProfileRate = rate }(runtime.MemProfileRate)
runtime.MemProfileRate = 1
sites := allocSites()
for rows.Next() {
if err := rows.Scan(&a, &b, &c); err != nil {
t.Fatal(err)
}
sum += a
}
allocs, where := allocSitesSince(sites, "github.com/liustack/sqlite_test.TestScanNumbersDoNotAllocate")
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if sum != 1000*100000+1000*1001/2 {
t.Errorf("sum %d", sum)
}
if allocs > 10 { // for 1000 rows
t.Errorf("%d allocations for 1000 rows, made at:\n%s", allocs, where)
}
}
// allocSites is the number of objects allocated so far at each stack, as
// the memory profile has them: it counts an allocation once two
// collections passed it.
func allocSites() map[[32]uintptr]int64 {
runtime.GC()
runtime.GC()
n, _ := runtime.MemProfile(nil, true)
var p []runtime.MemProfileRecord
for {
p = make([]runtime.MemProfileRecord, n+64)
var ok bool
if n, ok = runtime.MemProfile(p, true); ok {
p = p[:n]
break
}
}
sites := make(map[[32]uintptr]int64, len(p))
for _, r := range p {
sites[r.Stack0] += r.AllocObjects
}
return sites
}
// allocSitesSince is the stacks that allocated since allocSites gave
// before, with how many objects each. mine is the objects of the stacks that
// go through the function test, leaving out those of allocSites itself.
func allocSitesSince(before map[[32]uintptr]int64, test string) (mine int64, where string) {
var b strings.Builder
for stack, n := range allocSites() {
if n -= before[stack]; n <= 0 {
continue
}
fmt.Fprintf(&b, "%d objects:\n", n)
pcs := stack[:]
for i, pc := range pcs {
if pc == 0 {
pcs = pcs[:i]
break
}
}
frames := runtime.CallersFrames(pcs)
inTest, inHelper := false, false
for {
f, more := frames.Next()
if f.Function != "" {
fmt.Fprintf(&b, "\t%s %s:%d\n", f.Function, f.File, f.Line)
}
switch f.Function {
case test:
inTest = true
case "github.com/liustack/sqlite_test.allocSites", "github.com/liustack/sqlite_test.allocSitesSince":
inHelper = true
}
if !more {
break
}
}
if inTest && !inHelper {
mine += n
}
}
return mine, b.String()
}