aboutsummaryrefslogtreecommitdiff
path: root/src/internal/fuzz/mutator_test.go
blob: ee2912dfd218012eb418e531ad2ac9b13bb7fe8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 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 (
	"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))
	m := newMutator()

	for _, size := range []int{
		1,
		10,
		100,
		1000,
		10000,
		100000,
	} {
		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.r = newPcgRand()
				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))
	m := newMutator()

	for _, size := range []int{
		1,
		10,
		100,
		1000,
		10000,
		100000,
	} {
		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.r = newPcgRand()
				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))
	m := newMutator()

	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.r = newPcgRand()
				m.mutate([]interface{}{t}, workerSharedMemSize)
			}
		})
	}
}