Repository navigation
Expand file tree
/
Copy pathtest-concurrent-insert-v3.go
More file actions
208 lines (175 loc) · 4.57 KB
/
Copy pathtest-concurrent-insert-v3.go
File metadata and controls
208 lines (175 loc) · 4.57 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//go:build ignore
package main
import (
"database/sql"
"fmt"
"os"
"strings"
"sync"
"time"
_ "modernc.org/sqlite"
)
func main() {
// 使用临时测试数据库
dbPath := "./test_concurrent.db"
// 删除旧的测试数据库
os.Remove(dbPath)
fmt.Printf("测试数据库: %s\n\n", dbPath)
// 直接使用database/sql,设置busy_timeout
db, err := sql.Open("sqlite", dbPath)
if err != nil {
fmt.Printf("打开数据库失败: %v\n", err)
return
}
defer db.Close()
defer os.Remove(dbPath) // 清理测试数据库
// 设置busy_timeout(10秒)
_, err = db.Exec("PRAGMA busy_timeout = 10000")
if err != nil {
fmt.Printf("设置busy_timeout失败: %v\n", err)
return
}
// 设置WAL模式
_, err = db.Exec("PRAGMA journal_mode = WAL")
if err != nil {
fmt.Printf("设置WAL模式失败: %v\n", err)
return
}
// 创建测试表
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS request_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
platform TEXT,
model TEXT,
provider TEXT,
http_code INTEGER,
input_tokens INTEGER,
output_tokens INTEGER,
cache_create_tokens INTEGER,
cache_read_tokens INTEGER,
reasoning_tokens INTEGER,
is_stream INTEGER,
duration_sec REAL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
if err != nil {
fmt.Printf("创建表失败: %v\n", err)
return
}
fmt.Println("✅ 数据库初始化成功(WAL模式 + 10秒超时)\n")
// 测试1: 不带重试的并发写入
fmt.Println("========== 测试1: 并发写入(无重试)==========")
testWithoutRetry(db, 10)
time.Sleep(100 * time.Millisecond)
// 测试2: 带重试的并发写入
fmt.Println("\n========== 测试2: 并发写入(带重试)==========")
testWithRetry(db, 10)
}
func testWithoutRetry(db *sql.DB, concurrency int) {
var wg sync.WaitGroup
errors := 0
success := 0
mu := sync.Mutex{}
start := time.Now()
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
_, err := db.Exec(`
INSERT INTO request_log (
platform, model, provider, http_code,
input_tokens, output_tokens, cache_create_tokens, cache_read_tokens,
reasoning_tokens, is_stream, duration_sec
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
"test1",
"test-model",
fmt.Sprintf("provider-%d", id),
200, 100, 200, 0, 0, 0, 0, 1.5,
)
mu.Lock()
if err != nil {
errors++
if strings.Contains(err.Error(), "SQLITE_BUSY") {
fmt.Printf(" ⚠️ [%d] SQLITE_BUSY(busy_timeout可能未生效)\n", id)
} else {
fmt.Printf(" ❌ [%d] 失败: %v\n", id, err)
}
} else {
success++
fmt.Printf(" ✅ [%d] 成功\n", id)
}
mu.Unlock()
}(i)
}
wg.Wait()
duration := time.Since(start)
fmt.Printf("\n结果统计:\n")
fmt.Printf(" 总请求: %d\n", concurrency)
fmt.Printf(" 成功: %d\n", success)
fmt.Printf(" 失败: %d\n", errors)
fmt.Printf(" 耗时: %v\n", duration)
}
func testWithRetry(db *sql.DB, concurrency int) {
var wg sync.WaitGroup
errors := 0
success := 0
retries := 0
mu := sync.Mutex{}
start := time.Now()
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
var lastErr error
for attempt := 0; attempt < 3; attempt++ {
_, err := db.Exec(`
INSERT INTO request_log (
platform, model, provider, http_code,
input_tokens, output_tokens, cache_create_tokens, cache_read_tokens,
reasoning_tokens, is_stream, duration_sec
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
"test2",
"test-model",
fmt.Sprintf("provider-%d", id),
200, 100, 200, 0, 0, 0, 0, 1.5,
)
if err == nil {
mu.Lock()
success++
if attempt > 0 {
retries++
fmt.Printf(" ✅ [%d] 成功(第%d次重试)\n", id, attempt+1)
} else {
fmt.Printf(" ✅ [%d] 成功\n", id)
}
mu.Unlock()
return
}
lastErr = err
if strings.Contains(err.Error(), "SQLITE_BUSY") || strings.Contains(err.Error(), "database is locked") {
time.Sleep(time.Duration(50*(attempt+1)) * time.Millisecond)
continue
}
break
}
mu.Lock()
errors++
fmt.Printf(" ❌ [%d] 失败(3次重试后): %v\n", id, lastErr)
mu.Unlock()
}(i)
}
wg.Wait()
duration := time.Since(start)
fmt.Printf("\n结果统计:\n")
fmt.Printf(" 总请求: %d\n", concurrency)
fmt.Printf(" 成功: %d\n", success)
fmt.Printf(" 失败: %d\n", errors)
fmt.Printf(" 重试成功: %d\n", retries)
fmt.Printf(" 耗时: %v\n", duration)
if errors == 0 {
fmt.Printf("\n ✅ 所有请求成功!带重试的方案有效。\n")
}
}