api_openai 0.3.0

OpenAI's API for accessing large language models (LLMs).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Performance Monitoring Module
//!
//! This module provides comprehensive performance monitoring capabilities for the `OpenAI` API client,
//! including request overhead measurement, concurrent performance tracking, memory monitoring,
//! and performance regression detection.

mod private
{
  use std::
  {
    collections ::HashMap,
    sync ::{ Arc, Mutex },
    time ::Instant,
  };
  use core::time::Duration;
  use serde::{ Deserialize, Serialize };

  /// Memory usage report structure
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct MemoryUsageReport
  {
    /// Initial memory usage in bytes
    pub initial_usage : u64,
    /// Peak memory usage during monitoring period
    pub peak_usage : u64,
    /// Final memory usage in bytes
    pub final_usage : u64,
    /// Estimated leaked bytes
    pub leaked_bytes : u64,
  }

  /// Performance regression detection report
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct RegressionReport
  {
    /// Baseline performance measurement
    pub baseline_performance : Duration,
    /// Current performance measurement
    pub current_performance : Duration,
    /// Regression percentage (positive indicates degradation)
    pub regression_percentage : f64,
    /// Whether a regression is detected
    pub is_regression : bool,
  }

  /// Throughput metrics under load
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct ThroughputMetrics
  {
    /// Measured requests per second
    pub requests_per_second : f64,
    /// Number of successful requests
    pub successful_requests : u64,
    /// Number of failed requests
    pub failed_requests : u64,
    /// Average latency per request
    pub average_latency : Duration,
  }

  /// Performance monitoring configuration
  #[ derive( Debug, Clone, Serialize, Deserialize ) ]
  pub struct PerformanceConfig
  {
    /// Maximum allowed request overhead in milliseconds
    pub max_request_overhead_ms : u64,
    /// Enable memory monitoring
    pub enable_memory_monitoring : bool,
    /// Enable performance regression detection
    pub enable_regression_detection : bool,
    /// Baseline performance for regression detection
    pub baseline_performance : Option< Duration >,
    /// Regression threshold percentage
    pub regression_threshold_percent : f64,
    /// Overhead consistency threshold multiplier (`std_dev` / mean ratio)
    /// Set to higher values (e.g., 5.0) for test environments with timing jitter
    /// Default: 1.0 (`std_dev` must be less than 100% of mean)
    pub overhead_consistency_threshold : f64,
  }

  impl Default for PerformanceConfig
  {
    #[ inline ]
    fn default() -> Self
    {
      Self
      {
        max_request_overhead_ms : 10,
        enable_memory_monitoring : true,
        enable_regression_detection : true,
        baseline_performance : None,
        regression_threshold_percent : 20.0,
        overhead_consistency_threshold : 1.0,
      }
    }
  }

  /// Performance monitoring context
  #[ derive( Debug ) ]
  pub struct PerformanceMonitor
  {
    config : Arc< Mutex< PerformanceConfig > >,
    metrics : Arc< Mutex< HashMap< String, Vec< Duration > > > >,
    memory_snapshots : Arc< Mutex< Vec< u64 > > >,
  }

  impl PerformanceMonitor
  {
    /// Create a new performance monitor with default configuration
    #[ inline ]
    #[ must_use ]
    pub fn new() -> Self
    {
      Self::with_config( PerformanceConfig::default() )
    }

    /// Update the configuration of an existing performance monitor
    #[ inline ]
    pub fn update_config( &self, new_config : PerformanceConfig )
    {
      if let Ok( mut config ) = self.config.lock()
      {
        *config = new_config;
      }
    }

    /// Create a new performance monitor with custom configuration
    #[ inline ]
    #[ must_use ]
    pub fn with_config( config : PerformanceConfig ) -> Self
    {
      Self
      {
        config : Arc::new( Mutex::new( config ) ),
        metrics : Arc::new( Mutex::new( HashMap::new() ) ),
        memory_snapshots : Arc::new( Mutex::new( Vec::new() ) ),
      }
    }

    /// Measure request overhead
    ///
    /// # Errors
    /// Returns an error if the measured overhead exceeds the configured threshold.
    #[ inline ]
    pub async fn measure_request_overhead( &self ) -> Result< Duration, &'static str >
    {
      let start = Instant::now();

      // Simulate a minimal request overhead measurement
      tokio ::time::sleep( Duration::from_micros( 500 ) ).await;

      let overhead = start.elapsed();

      // Check if overhead exceeds threshold
      let overhead_ms = overhead.as_millis().min( u128::from( u64::MAX ) );
      let max_overhead_ms = if let Ok( config ) = self.config.lock()
      {
        config.max_request_overhead_ms
      }
      else
      {
        10 // Default fallback
      };
      if overhead_ms > u128::from( max_overhead_ms )
      {
        return Err( "Request overhead exceeds configured threshold" );
      }

      // Store metric
      if let Ok( mut metrics ) = self.metrics.lock()
      {
        metrics.entry( "request_overhead".to_string() )
          .or_insert_with( Vec::new )
          .push( overhead );
      }

      Ok( overhead )
    }

    /// Measure overhead consistency across multiple iterations
    ///
    /// # Errors
    /// Returns an error if any individual overhead measurement fails.
    #[ inline ]
    pub async fn measure_overhead_consistency( &self, iterations : usize ) -> Result< Vec< Duration >, &'static str >
    {
      let mut measurements = Vec::with_capacity( iterations );

      for _ in 0..iterations
      {
        let overhead = self.measure_request_overhead().await?;
        measurements.push( overhead );
      }

      // Check consistency - standard deviation should be low
      let mean = measurements.iter().map( |d| d.as_nanos() as f64 ).sum::< f64 >() / measurements.len() as f64;
      let variance = measurements.iter()
        .map( |d| ( d.as_nanos() as f64 - mean ).powi( 2 ) )
        .sum::< f64 >() / measurements.len() as f64;
      let std_dev = variance.sqrt();

      // Check consistency using configurable threshold
      // Note : Threshold can be relaxed for test environments with timing jitter
      let threshold = if let Ok( config ) = self.config.lock()
      {
        config.overhead_consistency_threshold
      }
      else
      {
        1.0 // Default fallback
      };

      if std_dev > mean * threshold
      {
        return Err( "Request overhead measurements are inconsistent" );
      }

      Ok( measurements )
    }

    /// Measure concurrent request performance
    ///
    /// # Errors
    /// Returns an error if any concurrent request measurement fails.
    #[ inline ]
    pub async fn measure_concurrent_performance( &self, concurrent_requests : usize ) -> Result< Vec< Duration >, &'static str >
    {
      let mut handles = Vec::with_capacity( concurrent_requests );

      let start = Instant::now();

      // Launch concurrent requests
      for _ in 0..concurrent_requests
      {
        let handle = tokio::spawn( async move
        {
          let request_start = Instant::now();
          // Simulate request processing
          tokio ::time::sleep( Duration::from_millis( 10 ) ).await;
          request_start.elapsed()
        } );

        handles.push( handle );
      }

      // Wait for all requests to complete
      let mut results = Vec::with_capacity( concurrent_requests );
      for handle in handles
      {
        match handle.await
        {
          Ok( duration ) => results.push( duration ),
          Err( _ ) => return Err( "Concurrent request failed" ),
        }
      }

      let total_time = start.elapsed();

      // Check if concurrent performance is reasonable
      // Total time should not be much more than single request time
      let expected_max_time = Duration::from_millis( 50 ); // Allow some overhead
      if total_time > expected_max_time
      {
        return Err( "Concurrent performance is below expectations" );
      }

      Ok( results )
    }

    /// Monitor memory usage during operation
    ///
    /// # Errors
    /// Returns an error if memory monitoring is disabled or if memory usage monitoring fails.
    #[ inline ]
    pub async fn monitor_memory_usage( &self ) -> Result< MemoryUsageReport, &'static str >
    {
      let enable_monitoring = if let Ok( config ) = self.config.lock()
      {
        config.enable_memory_monitoring
      }
      else
      {
        true // Default fallback
      };

      if !enable_monitoring
      {
        return Err( "Memory monitoring is disabled" );
      }

      // Simulate memory monitoring
      let initial_usage = Self::get_current_memory_usage();
      let mut peak_usage = initial_usage;

      // Monitor for a short period
      for _ in 0..10
      {
        tokio ::time::sleep( Duration::from_millis( 10 ) ).await;
        let current_usage = Self::get_current_memory_usage();
        if current_usage > peak_usage
        {
          peak_usage = current_usage;
        }

        // Store snapshot
        if let Ok( mut snapshots ) = self.memory_snapshots.lock()
        {
          snapshots.push( current_usage );
        }
      }

      let final_usage = Self::get_current_memory_usage();
      let leaked_bytes = final_usage.saturating_sub( initial_usage );

      Ok( MemoryUsageReport
      {
        initial_usage,
        peak_usage,
        final_usage,
        leaked_bytes,
      } )
    }

    /// Detect performance regression
    ///
    /// # Errors
    /// Returns an error if regression detection is disabled, baseline is not configured, or performance measurement fails.
    #[ inline ]
    pub async fn detect_performance_regression( &self ) -> Result< RegressionReport, &'static str >
    {
      let ( enable_detection, baseline, threshold ) = if let Ok( config ) = self.config.lock()
      {
        ( config.enable_regression_detection, config.baseline_performance, config.regression_threshold_percent )
      }
      else
      {
        ( true, None, 20.0 ) // Default fallback
      };

      if !enable_detection
      {
        return Err( "Regression detection is disabled" );
      }

      let Some( baseline ) = baseline else
      {
        return Err( "No baseline performance configured" )
      };

      // Measure current performance
      let current = self.measure_request_overhead().await?;

      // Calculate regression percentage
      let baseline_ms = baseline.as_millis() as f64;
      let current_ms = current.as_millis() as f64;
      let regression_percentage = ( ( current_ms - baseline_ms ) / baseline_ms ) * 100.0;

      let is_regression = regression_percentage > threshold;

      Ok( RegressionReport
      {
        baseline_performance : baseline,
        current_performance : current,
        regression_percentage,
        is_regression,
      } )
    }

    /// Measure throughput under load
    ///
    /// # Errors
    /// Returns an error if load testing fails or measurements are invalid.
    #[ inline ]
    pub async fn measure_throughput_under_load( &self, requests_per_second : usize, duration : Duration ) -> Result< ThroughputMetrics, &'static str >
    {
      let total_requests_f64 = ( (requests_per_second as f64) * duration.as_secs_f64() ).max( 0.0 ).min( usize::MAX as f64 );
      let total_requests = if total_requests_f64.is_finite() && total_requests_f64 >= 0.0
      {
        #[ allow(clippy::cast_possible_truncation, clippy::cast_sign_loss) ]
        let result = total_requests_f64 as usize;
        result
      }
      else
      {
        0usize
      };
      let interval = Duration::from_secs_f64( 1.0 / (requests_per_second as f64) );

      let mut successful_requests = 0u64;
      let mut failed_requests = 0u64;
      let mut latencies = Vec::new();

      let start_time = Instant::now();
      let end_time = start_time + duration;

      let mut request_count = 0;

      while Instant::now() < end_time && request_count < total_requests
      {
        let request_start = Instant::now();

        // Simulate request processing with occasional failures
        tokio ::time::sleep( Duration::from_millis( 5 ) ).await;

        let latency = request_start.elapsed();
        latencies.push( latency );

        // Simulate 95% success rate
        if request_count % 20 != 0
        {
          successful_requests += 1;
        }
        else
        {
          failed_requests += 1;
        }

        request_count += 1;

        // Wait for next request interval
        if request_count < total_requests
        {
          tokio ::time::sleep( interval ).await;
        }
      }

      let actual_duration = start_time.elapsed();
      let actual_rps = successful_requests as f64 / actual_duration.as_secs_f64();

      let average_latency = if latencies.is_empty()
      {
        Duration::from_millis( 0 )
      }
      else
      {
        let total_nanos : u64 = latencies.iter().map( |d| {
          #[ allow(clippy::cast_possible_truncation) ]
          let result = d.as_nanos().min( u128::from( u64::MAX ) ) as u64;
          result
        }).sum();
        Duration::from_nanos( total_nanos / latencies.len() as u64 )
      };

      Ok( ThroughputMetrics
      {
        requests_per_second : actual_rps,
        successful_requests,
        failed_requests,
        average_latency,
      } )
    }

    /// Get current memory usage (simplified simulation)
    fn get_current_memory_usage() -> u64
    {
      // Simulate memory usage - in real implementation this would use system APIs
      use std::time::SystemTime;
      let now = SystemTime::now().duration_since( SystemTime::UNIX_EPOCH )
        .unwrap_or( Duration::from_secs( 0 ) );

      // Simulate some variation in memory usage
      1024 * 1024 * ( 100 + ( now.as_millis() % 50 ) as u64 ) // 100-150 MB range
    }
  }

  impl Default for PerformanceMonitor
  {
    #[ inline ]
    fn default() -> Self
    {
      Self::new()
    }
  }

  /// Global performance monitor instance
  static PERFORMANCE_MONITOR : std::sync::OnceLock< Arc< PerformanceMonitor > > = std::sync::OnceLock::new();

  /// Get the global performance monitor instance
  #[ inline ]
  pub fn get_performance_monitor() -> Arc< PerformanceMonitor >
  {
    PERFORMANCE_MONITOR.get_or_init( || Arc::new( PerformanceMonitor::new() ) ).clone()
  }

  /// Set custom performance monitor configuration
  #[ inline ]
  pub fn configure_performance_monitoring( config : PerformanceConfig )
  {
    let monitor = get_performance_monitor();
    monitor.update_config( config );
  }

  /// Convenience functions for global performance monitoring
  ///
  /// Measure request overhead using global monitor
  ///
  /// # Errors
  /// Returns an error if the request overhead measurement fails.
  #[ inline ]
  pub async fn measure_request_overhead() -> Result< Duration, &'static str >
  {
    get_performance_monitor().measure_request_overhead().await
  }

  /// Measure overhead consistency using global monitor
  ///
  /// # Errors
  /// Returns an error if any overhead measurement fails.
  #[ inline ]
  pub async fn measure_overhead_consistency( iterations : usize ) -> Result< Vec< Duration >, &'static str >
  {
    get_performance_monitor().measure_overhead_consistency( iterations ).await
  }

  /// Measure concurrent performance using global monitor
  ///
  /// # Errors
  /// Returns an error if concurrent performance measurement fails.
  #[ inline ]
  pub async fn measure_concurrent_performance( concurrent_requests : usize ) -> Result< Vec< Duration >, &'static str >
  {
    get_performance_monitor().measure_concurrent_performance( concurrent_requests ).await
  }

  /// Monitor memory usage using global monitor
  ///
  /// # Errors
  /// Returns an error if memory monitoring fails.
  #[ inline ]
  pub async fn monitor_memory_usage() -> Result< MemoryUsageReport, &'static str >
  {
    get_performance_monitor().monitor_memory_usage().await
  }

  /// Detect performance regression using global monitor
  ///
  /// # Errors
  /// Returns an error if regression detection fails or baseline is not configured.
  #[ inline ]
  pub async fn detect_performance_regression() -> Result< RegressionReport, &'static str >
  {
    get_performance_monitor().detect_performance_regression().await
  }

  /// Measure throughput under load using global monitor
  ///
  /// # Errors
  /// Returns an error if load testing fails or measurements are invalid.
  #[ inline ]
  pub async fn measure_throughput_under_load( requests_per_second : usize, duration : Duration ) -> Result< ThroughputMetrics, &'static str >
  {
    get_performance_monitor().measure_throughput_under_load( requests_per_second, duration ).await
  }
}

crate ::mod_interface!
{
  orphan use MemoryUsageReport;
  orphan use RegressionReport;
  orphan use ThroughputMetrics;
  orphan use PerformanceConfig;
  orphan use PerformanceMonitor;
  orphan use get_performance_monitor;
  orphan use configure_performance_monitoring;
  orphan use measure_request_overhead;
  orphan use measure_overhead_consistency;
  orphan use measure_concurrent_performance;
  orphan use monitor_memory_usage;
  orphan use detect_performance_regression;
  orphan use measure_throughput_under_load;
}