getCacheStats method
Gets cache statistics for monitoring and optimization.
Returns:
- ClassLoaderStats containing cache metrics
Available Metrics
- Hit Rate: Percentage of cache hits vs misses
- Cache Sizes: Number of entries in each cache category
- Memory Usage: Estimated memory consumption
- Load Times: Average class loading performance
Example
final stats = loader.getCacheStats();
print('Cache hit rate: ${stats.hitRate}%');
print('Classes cached: ${stats.classCount}');
print('Memory usage: ${stats.memoryUsage} bytes');
Monitoring Use Cases
- Performance tuning and optimization
- Memory usage tracking
- Cache efficiency analysis
- Capacity planning
Implementation
@override
ClassLoaderStats getCacheStats() {
final totalRequests = _hitCount + _missCount;
final hitRate = totalRequests > 0 ? _hitCount / totalRequests : 0.0;
final avgLoadTime = _loadTimes.isNotEmpty
? _loadTimes.reduce((a, b) => a + b) / _loadTimes.length
: 0.0;
// Estimate memory usage (rough calculation)
final memoryUsage = (_classCache.length * 1024) + // ~1KB per class
(_subclassCache.length * 512) + // ~512B per subclass list
(_interfaceCache.length * 256) + // ~256B per interface list
(_mixinCache.length * 256); // ~256B per mixin list
return ClassLoaderStats(
classCount: _classCache.length,
subclassCount: _subclassCache.length,
interfaceCount: _interfaceCache.length,
declaredInterfaceCount: 0,
mixinCount: _mixinCache.length,
declaredMixinCount: 0,
hitRate: hitRate,
hitCount: _hitCount,
missCount: _missCount,
memoryUsage: memoryUsage,
averageLoadTime: avgLoadTime,
successfulLoads: _successfulLoads,
failedLoads: _failedLoads,
);
}