-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
162 lines (140 loc) · 4.76 KB
/
benchmark_test.go
File metadata and controls
162 lines (140 loc) · 4.76 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
package batchflow_test
import (
"context"
"testing"
"time"
"github.com/rushairer/batchflow"
)
func BenchmarkBatchFlow_Submit(b *testing.B) {
ctx := context.Background()
config := batchflow.PipelineConfig{
BufferSize: 10000,
FlushSize: 1000,
FlushInterval: time.Second,
}
batch, mock := batchflow.NewBatchFlowWithMock(ctx, config)
b.Cleanup(func() {
if testing.Verbose() {
agg := mock.SnapshotResults()
for table, m := range agg {
b.Logf("[MockExec Summary] table=%s batches=%d rows=%d args=%d", table, m["batches"], m["rows"], m["args"])
}
}
})
schema := batchflow.NewSQLSchema("users", batchflow.ConflictIgnoreOperationConfig, "id", "name", "email")
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
request := batchflow.NewRequest(schema).
SetInt64("id", int64(i)).
SetString("name", "User"+string(rune(i))).
SetString("email", "user"+string(rune(i))+"@example.com")
if err := batch.Submit(ctx, request); err != nil {
b.Errorf("Submit failed: %v", err)
}
i++
}
})
}
func BenchmarkBatchFlow_MultiSchema(b *testing.B) {
ctx := context.Background()
config := batchflow.PipelineConfig{
BufferSize: 10000,
FlushSize: 1000,
FlushInterval: time.Second,
}
batch, mock := batchflow.NewBatchFlowWithMock(ctx, config)
b.Cleanup(func() {
if testing.Verbose() {
agg := mock.SnapshotResults()
for table, m := range agg {
b.Logf("[MockExec Summary] table=%s batches=%d rows=%d args=%d", table, m["batches"], m["rows"], m["args"])
}
}
})
userSchema := batchflow.NewSQLSchema("users", batchflow.ConflictIgnoreOperationConfig, "id", "name", "email")
productSchema := batchflow.NewSQLSchema("products", batchflow.ConflictUpdateOperationConfig, "id", "name", "price")
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
if i%2 == 0 {
request := batchflow.NewRequest(userSchema).
SetInt64("id", int64(i)).
SetString("name", "User"+string(rune(i))).
SetString("email", "user"+string(rune(i))+"@example.com")
if err := batch.Submit(ctx, request); err != nil {
b.Errorf("Submit user failed: %v", err)
}
} else {
request := batchflow.NewRequest(productSchema).
SetInt64("id", int64(i)).
SetString("name", "Product"+string(rune(i))).
SetFloat64("price", float64(i)*10.5)
if err := batch.Submit(ctx, request); err != nil {
b.Errorf("Submit product failed: %v", err)
}
}
i++
}
})
}
func BenchmarkRequest_Creation(b *testing.B) {
schema := batchflow.NewSQLSchema("users", batchflow.ConflictIgnoreOperationConfig, "id", "name", "email", "age", "created_at")
b.ResetTimer()
for i := 0; i < b.N; i++ {
request := batchflow.NewRequest(schema).
SetInt64("id", int64(i)).
SetString("name", "User"+string(rune(i))).
SetString("email", "user"+string(rune(i))+"@example.com").
SetInt32("age", int32(20+i%50)).
SetTime("created_at", time.Now())
// 验证请求创建成功
if request.Schema() != schema {
b.Errorf("Schema mismatch")
}
}
}
func BenchmarkSQLGeneration(b *testing.B) {
schema := batchflow.NewSQLSchema("users", batchflow.ConflictIgnoreOperationConfig, "id", "name", "email")
data := make([]map[string]any, 100)
for i := 0; i < 100; i++ {
data[i] = map[string]any{
"id": int64(i),
"name": "User" + string(rune(i)),
"email": "user" + string(rune(i)) + "@example.com",
}
}
// 测试不同数据库驱动的SQL生成性能
drivers := map[string]batchflow.SQLDriver{
"MySQL": &mysqlDriver{},
"PostgreSQL": &postgresDriver{},
"SQLite": &sqliteDriver{},
}
ctx := context.Background()
for name, driver := range drivers {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _, err := driver.GenerateInsertSQL(ctx, schema, data)
if err != nil {
b.Errorf("GenerateInsertSQL failed: %v", err)
}
}
})
}
}
// 简化的驱动实现用于基准测试
type mysqlDriver struct{}
func (d *mysqlDriver) GenerateInsertSQL(ctx context.Context, schema *batchflow.SQLSchema, data []map[string]any) (string, []any, error) {
// 简化实现
return "INSERT IGNORE INTO users (id, name, email) VALUES (?, ?, ?)", []any{1, "test", "test@example.com"}, nil
}
type postgresDriver struct{}
func (d *postgresDriver) GenerateInsertSQL(ctx context.Context, schema *batchflow.SQLSchema, data []map[string]any) (string, []any, error) {
return "INSERT INTO users (id, name, email) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING", []any{1, "test", "test@example.com"}, nil
}
type sqliteDriver struct{}
func (d *sqliteDriver) GenerateInsertSQL(ctx context.Context, schema *batchflow.SQLSchema, data []map[string]any) (string, []any, error) {
return "INSERT OR IGNORE INTO users (id, name, email) VALUES (?, ?, ?)", []any{1, "test", "test@example.com"}, nil
}