Skip to content

Commit

Permalink
make alloc zero alloc test more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Feb 1, 2025
1 parent d91d704 commit 7d7b74c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ memprofile:
noallocsprofile:
rm mem.out || true
rm profile*.* || true
go test -test.v -test.run=TestNotASingleAllocAfterWarmUp -test.memprofile=mem.out ./json
(go test -test.v -test.run=TestNotASingleAllocAfterWarmUp -test.memprofile=mem.out ./json || true)
(cd json && go test -c -gcflags="-m" 2>&1 | grep "escapes" | grep -Ev "(_test.go|_testmain.go|autogenerated)")
go tool pprof -alloc_space -png mem.out

Expand Down
24 changes: 13 additions & 11 deletions json/alloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,30 @@ func TestNotASingleAllocAfterWarmUp(t *testing.T) {
// warm up buffer pool
for i := 0; i < num; i++ {
if err := jparser.Init(js[i%num]); err != nil {
t.Fatalf("seed = %v, err = %v", seed, err)
t.Fatalf("seed = %v, err = %v value = %v", seed, err, string(js[i%num]))
}
walk(jparser)
}
originalPoolSize := pool.Size()

const runsPerTest = 1
checkNoAllocs := func(f func()) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
if allocs := testing.AllocsPerRun(runsPerTest, f); allocs != 0 {
t.Errorf("seed = %v, got %v allocs, want 0 allocs, pool allocs = %v", seed, allocs, pool.Size()-originalPoolSize)
}
}
}
for i := 0; i < num; i++ {
t.Run(fmt.Sprintf("%d", i), checkNoAllocs(func() {
f := func() {
if err := jparser.Init(js[i]); err != nil {
t.Fatalf("seed = %v, err = %v", seed, err)
}
walk(jparser)
}))
}
allocs := testing.AllocsPerRun(runsPerTest, f)
if allocs != 0 {
poolallocs := pool.Size() - originalPoolSize
// there are sometimes allocations made by the testing framework
// retry to make sure that the allocation is the parser's fault.
allocs2 := testing.AllocsPerRun(runsPerTest, f)
if allocs2 != 0 {
t.Errorf("seed = %v, got %v allocs, want 0 allocs, pool allocs = %v", seed, allocs, poolallocs)
}
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions json/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

func TestParseRandom(t *testing.T) {
seed := time.Now().UnixNano()
seed = 1738425704510828000 // TODO remove this line
num := 10000
r := rand.New(rand.NewSource(seed))
js := randJsons(r, num)
Expand Down Expand Up @@ -264,13 +263,13 @@ func randWs(r *rand.Rand) string {
func randW(r *rand.Rand) rune {
switch r.Intn(4) {
case 0:
return '\u0020'
return '\u0020' // space
case 1:
return '\u000A'
return '\u000A' // \n
case 2:
return '\u000D'
return '\u000D' // \r
case 3:
return '\u0009'
return '\u0009' // \t
}
panic("unreachable")
}

0 comments on commit 7d7b74c

Please sign in to comment.