-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench_test.go
138 lines (118 loc) · 3.06 KB
/
bench_test.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package scan
import (
"context"
"database/sql"
"fmt"
"os"
"os/signal"
"syscall"
"testing"
"time"
_ "github.com/stephenafamo/fakedb"
)
// Benchmark Command: go test -v -run=XXX -cpu 1,2,4 -benchmem -bench=. -memprofile mem.prof
var (
db *sql.DB
dataSize = 100
)
func TestMain(m *testing.M) {
var err error
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
db, err = sql.Open("test", "bench")
if err != nil {
panic(fmt.Errorf("Error opening testdb %w", err))
}
defer db.Close()
err = prepareData(ctx)
if err != nil {
panic(err)
}
exitVal := m.Run()
os.Exit(exitVal)
}
func BenchmarkScanAll(b *testing.B) {
b.StopTimer()
ctx := context.Background()
for i := 0; i < b.N; i++ {
b.StopTimer()
rows, err := db.Query("SELECT|user||")
if err != nil {
panic(err)
}
b.StartTimer()
if _, err := AllFromRows(ctx, StructMapper[Userss](), rows); err != nil {
panic(err)
}
rows.Close()
}
}
func BenchmarkScanOne(b *testing.B) {
b.StopTimer()
ctx := context.Background()
for i := 0; i < b.N; i++ {
b.StopTimer()
rows, err := db.Query("SELECT|user||")
if err != nil {
panic(err)
}
b.StartTimer()
if _, err := OneFromRows(ctx, StructMapper[Userss](), rows); err != nil {
panic(err)
}
rows.Close()
}
}
func prepareData(ctx context.Context) error {
create := "CREATE|user|id=int64,username=string,password=string"
create += ",email=string,mobile_phone=string,company=string,avatar_url=string"
create += ",role=int16,last_online_at=int64,create_at=datetime,update_at=datetime"
if _, err := db.ExecContext(ctx, create); err != nil {
return err
}
for i := 0; i < dataSize; i++ {
userName := fmt.Sprintf("user%d", i+1)
password := fmt.Sprintf("password%d", i+1)
email := fmt.Sprintf("user%[email protected]", i+1)
mobilePhone := fmt.Sprintf("%d", 10000*(i+1))
company := fmt.Sprintf("company%d", i+1)
avatarURL := fmt.Sprintf("http://sqlscan.com/avatar/%d", i+1)
role := i % 3
lastOnlineAt := time.Now().Unix() + int64(i)
createAt := time.Now().UTC()
updateAt := time.Now().UTC()
_, err := db.Exec(`INSERT|user|id=?,username=?,password=?,email=?,mobile_phone=?,company=?,avatar_url=?,role=?,last_online_at=?,create_at=?,update_at=?`,
i, userName, password, email, mobilePhone, company, avatarURL, role, lastOnlineAt, createAt, updateAt)
if err != nil {
return err
}
}
return nil
}
type Userss struct {
ID int `db:"id"`
UserName string `db:"username"`
Password string `db:"password"`
Email string `db:"email"`
MobilePhone string `db:"mobile_phone"`
Company string `db:"company"`
AvatarURL string `db:"avatar_url"`
Role int `db:"role"`
LastOnlineAt int64 `db:"last_online_at"`
CreateAt time.Time `db:"create_at"`
UpdateAt time.Time `db:"update_at"`
}
func TestPrepare(t *testing.T) {
cnt := 0
rows, err := db.Query("SELECT|user||")
if err != nil {
t.Fatal(err)
}
defer rows.Close()
for rows.Next() {
cnt++
}
if cnt != dataSize {
t.Error("wrong cnt")
}
}