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)
}
}
+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
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 {
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 {
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()
}
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) {
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) {