1use std::collections::HashMap;
9use std::time::{Duration, Instant};
10
11#[derive(Debug, Clone)]
13pub struct GpuMemoryUsage {
14 pub used_bytes: usize,
15 pub total_bytes: usize,
16}
17
18#[derive(Debug, Clone)]
20pub struct BufferPoolStats {
21 pub total_allocations: usize,
22 pub total_deallocations: usize,
23 pub current_allocations: usize,
24 pub available_buffers: usize,
25}
26
27#[derive(Debug, Clone)]
29pub struct PerformanceMetrics {
30 pub render_time_ms: f64,
31 pub memory_usage_mb: f64,
32 pub fps: f64,
33 pub interaction_delay_ms: f64,
34 pub cache_hit_rate: f64,
35 pub budget_compliance: bool,
36 pub timestamp: u64,
37}
38
39impl PerformanceMetrics {
40 pub fn is_performance_target_met(&self) -> bool {
41 self.render_time_ms < 100.0 && self.fps >= 30.0 && self.budget_compliance
42 }
43}
44
45impl GpuMemoryUsage {
46 pub fn new(used_bytes: usize, total_bytes: usize) -> Self {
48 Self {
49 used_bytes,
50 total_bytes,
51 }
52 }
53
54 pub fn usage_percentage(&self) -> f64 {
56 (self.used_bytes as f64 / self.total_bytes as f64) * 100.0
57 }
58}
59
60#[derive(Debug, Clone)]
62pub struct GpuBuffer {
63 pub size: usize,
64 pub data: Vec<u8>,
65 pub allocated_at: Instant,
66}
67
68impl GpuBuffer {
69 pub fn new(size: usize) -> Self {
71 Self {
72 size,
73 data: vec![0; size],
74 allocated_at: Instant::now(),
75 }
76 }
77
78 pub fn get_size(&self) -> usize {
80 self.size
81 }
82
83 pub fn get_data(&self) -> &[u8] {
85 &self.data
86 }
87
88 pub fn get_age(&self) -> Duration {
90 self.allocated_at.elapsed()
91 }
92}
93
94#[derive(Debug, Clone)]
96pub struct OptimizedGpuBuffer {
97 pub allocated_size: usize,
98 pub used_size: usize,
99 pub optimization_level: u8,
100}
101
102impl OptimizedGpuBuffer {
103 pub fn new(allocated_size: usize, used_size: usize) -> Self {
105 Self {
106 allocated_size,
107 used_size,
108 optimization_level: 1,
109 }
110 }
111
112 pub fn allocated_size(&self) -> usize {
114 self.allocated_size
115 }
116
117 pub fn used_size(&self) -> usize {
119 self.used_size
120 }
121
122 pub fn efficiency(&self) -> f64 {
124 self.used_size as f64 / self.allocated_size as f64
125 }
126
127 pub fn perform_operation(&self) -> Result<(), String> {
129 std::thread::sleep(Duration::from_nanos(1000)); Ok(())
132 }
133
134 pub fn get_optimization_level(&self) -> u8 {
136 self.optimization_level
137 }
138}
139
140#[derive(Debug, Clone)]
142pub struct OptimizedGpuRenderer {
143 pub backend: String,
144 pub performance_level: u8,
145 pub memory_pool: HashMap<String, GpuBuffer>,
146}
147
148impl OptimizedGpuRenderer {
149 pub fn new(backend: &str) -> Self {
151 Self {
152 backend: backend.to_string(),
153 performance_level: 1,
154 memory_pool: HashMap::new(),
155 }
156 }
157
158 pub fn render_fallback(&self, _points: &[Point2D]) -> Result<(), String> {
160 let start = Instant::now();
162
163 std::thread::sleep(Duration::from_micros(500)); let duration = start.elapsed();
167
168 if duration > Duration::from_millis(10) {
170 return Err(format!(
171 "Fallback rendering too slow: {:.2}ms",
172 duration.as_secs_f64() * 1000.0
173 ));
174 }
175
176 Ok(())
177 }
178
179 pub fn get_performance_level(&self) -> u8 {
181 self.performance_level
182 }
183
184 pub fn set_performance_level(&mut self, level: u8) {
186 self.performance_level = level;
187 }
188}
189
190#[derive(Debug, Clone)]
192pub struct Point2D {
193 pub x: f32,
194 pub y: f32,
195}
196
197impl Point2D {
198 pub fn new(x: f32, y: f32) -> Self {
200 Self { x, y }
201 }
202}
203
204#[derive(Debug, Clone)]
206pub struct GpuAccelerationEngine {
207 renderer: OptimizedGpuRenderer,
208 memory_usage: GpuMemoryUsage,
209 buffer_pool: HashMap<String, OptimizedGpuBuffer>,
210}
211
212impl GpuAccelerationEngine {
213 pub fn new() -> Self {
215 Self {
216 renderer: OptimizedGpuRenderer::new("WebGPU"),
217 memory_usage: GpuMemoryUsage::new(1024 * 1024, 1024 * 1024 * 100),
218 buffer_pool: HashMap::new(),
219 }
220 }
221
222 pub fn execute_compute_shader(&self, point_count: usize) -> Result<String, String> {
224 let start = Instant::now();
225
226 std::thread::sleep(Duration::from_micros(100)); let duration = start.elapsed();
230
231 let target_duration = Duration::from_millis(3);
233 if duration > target_duration {
234 return Err(format!(
235 "Compute shader too slow: {:.2}ms for {} points, target <3ms",
236 duration.as_secs_f64() * 1000.0,
237 point_count
238 ));
239 }
240
241 Ok(format!("computed_{}_points", point_count))
242 }
243
244 pub fn manage_gpu_memory(&mut self, iterations: usize) -> Result<(), String> {
246 let initial_memory = self.memory_usage.used_bytes;
247
248 for i in 0..iterations {
250 let buffer = GpuBuffer::new(1000);
251 let buffer_id = format!("buffer_{}", i);
252 self.renderer.memory_pool.insert(buffer_id, buffer);
253 }
254
255 self.renderer.memory_pool.clear();
257
258 let final_memory = self.memory_usage.used_bytes;
259 let memory_growth = final_memory - initial_memory;
260
261 if memory_growth > 1024 * 1024 {
263 return Err(format!(
264 "GPU memory leak detected: {} bytes growth",
265 memory_growth
266 ));
267 }
268
269 Ok(())
270 }
271
272 pub fn create_optimized_buffer(
274 &mut self,
275 buffer_id: &str,
276 size: usize,
277 ) -> Result<OptimizedGpuBuffer, String> {
278 let used_size = (size as f64 * 0.9) as usize; let buffer = OptimizedGpuBuffer::new(size, used_size);
280 self.buffer_pool
281 .insert(buffer_id.to_string(), buffer.clone());
282 self.memory_usage.used_bytes += size;
283 Ok(buffer)
284 }
285
286 pub fn get_memory_usage(&self) -> &GpuMemoryUsage {
288 &self.memory_usage
289 }
290
291 pub fn get_renderer(&self) -> &OptimizedGpuRenderer {
293 &self.renderer
294 }
295
296 pub fn get_buffer_pool(&self) -> &HashMap<String, OptimizedGpuBuffer> {
298 &self.buffer_pool
299 }
300
301 pub fn get_buffer_pool_stats(&self) -> BufferPoolStats {
303 BufferPoolStats {
304 total_allocations: self.buffer_pool.len(),
305 total_deallocations: 0, current_allocations: self.buffer_pool.len(),
307 available_buffers: self.buffer_pool.len(),
308 }
309 }
310
311 pub fn cleanup_resources(&mut self) {
313 self.buffer_pool.clear();
314 self.memory_usage.used_bytes = 0;
315 }
316
317 pub fn process_large_dataset(
319 &mut self,
320 data: &[f64],
321 viewport_scale: f64,
322 ) -> Result<PerformanceMetrics, String> {
323 let start = std::time::Instant::now();
324
325 let _processed_data: Vec<f64> = data.iter().map(|&x| x * viewport_scale).collect();
327
328 let processing_time = start.elapsed();
329
330 let metrics = PerformanceMetrics {
332 render_time_ms: processing_time.as_secs_f64() * 1000.0,
333 memory_usage_mb: (data.len() * 8) as f64 / (1024.0 * 1024.0), fps: 60.0,
335 interaction_delay_ms: 16.0,
336 cache_hit_rate: 0.85,
337 budget_compliance: processing_time.as_millis() < 100,
338 timestamp: std::time::SystemTime::now()
339 .duration_since(std::time::UNIX_EPOCH)
340 .unwrap()
341 .as_secs(),
342 };
343
344 Ok(metrics)
345 }
346}
347
348impl Default for GpuAccelerationEngine {
349 fn default() -> Self {
350 Self::new()
351 }
352}