mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-29 22:14:50 +03:00
perf(traffic): size live client deltas by active rows (#7265)
splitMovedClientTraffics reserved room for every registered client before filtering down to the ones that moved bytes. On a large panel where almost everyone is idle, that was megabytes of unused slices and map buckets on every five-second poll. Count the moved rows first and size the slices and the email set from that count. Order, set membership, zero-delta filtering and the non-nil empty results are unchanged. Measured here at 50,000 rows with 1% active: 2.95 MB -> 39.6 KB and 109 -> 60 us per call; with every row active it costs about 4% more time for the same allocations. Fixes #7262
This commit is contained in:
@@ -53,9 +53,15 @@ const clientStatsSnapshotMaxClients = 5000
|
||||
// frame from growing with the client count — at 5k clients it was carrying about
|
||||
// a megabyte of zeros every five seconds.
|
||||
func splitMovedClientTraffics(clientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, []string, map[string]bool) {
|
||||
moved := make([]*xray.ClientTraffic, 0, len(clientTraffics))
|
||||
emails := make([]string, 0, len(clientTraffics))
|
||||
active := make(map[string]bool, len(clientTraffics))
|
||||
capacity := 0
|
||||
for _, ct := range clientTraffics {
|
||||
if ct != nil && ct.Up+ct.Down > 0 {
|
||||
capacity++
|
||||
}
|
||||
}
|
||||
moved := make([]*xray.ClientTraffic, 0, capacity)
|
||||
emails := make([]string, 0, capacity)
|
||||
active := make(map[string]bool, capacity)
|
||||
for _, ct := range clientTraffics {
|
||||
if ct == nil || ct.Up+ct.Down <= 0 {
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
||||
)
|
||||
|
||||
var splitMovedBenchmarkResult struct {
|
||||
moved []*xray.ClientTraffic
|
||||
emails []string
|
||||
active map[string]bool
|
||||
}
|
||||
|
||||
func BenchmarkSplitMovedClientTraffics(b *testing.B) {
|
||||
for _, count := range []int{1_000, 10_000, 50_000} {
|
||||
for _, percent := range []int{0, 1, 10, 50, 100} {
|
||||
b.Run(fmt.Sprintf("rows=%d/active=%d%%", count, percent), func(b *testing.B) {
|
||||
clients := make([]xray.ClientTraffic, count)
|
||||
rows := make([]*xray.ClientTraffic, count)
|
||||
for i := range clients {
|
||||
clients[i].Email = fmt.Sprintf("client-%d@example.test", i)
|
||||
if i*37%100 < percent {
|
||||
clients[i].Up, clients[i].Down = 1_024, 2_048
|
||||
}
|
||||
rows[i] = &clients[i]
|
||||
}
|
||||
b.ReportAllocs()
|
||||
for b.Loop() {
|
||||
splitMovedBenchmarkResult.moved, splitMovedBenchmarkResult.emails, splitMovedBenchmarkResult.active = splitMovedClientTraffics(rows)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func TestSplitMovedClientTraffics(t *testing.T) {
|
||||
moved, emails, active := splitMovedClientTraffics([]*xray.ClientTraffic{
|
||||
{Email: "a@x"}, {Email: "b@x"},
|
||||
})
|
||||
if len(moved) != 0 || len(emails) != 0 || len(active) != 0 {
|
||||
if moved == nil || emails == nil || active == nil || len(moved) != 0 || len(emails) != 0 || len(active) != 0 {
|
||||
t.Fatalf("expected an empty split, got %d/%d/%d", len(moved), len(emails), len(active))
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user