go-twig/benchmark
semihalev f1add2d820 Implement efficient LRU eviction strategy for attribute cache
- Added tracking of last access time and access count to cache entries
- Implemented eviction policy to remove least recently/frequently used entries
- Cache now removes 10% of entries when the cache is full, prioritizing by usage
- Added benchmarks and tests to verify eviction strategy
- Fixed the previously ineffective eviction strategy
- Improved cache efficiency for applications with many diverse object types

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-11 14:20:47 +03:00
..
BENCHMARK_RESULTS.md Add serialization benchmark and update results 2025-03-11 13:58:31 +03:00
complex_comparison.go Apply gofmt formatting to improve code style 2025-03-10 11:01:47 +03:00
engine_comparison.go Add comprehensive benchmarks comparing Twig with other template engines 2025-03-10 10:32:12 +03:00
go.mod Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
go.sum Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
macro_benchmark.go Implement efficient LRU eviction strategy for attribute cache 2025-03-11 14:20:47 +03:00
memory_benchmark.go Implement efficient LRU eviction strategy for attribute cache 2025-03-11 14:20:47 +03:00
MEMORY_RESULTS.md Optimize string to byte conversions during rendering 2025-03-11 14:16:29 +03:00
README.md Add macro benchmark tests and update documentation 2025-03-11 13:29:17 +03:00
serialization_benchmark.go Implement efficient LRU eviction strategy for attribute cache 2025-03-11 14:20:47 +03:00
serialization_main.go Implement efficient LRU eviction strategy for attribute cache 2025-03-11 14:20:47 +03:00
simple_benchmark.go Apply gofmt formatting to improve code style 2025-03-10 11:01:47 +03:00

Twig Template Engine Benchmarks

This directory contains benchmark tests for comparing the Twig template engine with other popular Go template engines.

Available Benchmarks

  1. simple_benchmark.go: Basic benchmark comparing Twig with Go's standard html/template.
  2. complex_comparison.go: Comprehensive benchmark with various template complexities comparing multiple engines.
  3. engine_comparison.go: Simple comparison of multiple template engines.
  4. macro_benchmark.go: Special benchmark for testing macro functionality performance.
  5. memory_benchmark.go: Detailed memory usage comparison between Twig and Go's template.

Running the Benchmarks

To run the benchmarks:

# Basic benchmark
go run simple_benchmark.go

# Comprehensive comparison of multiple engines
go run complex_comparison.go 

# Simple engine comparison
go run engine_comparison.go

# Macro performance benchmark
go run macro_benchmark.go

# Memory usage benchmark
go run memory_benchmark.go

Benchmark Methodology

These benchmarks measure:

  1. Rendering speed: Operations per second and time per operation
  2. Memory usage: Bytes allocated per operation
  3. Allocation count: Number of memory allocations per operation

We test with various template complexities:

  • Simple templates: Basic variable substitution
  • Medium templates: Conditionals and object properties
  • Complex templates: Loops, nested conditionals, and filters
  • Macro templates: Testing macro definition, calling, and importing

Key Findings

  1. Twig consistently outperforms other interpreted template engines
  2. Twig's performance advantage increases with template complexity
  3. Twig is significantly more memory-efficient than other engines
  4. Imported macros perform slightly better than direct macros
  5. Twig is up to 2.5x faster than Go's templates for complex templates

For detailed results, see the BENCHMARK_RESULTS.md file.

Sample Results

Results will vary based on your system, but here's a sample of what to expect:

===================================================
           Template Engine Benchmark
===================================================
Go version: go1.24.1
CPU: 8 cores
GOMAXPROCS: 8
Date: 2025-03-10 15:04:05
===================================================

Running benchmarks...

BenchmarkTwig/simple:
  Operations: 500000
  Time per op: 2.5µs
  Bytes per op: 128
  Allocs per op: 2

BenchmarkGoTemplate/simple:
  Operations: 1000000
  Time per op: 1.2µs
  Bytes per op: 64
  Allocs per op: 1

...and so on for other engines and template complexities

Notes

  • The standard Go template engine is generally fastest for simple templates
  • Twig provides better syntax and features for complex templates
  • Memory usage tends to increase with template complexity
  • The benchmarks include compilation time, which affects initial rendering
  • For production use, template caching significantly improves performance