]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.fuzz] internal/fuzz: stablize mutator benchmark and add additional benchmarks
authorRoland Shoemaker <roland@golang.org>
Fri, 4 Jun 2021 01:04:53 +0000 (18:04 -0700)
committerRoland Shoemaker <roland@golang.org>
Fri, 4 Jun 2021 15:59:32 +0000 (15:59 +0000)
Adds a few new benchmarks, and attempts to reduce the variability of the
existing BenchmarkMutatorBytes benchmark. These should help provide some
insight when we're working on performance issues.

Change-Id: I45b68ae36da99ec2eb4a610b7a3fc6fbf3d9494a
Reviewed-on: https://go-review.googlesource.com/c/go/+/324969
Trust: Roland Shoemaker <roland@golang.org>
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Roland Shoemaker <roland@golang.org>
Reviewed-by: Katie Hockman <katie@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>

src/internal/fuzz/mutator_test.go
src/internal/fuzz/worker_test.go [new file with mode: 0644]

index b1b5311639419d0ef40babe3d0defbb130aabf8b..5fcfb27c163bcf96b71d0367a0e7261ad0bd9c1d 100644 (file)
@@ -5,11 +5,17 @@
 package fuzz
 
 import (
+       "fmt"
+       "os"
        "strconv"
        "testing"
 )
 
 func BenchmarkMutatorBytes(b *testing.B) {
+       origEnv := os.Getenv("GODEBUG")
+       defer func() { os.Setenv("GODEBUG", origEnv) }()
+       os.Setenv("GODEBUG", fmt.Sprintf("%s,fuzzseed=123", origEnv))
+
        for _, size := range []int{
                1,
                10,
@@ -20,10 +26,74 @@ func BenchmarkMutatorBytes(b *testing.B) {
        } {
                size := size
                b.Run(strconv.Itoa(size), func(b *testing.B) {
-                       vals := []interface{}{make([]byte, size)}
-                       m := newMutator()
+                       buf := make([]byte, size)
+                       b.ResetTimer()
+
+                       for i := 0; i < b.N; i++ {
+                               // resize buffer to the correct shape and reset the PCG
+                               buf = buf[0:size]
+                               m := newMutator()
+                               m.mutate([]interface{}{buf}, workerSharedMemSize)
+                       }
+               })
+       }
+}
+
+func BenchmarkMutatorString(b *testing.B) {
+       origEnv := os.Getenv("GODEBUG")
+       defer func() { os.Setenv("GODEBUG", origEnv) }()
+       os.Setenv("GODEBUG", fmt.Sprintf("%s,fuzzseed=123", origEnv))
+
+       for _, size := range []int{
+               1,
+               10,
+               100,
+               1000,
+               10000,
+               100000,
+       } {
+               size := size
+               b.Run(strconv.Itoa(size), func(b *testing.B) {
+                       buf := make([]byte, size)
+                       b.ResetTimer()
+
+                       for i := 0; i < b.N; i++ {
+                               // resize buffer to the correct shape and reset the PCG
+                               buf = buf[0:size]
+                               m := newMutator()
+                               m.mutate([]interface{}{string(buf)}, workerSharedMemSize)
+                       }
+               })
+       }
+}
+
+func BenchmarkMutatorAllBasicTypes(b *testing.B) {
+       origEnv := os.Getenv("GODEBUG")
+       defer func() { os.Setenv("GODEBUG", origEnv) }()
+       os.Setenv("GODEBUG", fmt.Sprintf("%s,fuzzseed=123", origEnv))
+
+       types := []interface{}{
+               []byte(""),
+               string(""),
+               false,
+               float32(0),
+               float64(0),
+               int(0),
+               int8(0),
+               int16(0),
+               int32(0),
+               int64(0),
+               uint8(0),
+               uint16(0),
+               uint32(0),
+               uint64(0),
+       }
+
+       for _, t := range types {
+               b.Run(fmt.Sprintf("%T", t), func(b *testing.B) {
                        for i := 0; i < b.N; i++ {
-                               m.mutate(vals, workerSharedMemSize)
+                               m := newMutator()
+                               m.mutate([]interface{}{t}, workerSharedMemSize)
                        }
                })
        }
diff --git a/src/internal/fuzz/worker_test.go b/src/internal/fuzz/worker_test.go
new file mode 100644 (file)
index 0000000..10d61b1
--- /dev/null
@@ -0,0 +1,42 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package fuzz
+
+import (
+       "context"
+       "fmt"
+       "os"
+       "testing"
+)
+
+func BenchmarkWorkerFuzzOverhead(b *testing.B) {
+       origEnv := os.Getenv("GODEBUG")
+       defer func() { os.Setenv("GODEBUG", origEnv) }()
+       os.Setenv("GODEBUG", fmt.Sprintf("%s,fuzzseed=123", origEnv))
+
+       ws := &workerServer{
+               fuzzFn:     func(_ CorpusEntry) error { return nil },
+               workerComm: workerComm{memMu: make(chan *sharedMem, 1)},
+       }
+
+       mem, err := sharedMemTempFile(workerSharedMemSize)
+       if err != nil {
+               b.Fatalf("failed to create temporary shared memory file: %s", err)
+       }
+
+       initialVal := []interface{}{make([]byte, 32)}
+       encodedVals := marshalCorpusFile(initialVal...)
+       mem.setValue(encodedVals)
+
+       ws.memMu <- mem
+
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               ws.m = newMutator()
+               mem.setValue(encodedVals)
+
+               ws.fuzz(context.Background(), fuzzArgs{Limit: 1})
+       }
+}