mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
c12a4f72c7
Every raw, JSON and Clash subscription request read its eight presentation settings (host sort mode, datepicker, identity and info-node toggles, the expired and depleted templates, inclusive expiry and its timezone) with one query each. Read them in one query per request preparation. Missing keys, stored empty strings, duplicate keys and malformed booleans resolve as the single-key getters did, and a failed batch falls back to those getters so a readable setting keeps its value. Closes #7201
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package sub
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/op/go-logging"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/logger"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
|
|
)
|
|
|
|
func BenchmarkSubscriptionRenderSettings(b *testing.B) {
|
|
logger.InitLogger(logging.ERROR)
|
|
if os.Getenv("XUI_DB_TYPE") == "postgres" {
|
|
cleanup, err := testpg.IsolatePackage("subscription_render_bench")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
b.Cleanup(cleanup)
|
|
}
|
|
seedSubDB(b)
|
|
rows := []model.Setting{
|
|
{Key: "subHostSortMode", Value: "hosts-first"},
|
|
{Key: "datepicker", Value: "gregorian"},
|
|
{Key: "subShowIdentityOnAllLinks", Value: "true"},
|
|
{Key: "subInfoNodeEnable", Value: "false"},
|
|
{Key: "subExpiredTemplate", Value: "Expired"},
|
|
{Key: "subTrafficDepletedTemplate", Value: "Depleted"},
|
|
{Key: "subCalendarExpireInclusive", Value: "true"},
|
|
{Key: "timeLocation", Value: "UTC"},
|
|
}
|
|
for i := range 182 {
|
|
rows = append(rows, model.Setting{Key: fmt.Sprintf("fixture-%03d", i), Value: "fixture-value"})
|
|
}
|
|
seedRenderSettings(b, rows...)
|
|
for i := range 12 {
|
|
seedSubInbound(b, "bench-sub", fmt.Sprintf("fixture-%02d", i), 4900+i, i, wsTLSStream)
|
|
}
|
|
gin.SetMode(gin.TestMode)
|
|
router := newSubscriptionTestRouter(subscriptionTestRouterConfig{})
|
|
base := NewSubService("")
|
|
reads := countRenderSettingQueries(b)
|
|
|
|
for _, route := range []string{"prepare", "sub", "json", "clash"} {
|
|
b.Run(route, func(b *testing.B) {
|
|
request := func() {
|
|
if route == "prepare" {
|
|
_ = base.ForRequest("sub.example.com")
|
|
return
|
|
}
|
|
res := httptest.NewRecorder()
|
|
router.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "http://sub.example.com/"+route+"/bench-sub", nil))
|
|
if res.Code != http.StatusOK {
|
|
b.Errorf("%s: HTTP %d: %s", route, res.Code, res.Body.String())
|
|
}
|
|
}
|
|
for range 8 {
|
|
request()
|
|
}
|
|
reads.Store(0)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
request()
|
|
}
|
|
})
|
|
b.StopTimer()
|
|
b.ReportMetric(float64(reads.Load())/float64(b.N), "render-sql/op")
|
|
})
|
|
}
|
|
}
|