package service import ( "encoding/json" "fmt" "slices" "strings" "testing" "time" "gorm.io/gorm" "github.com/mhsanaei/3x-ui/v3/internal/database" "github.com/mhsanaei/3x-ui/v3/internal/database/model" "github.com/mhsanaei/3x-ui/v3/internal/testpg" ) // BenchmarkSlimInboundList compares the list service and its JSON encoding, // with dedicated SQL timing for the inbound queries. func BenchmarkSlimInboundList(b *testing.B) { cleanup, err := testpg.IsolatePackage("slim_settings_bench") if err != nil { b.Fatal(err) } b.Cleanup(cleanup) setupBulkDB(b) db := database.GetDB() svc := new(InboundService) var sqlStart time.Time var sqlElapsed time.Duration if err := db.Callback().Query().Before("gorm:query").Register("bench:inboundStart", func(tx *gorm.DB) { if tx.Statement.Table == "inbounds" { sqlStart = time.Now() } }); err != nil { b.Fatal(err) } if err := db.Callback().Query().After("gorm:query").Register("bench:inboundElapsed", func(tx *gorm.DB) { if tx.Statement.Table == "inbounds" { sqlElapsed += time.Since(sqlStart) } }); err != nil { b.Fatal(err) } for _, count := range []int{1000, 10000, 20000} { for _, shared := range []bool{false, true} { userID := count * 2 if shared { userID++ } settingsBytes := 0 for inbound := range 2 { clients := make([]map[string]any, count) for i := range clients { emailIndex := i if !shared { emailIndex += inbound * count } clients[i] = map[string]any{ "email": fmt.Sprintf("client-%d@example.test", emailIndex), "enable": true, "comment": "fixture", "flow": "xtls-rprx-vision", "id": strings.Repeat("a", 36), "subId": strings.Repeat("s", 16), } } payload, err := json.Marshal(map[string]any{"clients": clients, "decryption": "none"}) if err != nil { b.Fatal(err) } settingsBytes += len(payload) row := &model.Inbound{UserId: userID, Port: 2096, Tag: fmt.Sprintf("bench-%d-%d", userID, inbound), Protocol: model.VLESS, Enable: true, Settings: string(payload)} if err := db.Create(row).Error; err != nil { b.Fatal(err) } } baseline := func() ([]*model.Inbound, error) { var rows []*model.Inbound if err := db.Where("user_id = ?", userID).Order("id ASC").Find(&rows).Error; err != nil { return nil, err } if err := attachClientStats(db, rows); err != nil { return nil, err } svc.annotateFallbackParents(db, rows) svc.annotateLocalOriginGuid(rows) svc.backfillClientStats(db, rows) svc.overlayInboundsClientStats(db, rows) for _, row := range rows { row.Settings = slimSettingsClients(row.Settings) } return rows, nil } if _, err := svc.GetInboundsSlim(userID); err != nil { b.Fatal(err) } for _, variant := range []struct { name string read func() ([]*model.Inbound, error) settingsBytes int }{ {"full-read", baseline, settingsBytes}, {"warm-projection", func() ([]*model.Inbound, error) { return svc.GetInboundsSlim(userID) }, 0}, } { b.Run(fmt.Sprintf("%d/shared=%t/%s", count, shared, variant.name), func(b *testing.B) { var samples []time.Duration var responseBytes int sqlElapsed = 0 b.ReportAllocs() for b.Loop() { start := time.Now() rows, err := variant.read() if err != nil { b.Fatal(err) } response, err := json.Marshal(rows) if err != nil { b.Fatal(err) } responseBytes = len(response) samples = append(samples, time.Since(start)) } slices.Sort(samples) b.ReportMetric(float64(samples[(len(samples)-1)/2].Microseconds()), "p50-us") b.ReportMetric(float64(samples[(len(samples)*95+99)/100-1].Microseconds()), "p95-us") b.ReportMetric(float64(sqlElapsed.Microseconds())/float64(b.N), "inbound-sql-us/op") b.ReportMetric(float64(variant.settingsBytes), "settings-SQL-B/op") b.ReportMetric(float64(responseBytes), "response-B/op") }) } } } }