Skip to content

Commit

Permalink
_example: make Work a bit more erratic
Browse files Browse the repository at this point in the history
  • Loading branch information
arl committed Feb 22, 2022
1 parent cc1cf3a commit e3c0acf
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions _example/work.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
package example

import (
"context"
"fmt"
"math/rand"
"time"
)

// Work loops forever, generating a bunch of allocations of various sizes in
// order to force the garbage collector to work.
// Work loops forever, generating a bunch of allocations of various sizes, plus
// some goroutines, in order to force the garbage collector to work.
func Work() {
work(context.Background(), 0)
}

func work(ctx context.Context, lvl int) {
println("work", lvl)
m := make(map[int]interface{})
tick := time.NewTicker(10 * time.Millisecond)

for i := 0; ; i++ {
select {
case <-ctx.Done():
return
case <-tick.C:
break
}

var obj interface{}
switch i % 6 {

switch rand.Intn(10) % 10 {
case 0:
obj = &struct {
_ uint32
Expand All @@ -26,10 +42,20 @@ func Work() {
obj = fmt.Sprintf("a relatively long and useless string %d", i)
case 3:
obj = make([]byte, i%1024)
case 4:
case 4, 5:
obj = make([]byte, 10*i%1024)
case 5:
case 6, 7:
obj = make([]string, 512)
case 8:
obj = make([]byte, 16*1024)
case 9:
if lvl > 2 {
break
}
// Sometimes start another goroutine, just because...
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
work(ctx, lvl+1)
}

if i == 1000 {
Expand All @@ -38,6 +64,5 @@ func Work() {
}

m[i] = obj
time.Sleep(10 * time.Millisecond)
}
}

0 comments on commit e3c0acf

Please sign in to comment.