add experiment

This commit is contained in:
Gabriel Augendre 2025-01-13 14:51:40 +01:00
parent 000a90783c
commit 7c777c53a2
3 changed files with 45 additions and 13 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.idea .idea
data data
plot.html plot.html
.DS_Store

View file

@ -15,9 +15,13 @@ func main() {
// Setup the value we want to retrieve in each iteration // Setup the value we want to retrieve in each iteration
ctx := context.WithValue(context.Background(), key, "some-val") ctx := context.WithValue(context.Background(), key, "some-val")
fat(ctx, times) fat(ctx, times)
shadow(ctx, times)
ctx = context.WithValue(context.Background(), key, "some-val")
thin(ctx, times)
ctx = context.WithValue(context.Background(), key, "some-val")
experiment(ctx, times)
} }
func fat(ctx context.Context, times uint64) { func fat(ctx context.Context, times uint64) {
@ -33,7 +37,7 @@ func fat(ctx context.Context, times uint64) {
} }
} }
func shadow(ctx context.Context, times uint64) { func thin(ctx context.Context, times uint64) {
for range times { for range times {
// shadow the context, each iteration creates a new one and it doesn't grow // shadow the context, each iteration creates a new one and it doesn't grow
ctx := contextWithRandom(ctx) ctx := contextWithRandom(ctx)
@ -41,10 +45,33 @@ func shadow(ctx context.Context, times uint64) {
start := time.Now() start := time.Now()
_ = ctx.Value(key) _ = ctx.Value(key)
fmt.Printf("shadow,%v\n", time.Since(start).Nanoseconds()) fmt.Printf("thin,%v\n", time.Since(start).Nanoseconds())
}
}
func experiment(ctx context.Context, times uint64) {
wrapper := something()
r := &R{ctx}
for range times {
wrapper(r)
start := time.Now()
_ = r.Ctx.Value(key)
fmt.Printf("experiment,%v\n", time.Since(start).Nanoseconds())
} }
} }
func contextWithRandom(ctx context.Context) context.Context { func contextWithRandom(ctx context.Context) context.Context {
return context.WithValue(ctx, "other_key", uuid.Must(uuid.NewV4())) return context.WithValue(ctx, "other_key", uuid.Must(uuid.NewV4()))
} }
type R struct {
Ctx context.Context
}
func something() func(*R) {
return func(r *R) {
ctx := r.Ctx
ctx = contextWithRandom(ctx)
r.Ctx = ctx // triggered on this line
}
}

View file

@ -11,9 +11,9 @@ import (
) )
func main() { func main() {
shadow, fat := getData() thin, fat, experiment := getData()
plot := charts.NewLine() plot := charts.NewLine()
xAxis := make([]int, len(shadow)) xAxis := make([]int, len(thin))
for i := range xAxis { for i := range xAxis {
xAxis[i] = i xAxis[i] = i
} }
@ -25,8 +25,9 @@ func main() {
plot. plot.
SetXAxis(xAxis). SetXAxis(xAxis).
AddSeries("Shadow", shadow). AddSeries("Thin", thin).
AddSeries("Fat", fat) AddSeries("Fat", fat).
AddSeries("Experiment", experiment)
f, err := os.Create("plot.html") f, err := os.Create("plot.html")
if err != nil { if err != nil {
@ -38,9 +39,10 @@ func main() {
} }
} }
func getData() ([]opts.LineData, []opts.LineData) { func getData() ([]opts.LineData, []opts.LineData, []opts.LineData) {
var shadow []opts.LineData var thin []opts.LineData
var fat []opts.LineData var fat []opts.LineData
var experiment []opts.LineData
f, err := os.Open("data") f, err := os.Open("data")
if err != nil { if err != nil {
@ -66,12 +68,14 @@ func getData() ([]opts.LineData, []opts.LineData) {
point := opts.LineData{Value: val, Name: "ns"} point := opts.LineData{Value: val, Name: "ns"}
switch series { switch series {
case "shadow": case "thin":
shadow = append(shadow, point) thin = append(thin, point)
case "fat": case "fat":
fat = append(fat, point) fat = append(fat, point)
case "experiment":
experiment = append(experiment, point)
} }
} }
return shadow, fat return thin, fat, experiment
} }