-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathwork.go
42 lines (39 loc) · 922 Bytes
/
work.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package example
import (
"math/rand"
"reflect"
"runtime"
"strconv"
"time"
)
// Wwork loops forever, generating allocations of various sizes, in order to
// create artificial work for a nice 'demo effect'.
func Work() {
m := make(map[int64]any)
tick := time.NewTicker(30 * time.Millisecond)
clearTick := time.NewTicker(1 * time.Second)
for {
select {
case <-clearTick.C:
m = make(map[int64]any)
if rand.Intn(100) < 5 {
runtime.GC()
}
case ts := <-tick.C:
m[ts.UnixNano()] = newStruct()
}
}
}
// create a randomly sized struct (to create 'motion' on size classes plot).
func newStruct() any {
nfields := rand.Intn(32)
var fields []reflect.StructField
for i := 0; i < nfields; i++ {
fields = append(fields, reflect.StructField{
Name: "f" + strconv.Itoa(i),
PkgPath: "main",
Type: reflect.TypeOf(""),
})
}
return reflect.New(reflect.StructOf(fields)).Interface()
}