mirror of
https://github.com/restic/restic.git
synced 2025-10-10 09:53:43 +00:00
internal/ui/termstatus: Optimize and publish Truncate
name old time/op new time/op delta TruncateASCII-8 347ns ± 1% 69ns ± 1% -80.02% (p=0.000 n=9+10) TruncateUnicode-8 447ns ± 3% 348ns ± 1% -22.04% (p=0.000 n=10+10)
This commit is contained in:
@@ -19,13 +19,14 @@ func TestTruncate(t *testing.T) {
|
||||
{"foo", 0, ""},
|
||||
{"foo", -1, ""},
|
||||
{"Löwen", 4, "Löwe"},
|
||||
{"あああああああああ/data", 10, "あああああ"},
|
||||
{"あああああああああ/data", 11, "あああああ"},
|
||||
{"あああああ/data", 7, "あああ"},
|
||||
{"あああああ/data", 10, "あああああ"},
|
||||
{"あああああ/data", 11, "あああああ/"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run("", func(t *testing.T) {
|
||||
out := truncate(test.input, test.width)
|
||||
out := Truncate(test.input, test.width)
|
||||
if out != test.output {
|
||||
t.Fatalf("wrong output for input %v, width %d: want %q, got %q",
|
||||
test.input, test.width, test.output, out)
|
||||
@@ -33,3 +34,26 @@ func TestTruncate(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkTruncate(b *testing.B, s string, w int) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Truncate(s, w)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTruncateASCII(b *testing.B) {
|
||||
s := "This is an ASCII-only status message...\r\n"
|
||||
benchmarkTruncate(b, s, len(s)-1)
|
||||
}
|
||||
|
||||
func BenchmarkTruncateUnicode(b *testing.B) {
|
||||
s := "Hello World or Καλημέρα κόσμε or こんにちは 世界"
|
||||
w := 0
|
||||
for _, r := range s {
|
||||
w++
|
||||
if wideRune(r) {
|
||||
w++
|
||||
}
|
||||
}
|
||||
benchmarkTruncate(b, s, w-1)
|
||||
}
|
||||
|
Reference in New Issue
Block a user