-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathbench_policy.py
More file actions
168 lines (139 loc) · 5.31 KB
/
bench_policy.py
File metadata and controls
168 lines (139 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Benchmarks for policy evaluation."""
from __future__ import annotations
import tempfile
import time
from pathlib import Path
from typing import Any, Dict, List
from agent_os.policies.evaluator import PolicyEvaluator
from agent_os.policies.schema import (
PolicyAction,
PolicyCondition,
PolicyDefaults,
PolicyDocument,
PolicyOperator,
PolicyRule,
)
from agent_os.policies.shared import (
Condition,
SharedPolicyEvaluator,
SharedPolicyRule,
)
def _sync_timer(func, iterations: int = 10_000) -> Dict[str, Any]:
"""Run a synchronous function *iterations* times and return latency stats."""
latencies: List[float] = []
for _ in range(iterations):
start = time.perf_counter()
func()
latencies.append((time.perf_counter() - start) * 1_000)
latencies.sort()
total_seconds = sum(latencies) / 1_000
return {
"iterations": iterations,
"total_seconds": round(total_seconds, 4),
"ops_per_sec": round(iterations / total_seconds) if total_seconds > 0 else 0,
"p50_ms": round(latencies[len(latencies) // 2], 4),
"p95_ms": round(latencies[int(len(latencies) * 0.95)], 4),
"p99_ms": round(latencies[int(len(latencies) * 0.99)], 4),
}
def _make_policy_doc(num_rules: int) -> PolicyDocument:
"""Create a PolicyDocument with *num_rules* rules."""
rules = [
PolicyRule(
name=f"rule-{i}",
condition=PolicyCondition(
field="action",
operator=PolicyOperator.EQ,
value=f"action_{i}",
),
action=PolicyAction.DENY if i % 3 == 0 else PolicyAction.ALLOW,
priority=i,
)
for i in range(num_rules)
]
return PolicyDocument(
version="1.0",
name=f"bench-policy-{num_rules}",
rules=rules,
defaults=PolicyDefaults(action=PolicyAction.ALLOW),
)
def bench_single_rule_evaluation(iterations: int = 10_000) -> Dict[str, Any]:
"""Benchmark evaluating a single-rule policy."""
evaluator = PolicyEvaluator(policies=[_make_policy_doc(1)])
ctx = {"action": "action_0", "agent_id": "bench"}
return {"name": "Single Rule Evaluation", **_sync_timer(lambda: evaluator.evaluate(ctx), iterations)}
def bench_10_rule_policy(iterations: int = 10_000) -> Dict[str, Any]:
"""Benchmark evaluating a 10-rule policy."""
evaluator = PolicyEvaluator(policies=[_make_policy_doc(10)])
ctx = {"action": "action_9", "agent_id": "bench"}
return {"name": "Policy Evaluation (10 rules)", **_sync_timer(lambda: evaluator.evaluate(ctx), iterations)}
def bench_100_rule_policy(iterations: int = 10_000) -> Dict[str, Any]:
"""Benchmark evaluating a 100-rule policy."""
evaluator = PolicyEvaluator(policies=[_make_policy_doc(100)])
ctx = {"action": "action_99", "agent_id": "bench"}
return {"name": "Policy Evaluation (100 rules)", **_sync_timer(lambda: evaluator.evaluate(ctx), iterations)}
def bench_yaml_policy_load(iterations: int = 1_000) -> Dict[str, Any]:
"""Benchmark loading a policy from YAML."""
try:
import yaml
except ImportError:
return {"name": "YAML Policy Load", "skipped": True, "reason": "pyyaml not installed"}
doc = _make_policy_doc(10)
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
yaml.dump(
{
"version": doc.version,
"name": doc.name,
"rules": [
{
"name": r.name,
"condition": {
"field": r.condition.field,
"operator": r.condition.operator.value,
"value": r.condition.value,
},
"action": r.action.value,
"priority": r.priority,
}
for r in doc.rules
],
},
f,
)
yaml_path = f.name
def load() -> None:
PolicyDocument.from_yaml(yaml_path)
result = {"name": "YAML Policy Load", **_sync_timer(load, iterations)}
Path(yaml_path).unlink(missing_ok=True)
return result
def bench_shared_policy_evaluation(iterations: int = 10_000) -> Dict[str, Any]:
"""Benchmark SharedPolicyEvaluator cross-project evaluation."""
evaluator = SharedPolicyEvaluator()
rules = [
SharedPolicyRule(
id=f"shared-{i}",
action="deny" if i % 3 == 0 else "allow",
conditions=[Condition(field="agent_id", operator="eq", value=f"agent-{i}")],
priority=i,
)
for i in range(10)
]
ctx = {"agent_id": "agent-9", "action": "query"}
return {
"name": "SharedPolicy Cross-Project Eval",
**_sync_timer(lambda: evaluator.evaluate(ctx, rules), iterations),
}
def run_all() -> List[Dict[str, Any]]:
"""Run all policy benchmarks and return results."""
return [
bench_single_rule_evaluation(),
bench_10_rule_policy(),
bench_100_rule_policy(),
bench_yaml_policy_load(),
bench_shared_policy_evaluation(),
]
if __name__ == "__main__":
import json
for result in run_all():
print(json.dumps(result, indent=2))