From f5f5a00b92a07ebe5a371b515cb2bdacf89f0d91 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 10 Feb 2017 16:33:21 -0500 Subject: [PATCH] reflect: parallelize benchmarks Add a benchmark for PtrTo: it's the motivation for #17973, which is the motivation for #18177. Results remain comparable with the non-parallel version with -cpu=1: benchmark old ns/op new ns/op delta BenchmarkCall 357 360 +0.84% BenchmarkCall-6 90.3 90.7 +0.44% BenchmarkCallArgCopy/size=128 319 323 +1.25% BenchmarkCallArgCopy/size=128-6 329 82.2 -75.02% BenchmarkCallArgCopy/size=256 354 335 -5.37% BenchmarkCallArgCopy/size=256-6 340 85.2 -74.94% BenchmarkCallArgCopy/size=1024 374 703 +87.97% BenchmarkCallArgCopy/size=1024-6 378 95.8 -74.66% BenchmarkCallArgCopy/size=4096 627 631 +0.64% BenchmarkCallArgCopy/size=4096-6 643 120 -81.34% BenchmarkCallArgCopy/size=65536 10502 10169 -3.17% BenchmarkCallArgCopy/size=65536-6 10298 2240 -78.25% BenchmarkFieldByName1 139 132 -5.04% BenchmarkFieldByName1-6 144 24.9 -82.71% BenchmarkFieldByName2 2721 2778 +2.09% BenchmarkFieldByName2-6 3953 578 -85.38% BenchmarkFieldByName3 19136 18357 -4.07% BenchmarkFieldByName3-6 23072 3850 -83.31% BenchmarkInterfaceBig 12.7 15.5 +22.05% BenchmarkInterfaceBig-6 14.2 2.48 -82.54% BenchmarkInterfaceSmall 13.1 15.1 +15.27% BenchmarkInterfaceSmall-6 13.0 2.54 -80.46% BenchmarkNew 43.8 43.0 -1.83% BenchmarkNew-6 40.5 6.67 -83.53% benchmark old MB/s new MB/s speedup BenchmarkCallArgCopy/size=128 400.24 395.15 0.99x BenchmarkCallArgCopy/size=128-6 388.74 1557.76 4.01x BenchmarkCallArgCopy/size=256 722.44 762.44 1.06x BenchmarkCallArgCopy/size=256-6 751.98 3003.83 3.99x BenchmarkCallArgCopy/size=1024 2733.22 1455.50 0.53x BenchmarkCallArgCopy/size=1024-6 2706.40 10687.53 3.95x BenchmarkCallArgCopy/size=4096 6523.32 6488.25 0.99x BenchmarkCallArgCopy/size=4096-6 6363.85 34003.09 5.34x BenchmarkCallArgCopy/size=65536 6239.88 6444.46 1.03x BenchmarkCallArgCopy/size=65536-6 6363.83 29255.26 4.60x benchmark old allocs new allocs delta BenchmarkCall 0 0 +0.00% BenchmarkCall-6 0 0 +0.00% benchmark old bytes new bytes delta BenchmarkCall 0 0 +0.00% BenchmarkCall-6 0 0 +0.00% updates #17973 updates #18177 Change-Id: If70c5c742e8d1b138347f4963ad7cff38fffc018 Reviewed-on: https://go-review.googlesource.com/36831 Run-TryBot: Bryan Mills TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/reflect/all_test.go | 78 ++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index ff0e7e5791..1ec4f7954c 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -1576,9 +1576,11 @@ func BenchmarkCallArgCopy(b *testing.B) { args := []Value{size.arg} b.SetBytes(int64(size.arg.Len())) b.ResetTimer() - for i := 0; i < b.N; i++ { - size.fv.Call(args) - } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + size.fv.Call(args) + } + }) } name := fmt.Sprintf("size=%v", size.arg.Len()) b.Run(name, bench) @@ -2556,6 +2558,28 @@ func TestPtrToGC(t *testing.T) { } } +func BenchmarkPtrTo(b *testing.B) { + // Construct a type with a zero ptrToThis. + type T struct{ int } + t := SliceOf(TypeOf(T{})) + ptrToThis := ValueOf(t).Elem().FieldByName("ptrToThis") + if !ptrToThis.IsValid() { + b.Fatalf("%v has no ptrToThis field; was it removed from rtype?", t) + } + if ptrToThis.Int() != 0 { + b.Fatalf("%v.ptrToThis unexpectedly nonzero", t) + } + b.ResetTimer() + + // Now benchmark calling PtrTo on it: we'll have to hit the ptrMap cache on + // every call. + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + PtrTo(t) + } + }) +} + func TestAddr(t *testing.T) { var p struct { X, Y int @@ -4909,16 +4933,20 @@ type B1 struct { func BenchmarkFieldByName1(b *testing.B) { t := TypeOf(B1{}) - for i := 0; i < b.N; i++ { - t.FieldByName("Z") - } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + t.FieldByName("Z") + } + }) } func BenchmarkFieldByName2(b *testing.B) { t := TypeOf(S3{}) - for i := 0; i < b.N; i++ { - t.FieldByName("B") - } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + t.FieldByName("B") + } + }) } type R0 struct { @@ -5001,9 +5029,11 @@ func TestEmbed(t *testing.T) { func BenchmarkFieldByName3(b *testing.B) { t := TypeOf(R0{}) - for i := 0; i < b.N; i++ { - t.FieldByName("X") - } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + t.FieldByName("X") + } + }) } type S struct { @@ -5013,9 +5043,11 @@ type S struct { func BenchmarkInterfaceBig(b *testing.B) { v := ValueOf(S{}) - for i := 0; i < b.N; i++ { - v.Interface() - } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + v.Interface() + } + }) b.StopTimer() } @@ -5031,9 +5063,11 @@ func TestAllocsInterfaceBig(t *testing.T) { func BenchmarkInterfaceSmall(b *testing.B) { v := ValueOf(int64(0)) - for i := 0; i < b.N; i++ { - v.Interface() - } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + v.Interface() + } + }) } func TestAllocsInterfaceSmall(t *testing.T) { @@ -6007,9 +6041,11 @@ func TestOffsetLock(t *testing.T) { func BenchmarkNew(b *testing.B) { v := TypeOf(XM{}) - for i := 0; i < b.N; i++ { - New(v) - } + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + New(v) + } + }) } func TestSwapper(t *testing.T) { -- 2.48.1