numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Tests for performance metrics and monitoring

use numrs2::parallel::{ThreadPool, ThreadPoolConfig, WorkStealingPool, task};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;

// ============================================================================
// Statistics Collection Tests
// ============================================================================

#[test]
fn test_thread_pool_statistics_accuracy() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(4),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    let counter = Arc::new(AtomicU32::new(0));

    // Submit 20 tasks
    for _ in 0..20 {
        let counter_clone = Arc::clone(&counter);
        pool.submit(move || {
            counter_clone.fetch_add(1, Ordering::SeqCst);
        })
        .expect("Failed to submit task");
    }

    pool.wait().expect("Failed to wait");

    let stats = pool.statistics();
    assert_eq!(stats.tasks_submitted, 20);
    assert_eq!(counter.load(Ordering::SeqCst), 20);
}

#[test]
fn test_work_stealing_pool_statistics() {
    let pool = WorkStealingPool::new(4).expect("Failed to create work-stealing pool");

    let counter = Arc::new(AtomicU32::new(0));

    for _ in 0..50 {
        let counter_clone = Arc::clone(&counter);
        let task = task(move || {
            counter_clone.fetch_add(1, Ordering::SeqCst);
        });
        pool.submit(task).expect("Failed to submit task");
    }

    std::thread::sleep(Duration::from_millis(300));

    let stats = pool.statistics();
    assert_eq!(stats.tasks_submitted, 50);
    assert!(stats.tasks_completed <= 50);
    assert_eq!(counter.load(Ordering::SeqCst), 50);
}

// ============================================================================
// Performance Metrics Tests
// ============================================================================

#[test]
fn test_throughput_measurement() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(4),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    let counter = Arc::new(AtomicU32::new(0));
    let task_count = 100;

    let start = std::time::Instant::now();

    for _ in 0..task_count {
        let counter_clone = Arc::clone(&counter);
        pool.submit(move || {
            counter_clone.fetch_add(1, Ordering::SeqCst);
        })
        .expect("Failed to submit task");
    }

    pool.wait().expect("Failed to wait");
    let duration = start.elapsed();

    let throughput = task_count as f64 / duration.as_secs_f64();
    assert!(throughput > 0.0);
    assert_eq!(counter.load(Ordering::SeqCst), task_count);

    println!("Throughput: {:.2} tasks/sec", throughput);
}

#[test]
fn test_latency_tracking() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(2),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    let latencies = Arc::new(std::sync::Mutex::new(Vec::new()));

    for _ in 0..10 {
        let latencies_clone = Arc::clone(&latencies);
        let submit_time = std::time::Instant::now();

        pool.submit(move || {
            let latency = submit_time.elapsed();
            latencies_clone
                .lock()
                .expect("Failed to lock")
                .push(latency);
        })
        .expect("Failed to submit task");
    }

    pool.wait().expect("Failed to wait");

    let latencies_vec = latencies.lock().expect("Failed to lock");
    assert_eq!(latencies_vec.len(), 10);

    // Calculate average latency
    let avg_latency = latencies_vec.iter().sum::<Duration>() / latencies_vec.len() as u32;
    assert!(avg_latency < Duration::from_secs(1));
}

// ============================================================================
// Worker Utilization Tests
// ============================================================================

#[test]
fn test_cpu_utilization_tracking() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(4),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    // Submit CPU-intensive tasks
    for _ in 0..20 {
        pool.submit(|| {
            let mut sum = 0u64;
            for i in 0..1_000_000 {
                sum = sum.wrapping_add(i);
            }
            assert!(sum > 0);
        })
        .expect("Failed to submit task");
    }

    std::thread::sleep(Duration::from_millis(200));

    let stats = pool.statistics();
    assert!(stats.worker_utilization.len() > 0);

    // Some workers should be utilized
    let total_utilization: f64 = stats.worker_utilization.iter().sum();
    assert!(total_utilization >= 0.0);
}

#[test]
fn test_idle_time_tracking() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(4),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    // Initial state - workers should be idle
    std::thread::sleep(Duration::from_millis(50));
    let initial_stats = pool.statistics();
    assert!(initial_stats.active_threads >= 0);

    // Submit tasks
    for _ in 0..10 {
        pool.submit(|| {
            std::thread::sleep(Duration::from_millis(10));
        })
        .expect("Failed to submit task");
    }

    std::thread::sleep(Duration::from_millis(50));
    let active_stats = pool.statistics();
    assert!(active_stats.tasks_submitted > 0);

    pool.wait().expect("Failed to wait");

    // After completion, workers should be idle again
    std::thread::sleep(Duration::from_millis(50));
    let final_stats = pool.statistics();
    assert_eq!(final_stats.tasks_submitted, 10);
}

// ============================================================================
// Queue Metrics Tests
// ============================================================================

#[test]
fn test_queue_length_monitoring() {
    let pool = WorkStealingPool::new(2).expect("Failed to create work-stealing pool");

    // Submit many slow tasks to fill queues
    for _ in 0..50 {
        let task = task(|| {
            std::thread::sleep(Duration::from_millis(20));
        });
        pool.submit(task).expect("Failed to submit task");
    }

    // Check queue metrics
    std::thread::sleep(Duration::from_millis(10));
    let pending = pool.pending_tasks();
    assert!(pending > 0);

    std::thread::sleep(Duration::from_secs(2));
    let final_pending = pool.pending_tasks();
    assert_eq!(final_pending, 0);
}

#[test]
fn test_queue_wait_time() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(1),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    let wait_times = Arc::new(std::sync::Mutex::new(Vec::new()));

    // Submit tasks
    for _ in 0..5 {
        let wait_times_clone = Arc::clone(&wait_times);
        let submit_time = std::time::Instant::now();

        pool.submit(move || {
            let wait_time = submit_time.elapsed();
            wait_times_clone
                .lock()
                .expect("Failed to lock")
                .push(wait_time);
            std::thread::sleep(Duration::from_millis(10));
        })
        .expect("Failed to submit task");
    }

    pool.wait().expect("Failed to wait");

    let times = wait_times.lock().expect("Failed to lock");
    assert_eq!(times.len(), 5);

    // Later tasks should have longer wait times
    assert!(times[4] >= times[0]);
}

// ============================================================================
// Statistics API Tests
// ============================================================================

#[test]
fn test_statistics_api_completeness() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(4),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    for _ in 0..10 {
        pool.submit(|| {
            std::thread::sleep(Duration::from_millis(5));
        })
        .expect("Failed to submit task");
    }

    pool.wait().expect("Failed to wait");

    let stats = pool.statistics();

    // Verify all statistics fields
    assert_eq!(stats.tasks_submitted, 10);
    assert!(stats.active_threads >= 0);
    assert!(stats.worker_utilization.len() > 0);
}

#[test]
fn test_work_stealing_pool_metrics() {
    let pool = WorkStealingPool::new(4).expect("Failed to create work-stealing pool");

    for _ in 0..30 {
        let task = task(|| {
            std::thread::sleep(Duration::from_millis(5));
        });
        pool.submit(task).expect("Failed to submit task");
    }

    std::thread::sleep(Duration::from_millis(300));

    let stats = pool.statistics();
    assert_eq!(stats.tasks_submitted, 30);
    assert!(stats.worker_utilization.len() > 0);
}

// ============================================================================
// Real-time Monitoring Tests
// ============================================================================

#[test]
fn test_real_time_active_workers_count() {
    let pool = WorkStealingPool::new(4).expect("Failed to create work-stealing pool");

    // Initially no active workers
    let initial_active = pool.active_workers();
    assert!(initial_active >= 0);

    // Submit tasks
    for _ in 0..20 {
        let task = task(|| {
            std::thread::sleep(Duration::from_millis(50));
        });
        pool.submit(task).expect("Failed to submit task");
    }

    // During execution, should have active workers
    std::thread::sleep(Duration::from_millis(10));
    let active_during = pool.active_workers();
    assert!(active_during > 0);

    std::thread::sleep(Duration::from_secs(2));

    // After completion, workers should be idle
    let final_active = pool.active_workers();
    assert!(final_active >= 0);
}

#[test]
fn test_pending_tasks_real_time_tracking() {
    let pool = WorkStealingPool::new(2).expect("Failed to create work-stealing pool");

    // Submit slow tasks
    for _ in 0..30 {
        let task = task(|| {
            std::thread::sleep(Duration::from_millis(30));
        });
        pool.submit(task).expect("Failed to submit task");
    }

    // Check pending tasks immediately
    std::thread::sleep(Duration::from_millis(10));
    let pending_start = pool.pending_tasks();
    assert!(pending_start > 0);

    // Wait a bit and check again
    std::thread::sleep(Duration::from_millis(100));
    let pending_mid = pool.pending_tasks();
    assert!(pending_mid < pending_start);

    // Wait for all to complete
    std::thread::sleep(Duration::from_secs(2));
    let pending_end = pool.pending_tasks();
    assert_eq!(pending_end, 0);
}

// ============================================================================
// Historical Metrics Tests
// ============================================================================

#[test]
fn test_cumulative_statistics() {
    let pool = ThreadPool::with_config(ThreadPoolConfig {
        num_threads: Some(4),
        ..Default::default()
    })
    .expect("Failed to create thread pool");

    // First batch
    for _ in 0..10 {
        pool.submit(|| {}).expect("Failed to submit task");
    }
    pool.wait().expect("Failed to wait");

    let stats1 = pool.statistics();
    assert_eq!(stats1.tasks_submitted, 10);

    // Second batch
    for _ in 0..15 {
        pool.submit(|| {}).expect("Failed to submit task");
    }
    pool.wait().expect("Failed to wait");

    let stats2 = pool.statistics();
    assert_eq!(stats2.tasks_submitted, 25);
}

#[test]
fn test_metrics_consistency() {
    let pool = WorkStealingPool::new(4).expect("Failed to create work-stealing pool");

    for _ in 0..50 {
        let task = task(|| {});
        pool.submit(task).expect("Failed to submit task");
    }

    std::thread::sleep(Duration::from_millis(200));

    let stats = pool.statistics();

    // Consistency checks
    assert!(stats.tasks_submitted >= stats.tasks_completed);
    assert!(stats.worker_utilization.len() > 0);
    assert!(stats.queue_imbalance >= 0.0 && stats.queue_imbalance <= 1.0);
}