BenchmarkProjectionTracking measures the scan the element and key projections perform. Unlike the other quantity expressions, whose cost is independent of the values, these are linear in the size of the container and run once per call that is charged through them.
(b *testing.B)
| 529 | // the other quantity expressions, whose cost is independent of the values, these are linear in the |
| 530 | // size of the container and run once per call that is charged through them. |
| 531 | func BenchmarkProjectionTracking(b *testing.B) { |
| 532 | adapter := types.DefaultTypeAdapter |
| 533 | for _, size := range []int{1, 10, 100, 1000} { |
| 534 | elems := make([]string, size) |
| 535 | entries := make(map[string]string, size) |
| 536 | for i := range size { |
| 537 | elems[i] = strings.Repeat("x", i%16) |
| 538 | entries[elems[i]+fmt.Sprint(i)] = elems[i] |
| 539 | } |
| 540 | listVal := adapter.NativeToValue(elems) |
| 541 | mapVal := adapter.NativeToValue(entries) |
| 542 | |
| 543 | benchmarks := []struct { |
| 544 | name string |
| 545 | expr QuantityExpr |
| 546 | ctx TrackContext |
| 547 | }{ |
| 548 | {name: "elem_of_list", expr: ArgElem(0), ctx: &testTrackContext{argVals: []ref.Val{listVal}}}, |
| 549 | {name: "elem_total_list", expr: ElemTotal(Arg(0)), ctx: &testTrackContext{argVals: []ref.Val{listVal}}}, |
| 550 | {name: "key_of_map", expr: ArgKey(0), ctx: &testTrackContext{argVals: []ref.Val{mapVal}}}, |
| 551 | // The baseline: a size read, which does not scan. |
| 552 | {name: "arg_size", expr: Arg(0), ctx: &testTrackContext{args: []uint64{uint64(size)}}}, |
| 553 | } |
| 554 | for _, bm := range benchmarks { |
| 555 | b.Run(fmt.Sprintf("%s/%d", bm.name, size), func(b *testing.B) { |
| 556 | for i := 0; i < b.N; i++ { |
| 557 | bm.expr.track(bm.ctx) |
| 558 | } |
| 559 | }) |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | type testTrackContext struct { |
| 565 | args []uint64 |