-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_gorm.go
104 lines (96 loc) · 2.47 KB
/
lib_gorm.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
package sqlhbenchmarks
import (
"testing"
"github.com/nofeaturesonlybugs/sqlhbenchmarks/types"
"gorm.io/gorm"
)
// GORMSelect selects records using GORM.
func GORMSelect(limit int, db *gorm.DB) func(*testing.B) {
fn := func(b *testing.B) {
var dest []*types.Address
var result *gorm.DB
//
for k := 0; k < b.N; k++ {
result = db.Limit(limit).Find(&dest)
if result.Error != nil {
b.Fatalf("gorm failed with %v", result.Error.Error())
}
}
}
return fn
}
// GORMInsert performs INSERTs using GORM.
func GORMInsert(addresses []*types.Address, db *gorm.DB) func(b *testing.B) {
fn := func(b *testing.B) {
var result *gorm.DB
for k := 0; k < b.N; k++ {
for _, address := range addresses {
address.PreInsert(b)
//
result = db.Create(address)
if result.Error != nil {
b.Fatalf("gorm failed with %v", result.Error.Error())
}
// address.PostInsert(b) // TODO CreatedAt, ModifiedAt not working with our "stacked" model type.
}
}
}
return fn
}
// GORMPreparedInsert performs INSERTs using GORM.
func GORMPreparedInsert(addresses []*types.Address, db *gorm.DB) func(b *testing.B) {
fn := func(b *testing.B) {
var result *gorm.DB
b.StopTimer()
copies := make([]*types.Address, len(addresses))
b.StartTimer()
for k := 0; k < b.N; k++ {
b.StopTimer()
for k, v := range addresses {
copies[k] = &types.Address{
Street: v.Street,
City: v.City,
State: v.State,
Zip: v.Zip,
}
}
b.StartTimer()
result = db.Create(copies)
if result.Error != nil {
b.Fatalf("gorm failed with %v", result.Error.Error())
}
}
}
return fn
}
// GORMUpdate performs UPDATEs using GORM.
func GORMUpdate(addresses []*types.Address, db *gorm.DB) func(b *testing.B) {
fn := func(b *testing.B) {
var result *gorm.DB
for k := 0; k < b.N; k++ {
for _, address := range addresses {
address.PreUpdate(b)
//
result = db.Save(address)
if result.Error != nil {
b.Fatalf("gorm failed with %v", result.Error.Error())
}
// address.PostUpdate(b) // TODO CreatedAt, ModifiedAt not working with our "stacked" model type.
}
}
}
return fn
}
// GORMPreparedUpdate performs INSERTs using GORM.
func GORMPreparedUpdate(addresses []*types.Address, db *gorm.DB) func(b *testing.B) {
fn := func(b *testing.B) {
var result *gorm.DB
for k := 0; k < b.N; k++ {
result = db.Save(addresses)
if result.Error != nil {
b.Fatalf("gorm failed with %v", result.Error.Error())
}
}
}
return fn
}