Files
3x-ui/internal/util/common/format_test.go
T
Javier Guerrero c321933a54 perf: reduce traffic formatter allocations (#6790)
Co-authored-by: Javier Guerrero <javier.guerrero@github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
2026-10-12 11:05:40 +02:00

35 lines
801 B
Go

package common
import "testing"
func TestFormatTraffic(t *testing.T) {
cases := []struct {
name string
bytes int64
want string
}{
{"zero", 0, "0.00B"},
{"under_one_kb", 512, "512.00B"},
{"exactly_one_kb", 1024, "1.00KB"},
{"one_and_a_half_kb", 1536, "1.50KB"},
{"one_mb", 1024 * 1024, "1.00MB"},
{"one_gb", 1024 * 1024 * 1024, "1.00GB"},
{"one_tb", 1024 * 1024 * 1024 * 1024, "1.00TB"},
{"one_pb", 1024 * 1024 * 1024 * 1024 * 1024, "1.00PB"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := FormatTraffic(c.bytes)
if got != c.want {
t.Fatalf("FormatTraffic(%d) = %q, want %q", c.bytes, got, c.want)
}
})
}
}
func BenchmarkFormatTraffic(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = FormatTraffic(1536 + int64(i%64)*1024)
}
}