Reuse gob.Encoder and Decoder in DeepCopy

This commit is contained in:
Clayton Coleman
2015-04-11 19:51:44 -04:00
parent d577db9987
commit f95cc2b8f2
2 changed files with 72 additions and 12 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package conversion
import (
"math/rand"
"reflect"
"testing"
@@ -106,3 +107,37 @@ func TestDeepCopyPointerSeparate(t *testing.T) {
t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
}
}
var result interface{}
func BenchmarkDeepCopy(b *testing.B) {
table := []interface{}{
map[string]string{},
int(5),
"hello world",
struct {
A, B, C struct {
D map[string]int
}
X []int
Y []byte
}{},
}
f := fuzz.New().RandSource(rand.NewSource(1)).NilChance(.5).NumElements(0, 100)
for i := range table {
out := table[i]
obj := reflect.New(reflect.TypeOf(out)).Interface()
f.Fuzz(obj)
table[i] = obj
}
b.ResetTimer()
var r interface{}
for i := 0; i < b.N; i++ {
for j := range table {
r, _ = DeepCopy(table[j])
}
}
result = r
}