| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package main
- import (
- "git.ycl.pub/yl32940/TsDB2/DB"
- "fmt"
- "os"
- "time"
- )
- type Record struct {
- DB.BaseModel
- Name string `vrtx:"maxlen:32"`
- Age int64
- Score int64
- }
- const dbPath = "dirty_threshold_test.db"
- func testDirtyThreshold(threshold int, rows int) {
- os.Remove(dbPath)
- db, err := DB.Open(dbPath)
- if err != nil {
- panic(err)
- }
- defer db.Close()
- // 禁用防抖,只靠脏页阈值触发 Commit
- db.SetAutoCommitDelay(0)
- db.SetDirtyThreshold(threshold)
- tbl, err := DB.UseTable[Record](db, "test")
- if err != nil {
- panic(err)
- }
- start := time.Now()
- for i := 1; i <= rows; i++ {
- err := tbl.Insert(&Record{
- Name: fmt.Sprintf("Emp%07d", i),
- Age: int64(20 + i%40),
- Score: int64(i * 10),
- })
- if err != nil {
- panic(err)
- }
- }
- elapsed := time.Since(start)
- fi, _ := os.Stat(dbPath)
- fileSize := float64(fi.Size()) / 1024 / 1024
- fmt.Printf(" DirtyThreshold=%4d页 (%5.1fMB) | 插入 %6d行 | 耗时: %6.2fs | 文件: %5.1fMB\n",
- threshold, float64(threshold)*16/1024, rows, elapsed.Seconds(), fileSize)
- }
- func main() {
- fmt.Println("==============================================")
- fmt.Println(" DirtyThreshold 脏页阈值性能对比测试")
- fmt.Print("==============================================\n\n")
- rows := 50000
- fmt.Printf("测试场景: 插入 %d 行数据(禁用防抖,仅靠脏页阈值)\n\n", rows)
- // 测试不同阈值
- testDirtyThreshold(20, rows) // 用户建议值 (320KB)
- testDirtyThreshold(100, rows) // 小型 (1.6MB)
- testDirtyThreshold(500, rows) // 中小型 (7.8MB)
- testDirtyThreshold(2000, rows) // 当前默认值 (32MB)
- testDirtyThreshold(5000, rows) // 大型 (78MB)
- fmt.Println("\n说明:")
- fmt.Println(" - 阈值越小 → Commit 越频繁 → 磁盘 I/O 越多 → 性能越差")
- fmt.Println(" - 阈值越大 → 内存占用越高 → Commit 次数越少 → 性能越好")
- fmt.Println(" - 2000 页 (32MB) 是性能和内存的平衡点")
- }
|