From 1d23ffa157447c3452bb2e31f0bdf9118fafb88a Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 27 Apr 2022 20:23:23 -0700 Subject: [PATCH] size_test: add parseSize benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Shows on my laptop (6 iterations processed by benchstat): name time/op ParseSize-4 10.6µs ± 3% name alloc/op ParseSize-4 3.26kB ± 0% name allocs/op ParseSize-4 72.0 ± 0% Signed-off-by: Kir Kolyshkin --- size_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/size_test.go b/size_test.go index 9931199..5bdaddd 100644 --- a/size_test.go +++ b/size_test.go @@ -140,6 +140,18 @@ func TestRAMInBytes(t *testing.T) { assertError(t, RAMInBytes, "32bm") } +func BenchmarkParseSize(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, s := range []string{ + "", "32", "32b", "32 B", "32k", "32.5 K", "32kb", "32 Kb", + "32.8Mb", "32.9Gb", "32.777Tb", "32Pb", "0.3Mb", "-1", + } { + FromHumanSize(s) + RAMInBytes(s) + } + } +} + func assertEquals(t *testing.T, expected, actual interface{}) { if expected != actual { t.Errorf("Expected '%v' but got '%v'", expected, actual)