Skip to content

Commit

Permalink
added a allocation limit
Browse files Browse the repository at this point in the history
  • Loading branch information
JAicewizard committed Dec 27, 2020
1 parent fecf48b commit e95c13c
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 77 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ Data is are benchmarks that use Data, map benchmarks just convert the data to a

r5 3600 make bench
```
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```
BenchmarkV3 303894 3921 ns/op 1337 B/op 33 allocs/op
BenchmarkV3Decode 401946 2827 ns/op 778 B/op 28 allocs/op
BenchmarkV3Encode 1208140 1049 ns/op 558 B/op 5 allocs/op
BenchmarkV3int64 1657701 730 ns/op 572 B/op 11 allocs/op
BenchmarkV2 450891 2442 ns/op 1615 B/op 26 allocs/op
BenchmarkV2Decode 914095 1216 ns/op 776 B/op 22 allocs/op
BenchmarkV2Encode 962227 1174 ns/op 791 B/op 4 allocs/op
BenchmarkGobMap 99562 11806 ns/op 1916 B/op 68 allocs/op
BenchmarkGobMapDecode 183130 6511 ns/op 1260 B/op 48 allocs/op
BenchmarkGobMapEncode 235747 4983 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
7 changes: 5 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ alloc:
env GODEBUG=allocfreetrace=1 ./tt.test -test.run=none -test.bench=BenchmarkV3$$ -test.benchtime=10ms 2>trace.log

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

shortbench:
go test -bench=. -run=^\$ -benchtime=1s -benchmem
env GOMAXPROCS=1 go test -bench=. -run=^\$ -benchtime=1s -benchmem

test:
go test -short
37 changes: 19 additions & 18 deletions tt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,50 +660,51 @@ func testFuz(t *testing.T, testcase FuzzStruct) {
t.Error(err)
}
//this only tests public fields
deep.NilMapsAreEmpty = true
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) {
if testing.Short() {
t.Skip("Skipping long-running test.")
}
data := make([]byte, 1000000)
s := FuzzStruct{}
for i := 0; i < 50000; i++ {
for i := 0; i < 10000; i++ {
t.Run(fmt.Sprintf("%d", i), func(te *testing.T) {
rand.Read(data)
fuzz.NewFromGoFuzz(data).Fuzz(&s)
if i%1 == 0 {
if i%100 == 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{}
if testing.Short() {
t.Skip("Skipping long-running test.")
}
data := make([]byte, 10000)
for i := 0; i < 50000; i++ {
t.Run(fmt.Sprintf("%d", i), func(te *testing.T) {
rand.Read(data)
if i%10000 == 0 {
if i%500 == 0 {
fmt.Printf("%d\n", i)
}
testFuz(t, s)
buf := bytes.NewBuffer(data[:10000])
after := interface{}(nil)
dec := NewV3Decoder(buf, true)
dec.SetAllocLimmit(2 << 30) //1 GiB
err := dec.Decode(&after)
if err != nil {
t.Log(err)
}
})
}
}
Loading

0 comments on commit e95c13c

Please sign in to comment.