"""ASV benchmarks for secure template rendering. Measures the overhead introduced by security checks in the template rendering pipeline. """ from __future__ import annotations from cleveragents.templates.secure_renderer import ( SecureTemplateRenderer, TemplateConfig, render_template_secure, validate_template, ) class RenderBench: """Benchmark secure template rendering throughput.""" timeout = 60 def setup(self) -> None: self.renderer = SecureTemplateRenderer( config=TemplateConfig( allowed_keys=frozenset({"name", "role", "project"}), ), ) self.template = "Hello {name}, you are {role} on {project}" self.context = { "name": "Alice", "role": "developer", "project": "cleveragents", } def time_render_simple(self) -> None: """Benchmark rendering a simple template.""" self.renderer.render(self.template, self.context) def time_render_no_placeholders(self) -> None: """Benchmark rendering template with no placeholders.""" self.renderer.render("Static text with no vars", {}) def time_render_many_placeholders(self) -> None: """Benchmark rendering template with many placeholders.""" tpl = " ".join(f"{{k{i}}}" for i in range(50)) ctx = {f"k{i}": f"v{i}" for i in range(50)} renderer = SecureTemplateRenderer(config=TemplateConfig()) renderer.render(tpl, ctx) class ValidateBench: """Benchmark template validation throughput.""" timeout = 60 def setup(self) -> None: self.safe_template = "Hello {name}, role={role}" self.unsafe_template = "{obj.__class__.__init__.__globals__}" def time_validate_safe(self) -> None: """Benchmark validating a safe template.""" validate_template(self.safe_template) def time_validate_unsafe(self) -> None: """Benchmark validating an unsafe template.""" validate_template(self.unsafe_template) def time_validate_with_allowlist(self) -> None: """Benchmark validating with an allowlist.""" validate_template( self.safe_template, allowed_keys=frozenset({"name", "role"}), ) class ConvenienceFunctionBench: """Benchmark the convenience render_template_secure function.""" timeout = 60 def time_convenience_render(self) -> None: """Benchmark the convenience function.""" render_template_secure( "Hello {name}", {"name": "World"}, allowed_keys=frozenset({"name"}), )