From 0e3b2dca245a9088097359214f43d143ca1749ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Fri, 9 Feb 2024 12:56:21 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20ToString:?= =?UTF-8?q?=20improve=20test=20and=20bench=20setup=20-=20add=20string=20sl?= =?UTF-8?q?ice=20case=20-=20add=20parallel=20instruction=20for=20child=20t?= =?UTF-8?q?ests=20-=20benchmark=20table=20and=20parallel=20execution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convert_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/convert_test.go b/convert_test.go index ddc8c1a..f902083 100644 --- a/convert_test.go +++ b/convert_test.go @@ -92,10 +92,12 @@ func Test_ToString(t *testing.T) { {float64(3.14159), "3.14159"}, {time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC), "2000-01-01 12:34:56"}, {struct{ Name string }{"John"}, "{John}"}, + {[]string{"Hello", "World"}, "[Hello World]"}, } for _, tc := range tests { t.Run(reflect.TypeOf(tc.input).String(), func(t *testing.T) { + t.Parallel() res := ToString(tc.input) require.Equal(t, tc.expected, res) }) @@ -111,6 +113,7 @@ func Test_ToString(t *testing.T) { } for _, tc := range testsPtr { t.Run("pointer to "+reflect.TypeOf(tc.input).Elem().String(), func(t *testing.T) { + t.Parallel() res := ToString(tc.input) require.Equal(t, tc.expected, res) }) @@ -136,10 +139,16 @@ func Benchmark_ToString(b *testing.B) { float32(3.14), float64(3.14), time.Now(), + []string{"Hello", "World"}, } - for n := 0; n < b.N; n++ { - for _, value := range values { - _ = ToString(value) - } + for _, value := range values { + b.Run(reflect.TypeOf(value).String(), func(b *testing.B) { + b.ReportAllocs() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + _ = ToString(value) + } + }) + }) } }