BenchmarkVectorOperations 向量操作性能基准测试
(vectorCount int, dimension int)
| 193 | |
| 194 | // BenchmarkVectorOperations 向量操作性能基准测试 |
| 195 | func BenchmarkVectorOperations(vectorCount int, dimension int) error { |
| 196 | logger.SysLogf("=== 性能基准测试: %d个%d维向量 ===", vectorCount, dimension) |
| 197 | |
| 198 | config := LoadFromEnv() |
| 199 | store, err := NewVectorStore(config) |
| 200 | if err != nil { |
| 201 | return fmt.Errorf("创建向量存储失败: %v", err) |
| 202 | } |
| 203 | |
| 204 | ctx := context.Background() |
| 205 | if err := store.Connect(ctx); err != nil { |
| 206 | return fmt.Errorf("连接失败: %v", err) |
| 207 | } |
| 208 | defer store.Disconnect(ctx) |
| 209 | |
| 210 | testCollection := "benchmark_collection_" + fmt.Sprintf("%d", time.Now().Unix()) |
| 211 | |
| 212 | // 创建集合 |
| 213 | collectionConfig := CollectionConfig{ |
| 214 | Name: testCollection, |
| 215 | Dimension: dimension, |
| 216 | Metric: "cosine", |
| 217 | IndexType: "HNSW", |
| 218 | } |
| 219 | |
| 220 | if err := store.CreateCollection(ctx, collectionConfig); err != nil { |
| 221 | return fmt.Errorf("创建集合失败: %v", err) |
| 222 | } |
| 223 | defer store.DeleteCollection(ctx, testCollection) |
| 224 | |
| 225 | // 生成测试向量 |
| 226 | vectors := make([]VectorRecord, vectorCount) |
| 227 | for i := 0; i < vectorCount; i++ { |
| 228 | vector := make([]float32, dimension) |
| 229 | for j := 0; j < dimension; j++ { |
| 230 | vector[j] = float32(i*dimension + j) |
| 231 | } |
| 232 | vectors[i] = VectorRecord{ |
| 233 | ID: i + 1, |
| 234 | Vector: vector, |
| 235 | Metadata: map[string]interface{}{ |
| 236 | "index": i, |
| 237 | }, |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // 测试批量插入性能 |
| 242 | logger.SysLogf("插入 %d 个向量...", vectorCount) |
| 243 | startTime := time.Now() |
| 244 | if err := store.BatchInsert(ctx, testCollection, vectors); err != nil { |
| 245 | return fmt.Errorf("批量插入失败: %v", err) |
| 246 | } |
| 247 | insertDuration := time.Since(startTime) |
| 248 | insertThroughput := float64(vectorCount) / insertDuration.Seconds() |
| 249 | |
| 250 | logger.SysLogf("插入完成: 耗时 %v, 吞吐量 %.2f vectors/sec", insertDuration, insertThroughput) |
| 251 | |
| 252 | // 测试搜索性能 |
nothing calls this directly
no test coverage detected