- Added comprehensive course structure with 11 modules - Included theoretical content, practical examples, and tasks - Multi-language code examples (Java, Rust, Node.js, Python) - Detailed analysis of architectural approaches and trade-offs - Complete caching strategies documentation - Architecture examples from real-world systems (Twitter, Netflix, Uber) - Structured navigation and learning path - Ready for educational use
170 lines
5.0 KiB
Markdown
170 lines
5.0 KiB
Markdown
# Профилирование и анализ
|
|
|
|
## Навигация
|
|
- [🏠 Главная страница курса](../../../README.md)
|
|
- [📚 Модуль 10](../README.md)
|
|
|
|
---
|
|
|
|
## Инструменты профилирования
|
|
|
|
### 1. JProfiler
|
|
```java
|
|
// Профилирование с JProfiler
|
|
@Service
|
|
public class ProfiledUserService {
|
|
@Autowired
|
|
private UserRepository userRepository;
|
|
|
|
public User getUser(Long id) {
|
|
// JProfiler покажет время выполнения этого метода
|
|
return userRepository.findById(id);
|
|
}
|
|
|
|
public List<User> searchUsers(String query) {
|
|
// Профилирование покажет узкие места
|
|
List<User> users = userRepository.findByNameContaining(query);
|
|
|
|
// Профилирование покажет время обработки
|
|
return users.stream()
|
|
.filter(user -> user.isActive())
|
|
.collect(Collectors.toList());
|
|
}
|
|
}
|
|
```
|
|
|
|
### 2. VisualVM
|
|
```java
|
|
// Мониторинг с VisualVM
|
|
@Component
|
|
public class PerformanceMonitor {
|
|
private final MemoryMXBean memoryBean;
|
|
private final ThreadMXBean threadBean;
|
|
|
|
public PerformanceMonitor() {
|
|
this.memoryBean = ManagementFactory.getMemoryMXBean();
|
|
this.threadBean = ManagementFactory.getThreadMXBean();
|
|
}
|
|
|
|
@Scheduled(fixedRate = 5000)
|
|
public void logPerformanceMetrics() {
|
|
// Мониторинг памяти
|
|
MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();
|
|
log.info("Heap usage: {} / {}", heapUsage.getUsed(), heapUsage.getMax());
|
|
|
|
// Мониторинг потоков
|
|
int threadCount = threadBean.getThreadCount();
|
|
log.info("Thread count: {}", threadCount);
|
|
|
|
// Мониторинг CPU
|
|
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
|
|
double cpuUsage = osBean.getProcessCpuLoad();
|
|
log.info("CPU usage: {}%", cpuUsage * 100);
|
|
}
|
|
}
|
|
```
|
|
|
|
### 3. Application Performance Monitoring (APM)
|
|
```java
|
|
// APM интеграция
|
|
@Service
|
|
public class APMUserService {
|
|
@Autowired
|
|
private NewRelic newRelic;
|
|
|
|
public User getUser(Long id) {
|
|
newRelic.addCustomParameter("user.id", id);
|
|
|
|
try {
|
|
User user = userRepository.findById(id);
|
|
newRelic.recordMetric("User/Found", 1);
|
|
return user;
|
|
} catch (Exception e) {
|
|
newRelic.recordMetric("User/Error", 1);
|
|
newRelic.recordException(e);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public List<User> searchUsers(String query) {
|
|
newRelic.addCustomParameter("search.query", query);
|
|
|
|
List<User> users = userRepository.findByNameContaining(query);
|
|
newRelic.recordMetric("User/SearchResults", users.size());
|
|
|
|
return users;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Анализ производительности
|
|
|
|
### 1. Bottleneck Analysis
|
|
```java
|
|
// Анализ узких мест
|
|
@Service
|
|
public class BottleneckAnalyzer {
|
|
private final Timer databaseTimer;
|
|
private final Timer cacheTimer;
|
|
private final Timer networkTimer;
|
|
|
|
public BottleneckAnalyzer(MeterRegistry meterRegistry) {
|
|
this.databaseTimer = Timer.builder("database.operation.duration")
|
|
.register(meterRegistry);
|
|
this.cacheTimer = Timer.builder("cache.operation.duration")
|
|
.register(meterRegistry);
|
|
this.networkTimer = Timer.builder("network.operation.duration")
|
|
.register(meterRegistry);
|
|
}
|
|
|
|
public User getUser(Long id) {
|
|
// Анализ времени выполнения каждой операции
|
|
User user = databaseTimer.recordCallable(() -> {
|
|
return userRepository.findById(id);
|
|
});
|
|
|
|
if (user != null) {
|
|
cacheTimer.recordCallable(() -> {
|
|
userCache.put(id, user);
|
|
});
|
|
}
|
|
|
|
return user;
|
|
}
|
|
}
|
|
```
|
|
|
|
### 2. Memory Analysis
|
|
```java
|
|
// Анализ памяти
|
|
@Component
|
|
public class MemoryAnalyzer {
|
|
private final MemoryMXBean memoryBean;
|
|
|
|
public MemoryAnalyzer() {
|
|
this.memoryBean = ManagementFactory.getMemoryMXBean();
|
|
}
|
|
|
|
@Scheduled(fixedRate = 10000)
|
|
public void analyzeMemory() {
|
|
MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();
|
|
MemoryUsage nonHeapUsage = memoryBean.getNonHeapMemoryUsage();
|
|
|
|
log.info("Heap: {} / {} ({}%)",
|
|
heapUsage.getUsed(),
|
|
heapUsage.getMax(),
|
|
(heapUsage.getUsed() * 100) / heapUsage.getMax());
|
|
|
|
log.info("Non-Heap: {} / {} ({}%)",
|
|
nonHeapUsage.getUsed(),
|
|
nonHeapUsage.getMax(),
|
|
(nonHeapUsage.getUsed() * 100) / nonHeapUsage.getMax());
|
|
|
|
// Анализ утечек памяти
|
|
if (heapUsage.getUsed() > heapUsage.getMax() * 0.8) {
|
|
log.warn("High memory usage detected!");
|
|
}
|
|
}
|
|
}
|
|
```
|