Skip to content

Commit a78af74

Browse files
aepfliclaude
andauthored
perf(python): add O2 and O4 operator benchmarks (#98)
## Summary - Add **O2** benchmark: fractional operator with 8 weighted buckets (multi-variant experiment) - Add **O4** benchmark: semver range comparison using caret (`^`) operator - Update O1/O3 docstrings to reference their benchmark IDs for clarity Closes #88 ## Test plan - [x] `pytest benchmarks/ -v --benchmark-disable` passes for both new tests - [x] Existing benchmarks unaffected (32 passed, 4 pre-existing failures from missing `json_logic` module) - [x] `cargo fmt --check && cargo clippy -- -D warnings` passes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7638db9 commit a78af74

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

python/benchmarks/conftest.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,35 @@ def _build_flag_config():
101101
]
102102
},
103103
},
104-
# Semver flag
104+
# Fractional bucketing flag with 8 weighted buckets (O2)
105+
"fractional-8-flag": {
106+
"state": "ENABLED",
107+
"variants": {
108+
"v1": "v1",
109+
"v2": "v2",
110+
"v3": "v3",
111+
"v4": "v4",
112+
"v5": "v5",
113+
"v6": "v6",
114+
"v7": "v7",
115+
"v8": "v8",
116+
},
117+
"defaultVariant": "v1",
118+
"targeting": {
119+
"fractional": [
120+
{"var": "targetingKey"},
121+
["v1", 12],
122+
["v2", 13],
123+
["v3", 12],
124+
["v4", 13],
125+
["v5", 12],
126+
["v6", 13],
127+
["v7", 12],
128+
["v8", 13],
129+
]
130+
},
131+
},
132+
# Semver flag (equality, O3)
105133
"semver-flag": {
106134
"state": "ENABLED",
107135
"variants": {"new-ui": True, "old-ui": False},
@@ -114,6 +142,19 @@ def _build_flag_config():
114142
]
115143
},
116144
},
145+
# Semver range flag with caret operator (O4)
146+
"semver-range-flag": {
147+
"state": "ENABLED",
148+
"variants": {"on": True, "off": False},
149+
"defaultVariant": "off",
150+
"targeting": {
151+
"if": [
152+
{"sem_ver": [{"var": "version"}, "^", "1.2.0"]},
153+
"on",
154+
"off",
155+
]
156+
},
157+
},
117158
# starts_with flag
118159
"starts-with-flag": {
119160
"state": "ENABLED",

python/benchmarks/test_benchmarks.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class TestCustomOperatorBenchmarks:
124124
"""Benchmarks for flagd custom JSON Logic operators."""
125125

126126
def test_bench_fractional_operator(self, benchmark, evaluator):
127-
"""Fractional bucketing operator (MurmurHash3-based)."""
127+
"""Fractional bucketing operator with 3 buckets (O1)."""
128128
ctx = {"targetingKey": "user-abc-123"}
129129
result = benchmark(evaluator.evaluate_string, "fractional-flag", ctx, "fallback")
130130
assert result in [
@@ -133,12 +133,24 @@ def test_bench_fractional_operator(self, benchmark, evaluator):
133133
"treatment-b-experience",
134134
]
135135

136+
def test_bench_fractional_8_buckets(self, benchmark, evaluator):
137+
"""Fractional bucketing with 8 weighted buckets — multi-variant experiment (O2)."""
138+
ctx = {"targetingKey": "user-abc-123"}
139+
result = benchmark(evaluator.evaluate_string, "fractional-8-flag", ctx, "fallback")
140+
assert result in ["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"]
141+
136142
def test_bench_semver_operator(self, benchmark, evaluator):
137-
"""Semantic version comparison operator."""
143+
"""Semantic version equality comparison (O3)."""
138144
ctx = {"appVersion": "2.5.1"}
139145
result = benchmark(evaluator.evaluate_bool, "semver-flag", ctx, False)
140146
assert result is True
141147

148+
def test_bench_semver_range_operator(self, benchmark, evaluator):
149+
"""Semantic version range comparison with caret operator (O4)."""
150+
ctx = {"version": "1.5.3"}
151+
result = benchmark(evaluator.evaluate_bool, "semver-range-flag", ctx, False)
152+
assert result is True
153+
142154
def test_bench_starts_with_operator(self, benchmark, evaluator):
143155
"""String prefix matching operator."""
144156
ctx = {"email": "admin@example.com"}

0 commit comments

Comments
 (0)