diff --git a/.gitignore b/.gitignore index 31370fb..21974d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea data plot.html +.DS_Store diff --git a/generate/main.go b/generate/main.go index 43dfa10..93a95e8 100644 --- a/generate/main.go +++ b/generate/main.go @@ -15,9 +15,13 @@ func main() { // Setup the value we want to retrieve in each iteration ctx := context.WithValue(context.Background(), key, "some-val") - 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) { @@ -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 { // shadow the context, each iteration creates a new one and it doesn't grow ctx := contextWithRandom(ctx) @@ -41,10 +45,33 @@ func shadow(ctx context.Context, times uint64) { start := time.Now() _ = 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 { 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 + } +} diff --git a/plot/main.go b/plot/main.go index 9487eb8..15a0c9d 100644 --- a/plot/main.go +++ b/plot/main.go @@ -11,9 +11,9 @@ import ( ) func main() { - shadow, fat := getData() + thin, fat, experiment := getData() plot := charts.NewLine() - xAxis := make([]int, len(shadow)) + xAxis := make([]int, len(thin)) for i := range xAxis { xAxis[i] = i } @@ -25,8 +25,9 @@ func main() { plot. SetXAxis(xAxis). - AddSeries("Shadow", shadow). - AddSeries("Fat", fat) + AddSeries("Thin", thin). + AddSeries("Fat", fat). + AddSeries("Experiment", experiment) f, err := os.Create("plot.html") if err != nil { @@ -38,9 +39,10 @@ func main() { } } -func getData() ([]opts.LineData, []opts.LineData) { - var shadow []opts.LineData +func getData() ([]opts.LineData, []opts.LineData, []opts.LineData) { + var thin []opts.LineData var fat []opts.LineData + var experiment []opts.LineData f, err := os.Open("data") if err != nil { @@ -66,12 +68,14 @@ func getData() ([]opts.LineData, []opts.LineData) { point := opts.LineData{Value: val, Name: "ns"} switch series { - case "shadow": - shadow = append(shadow, point) + case "thin": + thin = append(thin, point) case "fat": fat = append(fat, point) + case "experiment": + experiment = append(experiment, point) } } - return shadow, fat + return thin, fat, experiment }