Skip to content

Conversation

@jgowdy-godaddy
Copy link
Contributor

Summary

This PR implements comprehensive performance benchmarks for Asherah's critical hot paths with detailed memory allocation tracking. The benchmarks are designed to detect performance regressions and guide optimization efforts for production deployments.

Hot Path Benchmarks Added

  • SessionFactory.GetSession: Both cached and uncached scenarios
  • Session.Encrypt/Decrypt: Core operations with 1KB realistic payloads
  • Round-trip operations: Full encrypt-decrypt cycles
  • Key cache operations: GetOrLoad and GetOrLoadLatest performance
  • Reference counting: Cached crypto key lifecycle operations
  • Memory pressure testing: Large payload operations (64KB)

Concurrent Benchmarks Added

  • Concurrent encryption/decryption: Performance under load
  • Concurrent session creation: SessionFactory scalability
  • Concurrent key cache access: Both same-key and unique-key scenarios
  • Mixed operations: Real-world encrypt/decrypt patterns
  • Session cache behavior: Cache performance under concurrent access
  • Reference counting concurrency: Thread-safe reference management

Key Features

Allocation tracking: All benchmarks use b.ReportAllocs() for detailed memory profiling
Realistic workloads: 1KB standard payloads, 64KB for memory pressure testing
Import cycle safe: Custom minimal mock implementations avoid dependency issues
Production patterns: Benchmarks mirror real-world usage scenarios
Concurrent testing: Both single-threaded and multi-threaded workloads

Sample Results

BenchmarkSessionFactory_GetSession_HotPath-16      6408549    180.3 ns/op    472 B/op   10 allocs/op
BenchmarkSessionFactory_GetSession_Cached-16      53285770     22.46 ns/op      0 B/op    0 allocs/op
BenchmarkSession_Encrypt_HotPath-16                  25484     46967 ns/op   2210 B/op   27 allocs/op
BenchmarkSession_Decrypt_HotPath-16                1232919       988.1 ns/op    256 B/op    8 allocs/op
BenchmarkSession_EncryptDecrypt_RoundTrip-16         24415     49267 ns/op   3490 B/op   36 allocs/op
BenchmarkKeyCache_GetOrLoad_WithAllocation-16     17500576        67.21 ns/op     40 B/op    2 allocs/op

Testing

  • ✅ All existing tests pass
  • ✅ New benchmarks execute successfully
  • ✅ Memory allocation tracking confirmed working
  • ✅ Both sequential and concurrent benchmarks validated

Test plan

  • Run go test -bench=Benchmark -benchtime=1s to verify all benchmarks execute
  • Run go test ./... to ensure no regressions
  • Verify allocation tracking reports meaningful data
  • Test both hot path and concurrent benchmark suites

Generated with Claude Code

Copy link
Contributor

@aka-bo aka-bo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on the Session benchmarks! Those are valuable additions.

However, there's significant overlap with existing benchmarks. The project already has omprehensive key cache benchmarks in key_cache_benchmark_test.go that cover the same GetOrLoad/GetOrLoadLatest scenarios you're testing. The duplicated benchmarks should be removed.

Also, ReportAllocs() just enables allocation tracking by default (same as running existing benchmarks with -benchmem). Consider adding -benchmem to ./build/go/benchmark_test.sh instead - that would give all benchmarks allocation tracking without code changes.

The file naming should follow the project's established {thing-to-benchmark}_benchmark_test.go convention. Consider session_benchmark_test.go to match the existing pattern.

The .tool-versions file needs to be removed from the PR too.

jgowdy-godaddy and others added 6 commits October 8, 2025 09:36
This implements comprehensive performance benchmarks for Asherah's critical hot paths:

**Hot Path Benchmarks:**
- SessionFactory.GetSession (cached and uncached scenarios)
- Session.Encrypt/Decrypt operations with 1KB payloads
- Round-trip encrypt-decrypt operations
- Key cache operations (GetOrLoad, GetOrLoadLatest)
- Reference counting operations for cached crypto keys
- Large payload operations (64KB) for memory pressure testing

**Concurrent Benchmarks:**
- Concurrent encryption/decryption under load
- Concurrent session creation
- Concurrent key cache access (same key vs unique keys)
- Mixed encrypt/decrypt operations
- Session cache behavior under concurrent access
- Reference counting under concurrent access

**Key Features:**
- All benchmarks use b.ReportAllocs() for detailed memory allocation tracking
- Realistic payload sizes (1KB standard, 64KB for memory pressure)
- Minimal mock implementations to avoid import cycles
- Comprehensive coverage of production usage patterns
- Both single-threaded and concurrent workload scenarios

These benchmarks enable performance regression detection and optimization
guidance for the most critical code paths in production deployments.

Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove trailing whitespace
- Add missing newlines at end of files

Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Fix gci import ordering issues
- Fix gofumpt formatting issue with var declaration
- Fix unparam issue by marking unused testing.B parameter
The benchmark was sharing the same DataRowRecord across all goroutines,
causing race conditions when accessing internal byte slices. Fixed by
creating multiple copies of the encrypted data and using round-robin
selection to ensure thread safety.
@jgowdy-godaddy jgowdy-godaddy force-pushed the add/hot-path-benchmarks branch from e8e292e to 615b2f2 Compare October 8, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants