|
| 1 | +"""Benchmarks for merge_row_batch and batch_items. |
| 2 | +
|
| 3 | +merge_row_batch is called before every flush to the Braintrust API to |
| 4 | +de-duplicate and merge rows in a pending batch. batch_items is used to |
| 5 | +split the resulting rows into API-request-sized chunks. |
| 6 | +
|
| 7 | +Both functions mutate their inputs, so each benchmark wrapper builds fresh |
| 8 | +row lists per iteration. |
| 9 | +""" |
| 10 | + |
| 11 | +import pathlib |
| 12 | +import sys |
| 13 | + |
| 14 | +import pyperf |
| 15 | + |
| 16 | + |
| 17 | +if __package__ in (None, ""): |
| 18 | + sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[2])) |
| 19 | + |
| 20 | +from braintrust.db_fields import IS_MERGE_FIELD |
| 21 | +from braintrust.merge_row_batch import batch_items, merge_row_batch |
| 22 | + |
| 23 | +from benchmarks._utils import disable_pyperf_psutil |
| 24 | + |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | +# Row factories — called inside each benchmark wrapper to get fresh dicts. |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | + |
| 30 | + |
| 31 | +def _unique_rows(n: int) -> list[dict]: |
| 32 | + """n rows, all distinct IDs — no merging needed.""" |
| 33 | + return [{"id": f"row-{i}", "project_id": "proj-1", "value": i} for i in range(n)] |
| 34 | + |
| 35 | + |
| 36 | +def _merge_rows(n: int) -> list[dict]: |
| 37 | + """n rows forming n//2 pairs: first is a base, second is an IS_MERGE update.""" |
| 38 | + rows = [] |
| 39 | + for i in range(n // 2): |
| 40 | + rows.append({"id": f"row-{i}", "project_id": "proj-1", "payload": {"a": i}}) |
| 41 | + rows.append( |
| 42 | + { |
| 43 | + "id": f"row-{i}", |
| 44 | + "project_id": "proj-1", |
| 45 | + "payload": {"b": i + 100}, |
| 46 | + IS_MERGE_FIELD: True, |
| 47 | + } |
| 48 | + ) |
| 49 | + return rows |
| 50 | + |
| 51 | + |
| 52 | +def _mixed_rows(n: int) -> list[dict]: |
| 53 | + """Mix of unique rows and merge pairs (roughly half each).""" |
| 54 | + rows = [] |
| 55 | + for i in range(n // 4): |
| 56 | + # pair that will be merged |
| 57 | + rows.append({"id": f"merge-{i}", "project_id": "proj-1", "payload": {"a": i}}) |
| 58 | + rows.append( |
| 59 | + { |
| 60 | + "id": f"merge-{i}", |
| 61 | + "project_id": "proj-1", |
| 62 | + "payload": {"b": i + 100}, |
| 63 | + IS_MERGE_FIELD: True, |
| 64 | + } |
| 65 | + ) |
| 66 | + for i in range(n // 2): |
| 67 | + rows.append({"id": f"unique-{i}", "project_id": "proj-1", "value": i}) |
| 68 | + return rows |
| 69 | + |
| 70 | + |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +# Benchmark wrappers |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | + |
| 75 | +_SMALL_N = 10 |
| 76 | +_MEDIUM_N = 50 |
| 77 | +_LARGE_N = 200 |
| 78 | + |
| 79 | + |
| 80 | +def _bench_no_conflict_small() -> None: |
| 81 | + merge_row_batch(_unique_rows(_SMALL_N)) |
| 82 | + |
| 83 | + |
| 84 | +def _bench_no_conflict_medium() -> None: |
| 85 | + merge_row_batch(_unique_rows(_MEDIUM_N)) |
| 86 | + |
| 87 | + |
| 88 | +def _bench_no_conflict_large() -> None: |
| 89 | + merge_row_batch(_unique_rows(_LARGE_N)) |
| 90 | + |
| 91 | + |
| 92 | +def _bench_all_merge_small() -> None: |
| 93 | + merge_row_batch(_merge_rows(_SMALL_N)) |
| 94 | + |
| 95 | + |
| 96 | +def _bench_all_merge_medium() -> None: |
| 97 | + merge_row_batch(_merge_rows(_MEDIUM_N)) |
| 98 | + |
| 99 | + |
| 100 | +def _bench_mixed_medium() -> None: |
| 101 | + merge_row_batch(_mixed_rows(_MEDIUM_N)) |
| 102 | + |
| 103 | + |
| 104 | +# batch_items: split a list of strings by item-count and byte-count limits. |
| 105 | +_BATCH_STRINGS = [f"item-payload-{i:04d}" * 4 for i in range(200)] |
| 106 | +_ITEM_SIZE = len(_BATCH_STRINGS[0].encode()) |
| 107 | + |
| 108 | + |
| 109 | +def _bench_batch_items_count_limit() -> None: |
| 110 | + batch_items(_BATCH_STRINGS, batch_max_num_items=20) |
| 111 | + |
| 112 | + |
| 113 | +def _bench_batch_items_byte_limit() -> None: |
| 114 | + batch_items( |
| 115 | + _BATCH_STRINGS, |
| 116 | + batch_max_num_bytes=_ITEM_SIZE * 15, |
| 117 | + get_byte_size=lambda s: len(s.encode()), |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def _bench_batch_items_both_limits() -> None: |
| 122 | + batch_items( |
| 123 | + _BATCH_STRINGS, |
| 124 | + batch_max_num_items=20, |
| 125 | + batch_max_num_bytes=_ITEM_SIZE * 15, |
| 126 | + get_byte_size=lambda s: len(s.encode()), |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def main(runner: pyperf.Runner | None = None) -> None: |
| 131 | + if runner is None: |
| 132 | + disable_pyperf_psutil() |
| 133 | + runner = pyperf.Runner() |
| 134 | + |
| 135 | + runner.bench_func("merge_row_batch[no-conflict-small]", _bench_no_conflict_small) |
| 136 | + runner.bench_func("merge_row_batch[no-conflict-medium]", _bench_no_conflict_medium) |
| 137 | + runner.bench_func("merge_row_batch[no-conflict-large]", _bench_no_conflict_large) |
| 138 | + runner.bench_func("merge_row_batch[all-merge-small]", _bench_all_merge_small) |
| 139 | + runner.bench_func("merge_row_batch[all-merge-medium]", _bench_all_merge_medium) |
| 140 | + runner.bench_func("merge_row_batch[mixed-medium]", _bench_mixed_medium) |
| 141 | + |
| 142 | + runner.bench_func("batch_items[count-limit]", _bench_batch_items_count_limit) |
| 143 | + runner.bench_func("batch_items[byte-limit]", _bench_batch_items_byte_limit) |
| 144 | + runner.bench_func("batch_items[both-limits]", _bench_batch_items_both_limits) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + main() |
0 commit comments