mirror of
https://github.com/0xfnzero/solana-streamer.git
synced 2026-08-18 11:28:05 +00:00
feat: Add account event parser and protocol type definitions
- Add account event parser (account_event_parser.rs) for account-level event handling - Introduce type definitions for pumpfun, pumpswap, raydium_amm_v4, raydium_clmm, raydium_cpmm protocols - Enhance event processor to support account event processing alongside transactions - Improve subscription system with separate transaction and account filters - Optimize parser configuration using Option types for cleaner code structure - Update examples and documentation to reflect new capabilities Changes include: - 6 new types.rs files for protocol-specific data structures - Updated event processor for account, transaction, and block meta events - Refactored subscription manager with account filtering support - Enhanced metrics and batch processing logic
This commit is contained in:
@@ -108,7 +108,7 @@ impl StreamClientConfig {
|
||||
channel_size: 20000,
|
||||
strategy: BackpressureStrategy::Drop,
|
||||
},
|
||||
enable_metrics: true,
|
||||
enable_metrics: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use super::constants::*;
|
||||
use super::config::StreamClientConfig;
|
||||
use super::constants::*;
|
||||
|
||||
/// 通用性能监控指标
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PerformanceMetrics {
|
||||
pub start_time: std::time::Instant,
|
||||
pub process_count: u64,
|
||||
pub events_processed: u64,
|
||||
pub events_per_second: f64,
|
||||
pub average_processing_time_ms: f64,
|
||||
pub min_processing_time_ms: f64,
|
||||
pub max_processing_time_ms: f64,
|
||||
pub cache_hit_rate: f64,
|
||||
pub last_update_time: std::time::Instant,
|
||||
pub events_in_window: u64,
|
||||
pub window_start_time: std::time::Instant,
|
||||
@@ -28,12 +29,13 @@ impl PerformanceMetrics {
|
||||
pub fn new() -> Self {
|
||||
let now = std::time::Instant::now();
|
||||
Self {
|
||||
start_time: std::time::Instant::now(),
|
||||
process_count: 0,
|
||||
events_processed: 0,
|
||||
events_per_second: 0.0,
|
||||
average_processing_time_ms: 0.0,
|
||||
min_processing_time_ms: 0.0,
|
||||
max_processing_time_ms: 0.0,
|
||||
cache_hit_rate: 0.0,
|
||||
last_update_time: now,
|
||||
events_in_window: 0,
|
||||
window_start_time: now,
|
||||
@@ -51,7 +53,7 @@ pub struct MetricsManager {
|
||||
impl MetricsManager {
|
||||
/// 创建新的性能监控管理器
|
||||
pub fn new(
|
||||
metrics: Arc<Mutex<PerformanceMetrics>>,
|
||||
metrics: Arc<Mutex<PerformanceMetrics>>,
|
||||
config: Arc<StreamClientConfig>,
|
||||
stream_name: String,
|
||||
) -> Self {
|
||||
@@ -68,14 +70,13 @@ impl MetricsManager {
|
||||
pub async fn print_metrics(&self) {
|
||||
let metrics = self.get_metrics().await;
|
||||
println!("📊 {} Performance Metrics:", self.stream_name);
|
||||
println!(" Run Time: {:?}", metrics.start_time.elapsed());
|
||||
println!(" Process Count: {}", metrics.process_count);
|
||||
println!(" Events Processed: {}", metrics.events_processed);
|
||||
println!(" Events/Second: {:.2}", metrics.events_per_second);
|
||||
println!(" Avg Processing Time: {:.2}ms", metrics.average_processing_time_ms);
|
||||
println!(" Min Processing Time: {:.2}ms", metrics.min_processing_time_ms);
|
||||
println!(" Max Processing Time: {:.2}ms", metrics.max_processing_time_ms);
|
||||
if metrics.cache_hit_rate > 0.0 {
|
||||
println!(" Cache Hit Rate: {:.2}%", metrics.cache_hit_rate * 100.0);
|
||||
}
|
||||
println!("---");
|
||||
}
|
||||
|
||||
@@ -88,9 +89,9 @@ impl MetricsManager {
|
||||
|
||||
let metrics_manager = self.clone();
|
||||
tokio::spawn(async move {
|
||||
let mut interval = tokio::time::interval(
|
||||
tokio::time::Duration::from_secs(DEFAULT_METRICS_PRINT_INTERVAL_SECONDS)
|
||||
);
|
||||
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(
|
||||
DEFAULT_METRICS_PRINT_INTERVAL_SECONDS,
|
||||
));
|
||||
loop {
|
||||
interval.tick().await;
|
||||
metrics_manager.print_metrics().await;
|
||||
@@ -98,6 +99,15 @@ impl MetricsManager {
|
||||
});
|
||||
}
|
||||
|
||||
/// 更新处理次数
|
||||
pub async fn add_process_count(&self) {
|
||||
if !self.config.enable_metrics {
|
||||
return;
|
||||
}
|
||||
let mut metrics = self.metrics.lock().await;
|
||||
metrics.process_count += 1;
|
||||
}
|
||||
|
||||
/// 更新性能指标
|
||||
pub async fn update_metrics(&self, events_processed: u64, processing_time_ms: f64) {
|
||||
// 检查是否启用性能监控
|
||||
@@ -107,26 +117,29 @@ impl MetricsManager {
|
||||
|
||||
let mut metrics = self.metrics.lock().await;
|
||||
let now = std::time::Instant::now();
|
||||
|
||||
|
||||
metrics.events_processed += events_processed;
|
||||
metrics.events_in_window += events_processed;
|
||||
metrics.last_update_time = now;
|
||||
|
||||
|
||||
// 更新最快和最慢处理时间
|
||||
if processing_time_ms < metrics.min_processing_time_ms || metrics.min_processing_time_ms == 0.0 {
|
||||
if processing_time_ms < metrics.min_processing_time_ms
|
||||
|| metrics.min_processing_time_ms == 0.0
|
||||
{
|
||||
metrics.min_processing_time_ms = processing_time_ms;
|
||||
}
|
||||
if processing_time_ms > metrics.max_processing_time_ms {
|
||||
metrics.max_processing_time_ms = processing_time_ms;
|
||||
}
|
||||
|
||||
|
||||
// 计算平均处理时间
|
||||
if metrics.events_processed > 0 {
|
||||
metrics.average_processing_time_ms =
|
||||
(metrics.average_processing_time_ms * (metrics.events_processed - events_processed) as f64 + processing_time_ms)
|
||||
metrics.average_processing_time_ms = (metrics.average_processing_time_ms
|
||||
* (metrics.events_processed - events_processed) as f64
|
||||
+ processing_time_ms)
|
||||
/ metrics.events_processed as f64;
|
||||
}
|
||||
|
||||
|
||||
// 基于时间窗口计算每秒处理事件数
|
||||
let window_duration = std::time::Duration::from_secs(DEFAULT_METRICS_WINDOW_SECONDS);
|
||||
if now.duration_since(metrics.window_start_time) >= window_duration {
|
||||
@@ -137,22 +150,11 @@ impl MetricsManager {
|
||||
// 如果窗口内没有事件,保持之前的速率或设为0
|
||||
metrics.events_per_second = 0.0;
|
||||
}
|
||||
|
||||
|
||||
// 重置窗口
|
||||
metrics.events_in_window = 0;
|
||||
metrics.window_start_time = now;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// 更新缓存命中率
|
||||
pub async fn update_cache_hit_rate(&self, hit_rate: f64) {
|
||||
if !self.config.enable_metrics {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut metrics = self.metrics.lock().await;
|
||||
metrics.cache_hit_rate = hit_rate;
|
||||
}
|
||||
|
||||
/// 记录慢处理操作
|
||||
|
||||
Reference in New Issue
Block a user