-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
164 lines (134 loc) · 4.13 KB
/
Copy pathbenchmark_test.go
File metadata and controls
164 lines (134 loc) · 4.13 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package pagination
import (
"fmt"
"testing"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func setupBenchmarkDB(recordCount int) *gorm.DB {
db, _ := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
db.AutoMigrate(&TestUser{})
batchSize := 1000
for i := 0; i < recordCount; i += batchSize {
var users []TestUser
end := i + batchSize
if end > recordCount {
end = recordCount
}
for j := i; j < end; j++ {
users = append(users, TestUser{
Name: fmt.Sprintf("User %d", j),
Email: fmt.Sprintf("user%d@example.com", j),
Age: 20 + (j % 50),
})
}
db.CreateInBatches(users, batchSize)
}
return db
}
func BenchmarkPaginatedQuery_1000Records(b *testing.B) {
db := setupBenchmarkDB(1000)
builder := NewSimpleQueryBuilder("test_users").
WithSearchFields("name", "email").
WithDefaultSort("id asc")
pagination := PaginationRequest{Page: 1, PerPage: 20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}
func BenchmarkPaginatedQuery_10000Records(b *testing.B) {
db := setupBenchmarkDB(10000)
builder := NewSimpleQueryBuilder("test_users").
WithSearchFields("name", "email").
WithDefaultSort("id asc")
pagination := PaginationRequest{Page: 1, PerPage: 20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}
func BenchmarkPaginatedQuery_WithSearch(b *testing.B) {
db := setupBenchmarkDB(5000)
builder := NewSimpleQueryBuilder("test_users").
WithSearchFields("name", "email").
WithDefaultSort("id asc")
pagination := PaginationRequest{Page: 1, PerPage: 20, Search: "User 100"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}
func BenchmarkPaginatedQuery_WithFilters(b *testing.B) {
db := setupBenchmarkDB(5000)
builder := NewSimpleQueryBuilder("test_users").
WithSearchFields("name", "email").
WithDefaultSort("id asc").
WithFilters(func(query *gorm.DB) *gorm.DB {
return query.Where("age > ?", 30)
})
pagination := PaginationRequest{Page: 1, PerPage: 20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}
func BenchmarkDynamicFilter(b *testing.B) {
db := setupBenchmarkDB(5000)
filter := &DynamicFilter{
TableName: "test_users",
Model: TestUser{},
SearchFields: []string{"name", "email"},
DefaultSort: "id asc",
Filters: []FilterCondition{
{Field: "age", Operator: ">", Value: 30, Logic: "AND"},
},
}
pagination := PaginationRequest{Page: 1, PerPage: 20}
filter.Pagination = pagination
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, filter, pagination, []string{})
}
}
func BenchmarkChainableQueryBuilder(b *testing.B) {
db := setupBenchmarkDB(5000)
builder := NewChainableQueryBuilder("test_users").
WithSearchFields("name", "email").
WithDefaultSort("id asc").
WithFilters(func(query *gorm.DB) *gorm.DB {
return query.Where("age > ?", 25)
})
pagination := PaginationRequest{Page: 1, PerPage: 20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}
func BenchmarkPaginatedQuery_PageSize10(b *testing.B) {
db := setupBenchmarkDB(1000)
builder := NewSimpleQueryBuilder("test_users")
pagination := PaginationRequest{Page: 1, PerPage: 10}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}
func BenchmarkPaginatedQuery_PageSize50(b *testing.B) {
db := setupBenchmarkDB(1000)
builder := NewSimpleQueryBuilder("test_users")
pagination := PaginationRequest{Page: 1, PerPage: 50}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}
func BenchmarkPaginatedQuery_PageSize100(b *testing.B) {
db := setupBenchmarkDB(1000)
builder := NewSimpleQueryBuilder("test_users")
pagination := PaginationRequest{Page: 1, PerPage: 100}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = PaginatedQuery[TestUser](db, builder, pagination, []string{})
}
}