This directory contains comprehensive benchmarks for HOT cache performance across different scenarios and configurations.
HOT is designed for high-performance scenarios with the following characteristics:
- Zero-allocation operations where possible
- Lock-free operations when thread safety is disabled
- Batch operations for better throughput
- Sharded architecture for high concurrency
- Monotonic clock lookup (2.5x faster)
- Set/Get Performance: Single key-value operations
- Batch Operations: Multiple key-value operations
- Memory Usage: Memory footprint analysis
- Concurrent Access: Multi-threaded performance
- LRU Performance: Least Recently Used algorithm
- LFU Performance: Least Frequently Used algorithm
- ARC Performance: Adaptive Replacement Cache
- 2Q Performance: Two Queue algorithm
- FIFO Performance: First In, First Out algorithm
- TTL Performance: Time-to-live overhead
- Sharding Performance: Multi-shard scalability
- Metrics Overhead: Prometheus metrics impact
- Loader Performance: Database integration overhead
- vs Standard Maps: Go built-in map performance
- vs Redis: Redis performance comparison
- vs Other Go Caches: Popular Go caching libraries
# Install benchmark dependencies
go get golang.org/x/perf/cmd/benchstat
go get github.com/cespare/prettybench# Run all benchmarks
make bench# Compare benchmark results
benchstat old.txt new.txt
# Pretty print benchmark results
prettybench -benchmem ./...- Set: ? per operation
- Get: ? per operation
- Delete: ? per operation
- SetMany: ? per item
- GetMany: ? per item
- DeleteMany: ? per item
- Per Item: ? bytes overhead
- Cache Structure: ? base overhead
- Sharding: ? per shard
- Single-threaded: ? ops/sec
- Multi-threaded (4 cores): ? ops/sec
- Sharded (16 shards): ? ops/sec
// For general use
cache := hot.NewHotCache[string, int](hot.LRU, 1000)
// For frequently accessed data
cache := hot.NewHotCache[string, int](hot.LFU, 1000)
// For mixed access patterns
cache := hot.NewHotCache[string, int](hot.ARC, 1000)
// For simple, predictable eviction
cache := hot.NewHotCache[string, int](hot.FIFO, 1000)// ❌ Slow: Individual operations
for _, key := range keys {
cache.Set(key, value)
}
// ✅ Fast: Batch operations
cache.SetMany(items)// For maximum performance
cache := hot.NewHotCache[string, int](hot.LRU, 1_000).
WithoutLocking(). // Single-threaded only
Build()// For high concurrency
cache := hot.NewHotCache[string, int](hot.LRU, 1_000).
WithSharding(16, hasher).
Build()// TODO
// TODO
When adding new features, please include appropriate benchmarks:
- Add benchmark functions in the relevant package
- Update this README with new performance data
- Run benchmarks before and after changes
- Document performance impact in pull requests
func BenchmarkNewFeature(b *testing.B) {
cache := hot.NewHotCache[string, int](hot.LRU, 1000).
WithNewFeature().
Build()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
key := fmt.Sprintf("key:%d", i%1000)
cache.Set(key, i)
cache.Get(key)
i++
}
})
}# Generate CPU profile
go test -bench=BenchmarkLRU -cpuprofile=cpu.prof ./pkg/lru/
# Analyze with pprof
go tool pprof cpu.prof# Generate memory profile
go test -bench=BenchmarkLRU -memprofile=mem.prof ./pkg/lru/
# Analyze with pprof
go tool pprof mem.prof# Generate flame graph
go test -bench=BenchmarkLRU -cpuprofile=cpu.prof ./pkg/lru/
go tool pprof -http=:8080 cpu.prof# Disable CPU frequency scaling
export GOMAXPROCS=1
export GOGC=off
# Run benchmarks
make bench# Linux: Disable CPU frequency scaling
echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# macOS: Disable App Nap
defaults write NSGlobalDomain NSAppSleepDisabled -bool trueFor performance-related questions or issues:
- 📖 Documentation: https://github.com/samber/hot
- 📧 Twitter: https://twitter.com/samuelberthe
- 🐛 Issues: https://github.com/samber/hot/issues