main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. Age int64
  12. Score int64
  13. }
  14. const dbPath = "dirty_threshold_test.db"
  15. func testDirtyThreshold(threshold int, rows int) {
  16. os.Remove(dbPath)
  17. db, err := DB.Open(dbPath)
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer db.Close()
  22. // 禁用防抖,只靠脏页阈值触发 Commit
  23. db.SetAutoCommitDelay(0)
  24. db.SetDirtyThreshold(threshold)
  25. tbl, err := DB.UseTable[Record](db, "test")
  26. if err != nil {
  27. panic(err)
  28. }
  29. start := time.Now()
  30. for i := 1; i <= rows; i++ {
  31. err := tbl.Insert(&Record{
  32. Name: fmt.Sprintf("Emp%07d", i),
  33. Age: int64(20 + i%40),
  34. Score: int64(i * 10),
  35. })
  36. if err != nil {
  37. panic(err)
  38. }
  39. }
  40. elapsed := time.Since(start)
  41. fi, _ := os.Stat(dbPath)
  42. fileSize := float64(fi.Size()) / 1024 / 1024
  43. fmt.Printf(" DirtyThreshold=%4d页 (%5.1fMB) | 插入 %6d行 | 耗时: %6.2fs | 文件: %5.1fMB\n",
  44. threshold, float64(threshold)*16/1024, rows, elapsed.Seconds(), fileSize)
  45. }
  46. func main() {
  47. fmt.Println("==============================================")
  48. fmt.Println(" DirtyThreshold 脏页阈值性能对比测试")
  49. fmt.Print("==============================================\n\n")
  50. rows := 50000
  51. fmt.Printf("测试场景: 插入 %d 行数据(禁用防抖,仅靠脏页阈值)\n\n", rows)
  52. // 测试不同阈值
  53. testDirtyThreshold(20, rows) // 用户建议值 (320KB)
  54. testDirtyThreshold(100, rows) // 小型 (1.6MB)
  55. testDirtyThreshold(500, rows) // 中小型 (7.8MB)
  56. testDirtyThreshold(2000, rows) // 当前默认值 (32MB)
  57. testDirtyThreshold(5000, rows) // 大型 (78MB)
  58. fmt.Println("\n说明:")
  59. fmt.Println(" - 阈值越小 → Commit 越频繁 → 磁盘 I/O 越多 → 性能越差")
  60. fmt.Println(" - 阈值越大 → 内存占用越高 → Commit 次数越少 → 性能越好")
  61. fmt.Println(" - 2000 页 (32MB) 是性能和内存的平衡点")
  62. }