mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-22 08:35:18 +08:00
perf: reduce traffic formatter allocations (#6790)
Co-authored-by: Javier Guerrero <[email protected]> Co-authored-by: Copilot App <[email protected]>
This commit is contained in:
co-authored by
Javier Guerrero
Copilot App
parent
d43e0c7ead
commit
c321933a54
@@ -1,8 +1,6 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
import "strconv"
|
||||
|
||||
// FormatTraffic formats traffic bytes into human-readable units (B, KB, MB, GB, TB, PB).
|
||||
func FormatTraffic(trafficBytes int64) string {
|
||||
@@ -14,5 +12,5 @@ func FormatTraffic(trafficBytes int64) string {
|
||||
size /= 1024
|
||||
unitIndex++
|
||||
}
|
||||
return fmt.Sprintf("%.2f%s", size, units[unitIndex])
|
||||
return strconv.FormatFloat(size, 'f', 2, 64) + units[unitIndex]
|
||||
}
|
||||
|
||||
@@ -26,3 +26,9 @@ func TestFormatTraffic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFormatTraffic(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = FormatTraffic(1536 + int64(i%64)*1024)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user