Skip to content

Commit

Permalink
add experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Crocmagnon committed Jan 13, 2025
1 parent 000a907 commit 7c777c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
data
plot.html
.DS_Store
35 changes: 31 additions & 4 deletions generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -33,18 +37,41 @@ 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)

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
}
}
22 changes: 13 additions & 9 deletions plot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit 7c777c5

Please # to comment.