Skip to content

Commit

Permalink
redid a lot of the underlying structure and added fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
JAicewizard committed Dec 27, 2020
1 parent 36fc9ab commit fecf48b
Show file tree
Hide file tree
Showing 14 changed files with 895 additions and 446 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ Data is are benchmarks that use Data, map benchmarks just convert the data to a

r5 3600 make bench
```
BenchmarkV3 3015724 3935 ns/op 1157 B/op 33 allocs/op
BenchmarkV3Decode 4057526 2961 ns/op 739 B/op 28 allocs/op
BenchmarkV3Encode 13256769 896 ns/op 383 B/op 5 allocs/op
BenchmarkV2 4915644 2423 ns/op 1389 B/op 27 allocs/op
BenchmarkV2Decode 9181489 1292 ns/op 784 B/op 23 allocs/op
BenchmarkV2Encode 10495838 1113 ns/op 592 B/op 4 allocs/op
BenchmarkGobMap 979080 11936 ns/op 1916 B/op 68 allocs/op
BenchmarkGobMapDecode 1848817 6436 ns/op 1260 B/op 48 allocs/op
BenchmarkGobMapEncode 2300364 5208 ns/op 656 B/op 20 allocs/op
```
BenchmarkV3 305139 3920 ns/op 1326 B/op 33 allocs/op
BenchmarkV3Decode 374406 2980 ns/op 778 B/op 28 allocs/op
BenchmarkV3Encode 1238996 1003 ns/op 545 B/op 5 allocs/op
BenchmarkV3int64 1504563 787 ns/op 412 B/op 11 allocs/op
BenchmarkV2 446307 2431 ns/op 1645 B/op 26 allocs/op
BenchmarkV2Decode 976438 1254 ns/op 776 B/op 22 allocs/op
BenchmarkV2Encode 943730 1184 ns/op 751 B/op 4 allocs/op
BenchmarkGobMap 91776 12232 ns/op 1916 B/op 68 allocs/op
BenchmarkGobMapDecode 165802 6618 ns/op 1260 B/op 48 allocs/op
BenchmarkGobMapEncode 237160 5112 ns/op 656 B/op 20 allocs/op```
## notes
- all tests are run using `GOMAXPROCS=1`, this is because on zen running on multiple threads will cause horrible cache-invalidation. A single alloc/op would cause the GC to run at some point, this would kick the benching to a diferent core. The reason I decided to run using `GOMAXPROCS=1` is because this doesnt have a big impact on Intel cpus, and any real world application would be generating garbage anyways, so eleminitin the GC from running should be part of the benchmark. Another reason coul be: real world applications would so something else in between runs causing cache-invalidation anyways.
Expand Down
Binary file added cpu.out
Binary file not shown.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/jaicewizard/tt

go 1.12

require github.com/go-test/deep v1.0.6
require (
github.com/go-test/deep v1.0.6
github.com/google/gofuzz v1.2.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/go-test/deep v1.0.5 h1:AKODKU3pDH1RzZzm6YZu77YWtEAq6uh1rLIAQlay2qc=
github.com/go-test/deep v1.0.5/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/go-test/deep v1.0.6 h1:UHSEyLZUwX9Qoi99vVwvewiMC8mM2bf7XEM2nqvzEn8=
github.com/go-test/deep v1.0.6/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ alloc:
env GODEBUG=allocfreetrace=1 ./tt.test -test.run=none -test.bench=BenchmarkV3$$ -test.benchtime=10ms 2>trace.log

bench:
go test -bench=. -benchtime=10s -benchmem
go test -bench=. -run=^\$ -benchtime=10s -benchmem

shortbench:
go test -bench=. -benchtime=1s -benchmem
go test -bench=. -run=^\$ -benchtime=1s -benchmem
90 changes: 86 additions & 4 deletions tt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tt
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/gob"
"encoding/json"
"fmt"
Expand All @@ -15,6 +16,7 @@ import (
"time"

"github.com/go-test/deep"
fuzz "github.com/google/gofuzz"
v1 "github.com/jaicewizard/tt/v1"
)

Expand Down Expand Up @@ -586,7 +588,10 @@ var testCases = []testCase{

func testStructDecode(t *testing.T, testcase testCase) {
buf := &bytes.Buffer{}
Encodev3(testcase.data, buf)
err := Encodev3(testcase.data, buf)
if err != nil {
t.Error(err)
}
equal := false
bytes := []byte{}
var diff []string
Expand All @@ -602,7 +607,7 @@ func testStructDecode(t *testing.T, testcase testCase) {
t.Error(diff)
}
after := reflect.New(reflect.TypeOf(testcase.data)).Elem()
err := Decodev3(buf, after.Addr().Interface())
err = Decodev3(buf, after.Addr().Interface())
if err != nil {
t.Error(err)
}
Expand All @@ -622,6 +627,83 @@ func TestEncodeDecode(t *testing.T) {
}
}

type testStructEmbeded struct {
Name string `TT:"hi"`
type FuzzStruct struct {
M map[int32]Point
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Uint uint
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Int int
Float32 float32
Float64 float64
Map2 map[string]Point
}
type Point struct {
X float32
Y float64
}

func testFuz(t *testing.T, testcase FuzzStruct) {
buf := &bytes.Buffer{}
err := Encodev3(testcase, buf)
if err != nil {
t.Error(err)
}
after := reflect.New(reflect.TypeOf(testcase)).Elem()
err = Decodev3(buf, after.Addr().Interface())
if err != nil {
t.Error(err)
}
//this only tests public fields
if diff := deep.Equal(testcase, after.Interface()); diff != nil {
t.Error(testcase)
t.Error(after.Interface())
t.Error(diff)
}
}

//{map[-1849047553:{0.70218486 0.5657049046289432} -1704443557:{0.2711049 0.1320528724201477} -579969259:{0.41055316 0.6793484076806898} -324059402:{0.2030408 0.24218549972596393} 109147779:{0.4525726 0.32443739229246954} 644540783:{0.17598473 0.6027663378651792} 1387980123:{0.53488904 0.8896629879214714} 1466347468:{0.6818829 0.03911422469086078}] 36 18963 847877453 10861296638687174198 16814441198678319341 -120 9841 1125880010 -4082773609634810997 5510737390671974350 0.87748927 0.9334683660418484 map[]}
//{map[-1849047553:{0.70218486 0.5657049046289432} -1704443557:{0.2711049 0.1320528724201477} -579969259:{0.41055316 0.6793484076806898} -324059402:{0.2030408 0.24218549972596393} 109147779:{0.4525726 0.32443739229246954} 644540783:{0.17598473 0.6027663378651792} 1387980123:{0.53488904 0.8896629879214714} 1466347468:{0.6818829 0.03911422469086078}] 36 18963 847877453 10861296638687174198 16814441198678319341 -120 9841 1125880010 -4082773609634810997 5510737390671974350 0.87748927 0.9334683660418484 map[]}
func TestEncodeDecodeFuzz(t *testing.T) {
data := make([]byte, 1000000)
s := FuzzStruct{}
for i := 0; i < 50000; i++ {
t.Run(fmt.Sprintf("%d", i), func(te *testing.T) {
rand.Read(data)
fuzz.NewFromGoFuzz(data).Fuzz(&s)
if i%1 == 0 {
fmt.Printf("%d\n", i)
}
testFuz(t, s)
if i%1 == 0 {
fmt.Printf("%d\n", i)
}
buf := bytes.NewBuffer(data[:10000])
after := map[interface{}]interface{}{}
err := Decodev3(buf, &after)
if err != nil {
t.Error(err)
}

})
}
}

func TestDecodeFuzz(t *testing.T) {
data := make([]byte, 1000000)
s := FuzzStruct{}
for i := 0; i < 50000; i++ {
t.Run(fmt.Sprintf("%d", i), func(te *testing.T) {
rand.Read(data)
if i%10000 == 0 {
fmt.Printf("%d\n", i)
}
testFuz(t, s)
})
}
}
Loading

0 comments on commit fecf48b

Please sign in to comment.