main.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "git.ycl.pub/yl32940/TsDB2/DB"
  4. "fmt"
  5. "os"
  6. "time"
  7. )
  8. type Record struct {
  9. DB.BaseModel
  10. Name string `vrtx:"maxlen:32"`
  11. }
  12. const dbPath = "commit_preset_test.db"
  13. func testPreset(preset string, rows int) {
  14. os.Remove(dbPath)
  15. db, err := DB.Open(dbPath)
  16. if err != nil {
  17. panic(err)
  18. }
  19. db.SetCommitPreset(preset)
  20. tbl, err := DB.UseTable[Record](db, "test")
  21. if err != nil {
  22. panic(err)
  23. }
  24. start := time.Now()
  25. for i := 1; i <= rows; i++ {
  26. tbl.Insert(&Record{
  27. Name: fmt.Sprintf("Emp%07d", i),
  28. })
  29. }
  30. elapsed := time.Since(start)
  31. fi, _ := os.Stat(dbPath)
  32. fileSize := float64(fi.Size()) / 1024 / 1024
  33. fmt.Printf(" %-8s | %6d行 | %5.2fs | %5.1fMB\n", preset, rows, elapsed.Seconds(), fileSize)
  34. db.Close()
  35. }
  36. func main() {
  37. fmt.Println("==============================================")
  38. fmt.Println(" 提交策略预设性能对比测试")
  39. fmt.Print("==============================================\n\n")
  40. rows := 10000
  41. fmt.Printf("测试场景: 插入 %d 行数据\n\n", rows)
  42. fmt.Println(" 预设类型 | 行数 | 耗时 | 文件大小")
  43. fmt.Println(" ---------+--------+--------+----------")
  44. testPreset("batch", rows) // 离线批量导入
  45. testPreset("online", rows) // 在线业务
  46. testPreset("safe", rows) // 关键数据
  47. fmt.Println("\n说明:")
  48. fmt.Println(" - batch: 高吞吐,适合离线导入 (断电风险: ~10,000行)")
  49. fmt.Println(" - online: 平衡性能和安全 (断电风险: ~2,000行)")
  50. fmt.Println(" - safe: 最高安全,适合关键业务 (断电风险: ~400行)")
  51. fmt.Println("\n选择建议:")
  52. fmt.Println(" - 批量导入脚本 → batch")
  53. fmt.Println(" - Web/API 服务 → online")
  54. fmt.Println(" - 金融/医疗数据 → safe")
  55. }