MCPcopy
hub / github.com/google/go-cmp / BenchmarkBytes

Function BenchmarkBytes

cmp/compare_test.go:2962–3008  ·  view source on GitHub ↗

BenchmarkBytes benchmarks the performance of performing Equal or Diff on large slices of bytes.

(b *testing.B)

Source from the content-addressed store, hash-verified

2960// BenchmarkBytes benchmarks the performance of performing Equal or Diff on
2961// large slices of bytes.
2962func BenchmarkBytes(b *testing.B) {
2963 // Create a list of PathFilters that never apply, but are evaluated.
2964 const maxFilters = 5
2965 var filters cmp.Options
2966 errorIface := reflect.TypeOf((*error)(nil)).Elem()
2967 for i := 0; i <= maxFilters; i++ {
2968 filters = append(filters, cmp.FilterPath(func(p cmp.Path) bool {
2969 return p.Last().Type().AssignableTo(errorIface) // Never true
2970 }, cmp.Ignore()))
2971 }
2972
2973 type benchSize struct {
2974 label string
2975 size int64
2976 }
2977 for _, ts := range []benchSize{
2978 {"4KiB", 1 << 12},
2979 {"64KiB", 1 << 16},
2980 {"1MiB", 1 << 20},
2981 {"16MiB", 1 << 24},
2982 } {
2983 bx := append(append(make([]byte, ts.size/2), 'x'), make([]byte, ts.size/2)...)
2984 by := append(append(make([]byte, ts.size/2), 'y'), make([]byte, ts.size/2)...)
2985 b.Run(ts.label, func(b *testing.B) {
2986 // Iteratively add more filters that never apply, but are evaluated
2987 // to measure the cost of simply evaluating each filter.
2988 for i := 0; i <= maxFilters; i++ {
2989 b.Run(fmt.Sprintf("EqualFilter%d", i), func(b *testing.B) {
2990 b.ReportAllocs()
2991 b.SetBytes(2 * ts.size)
2992 for j := 0; j < b.N; j++ {
2993 cmp.Equal(bx, by, filters[:i]...)
2994 }
2995 })
2996 }
2997 for i := 0; i <= maxFilters; i++ {
2998 b.Run(fmt.Sprintf("DiffFilter%d", i), func(b *testing.B) {
2999 b.ReportAllocs()
3000 b.SetBytes(2 * ts.size)
3001 for j := 0; j < b.N; j++ {
3002 cmp.Diff(bx, by, filters[:i]...)
3003 }
3004 })
3005 }
3006 })
3007 }
3008}

Callers

nothing calls this directly

Calls 6

FilterPathFunction · 0.92
IgnoreFunction · 0.92
EqualFunction · 0.92
DiffFunction · 0.92
LastMethod · 0.80
TypeMethod · 0.65

Tested by

no test coverage detected