aprender-serve 0.64.0

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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

// =============================================================================
// IMP-137: Add Reset Capability for Metrics (RED PHASE - FAILING TESTS)
// =============================================================================
//
// Per spec: Allow resetting metrics to zero for fresh benchmarking.
// This is essential for A/B testing and iterative performance tuning.
//
// Test TDD Anchors:
// - IMP-137a: DispatchMetrics should have reset() method
// - IMP-137b: reset() should clear all counters to zero
// - IMP-137c: reset() should reset all latency tracking
// - IMP-137d: reset() should reset bucket counts

// IMP-137a: DispatchMetrics should have reset() method
#[test]
fn test_imp_137a_dispatch_metrics_has_reset_method() {
    use crate::gguf::DispatchMetrics;
    use std::time::Duration;

    let metrics = DispatchMetrics::new();

    // Record some data
    metrics.record_cpu_dispatch();
    metrics.record_gpu_dispatch();
    metrics.record_cpu_latency(Duration::from_micros(100));

    // IMP-137a: reset() should exist and be callable
    metrics.reset();

    // After reset, all counters should be zero
    assert_eq!(
        metrics.cpu_dispatches(),
        0,
        "IMP-137a: CPU dispatches should be 0 after reset"
    );
    assert_eq!(
        metrics.gpu_dispatches(),
        0,
        "IMP-137a: GPU dispatches should be 0 after reset"
    );
}

// IMP-137b: reset() should clear all counters to zero
#[test]
fn test_imp_137b_reset_clears_all_counters() {
    use crate::gguf::DispatchMetrics;
    use std::time::Duration;

    let metrics = DispatchMetrics::new();

    // Record various data
    for _ in 0..10 {
        metrics.record_cpu_dispatch();
        metrics.record_cpu_latency(Duration::from_micros(100));
    }
    for _ in 0..5 {
        metrics.record_gpu_dispatch();
        metrics.record_gpu_latency(Duration::from_micros(500));
    }

    // Verify data was recorded
    assert_eq!(
        metrics.cpu_dispatches(),
        10,
        "IMP-137b: Pre-reset CPU count"
    );
    assert_eq!(metrics.gpu_dispatches(), 5, "IMP-137b: Pre-reset GPU count");

    // Reset
    metrics.reset();

    // IMP-137b: All counters should be zero
    assert_eq!(
        metrics.cpu_dispatches(),
        0,
        "IMP-137b: Post-reset CPU dispatches"
    );
    assert_eq!(
        metrics.gpu_dispatches(),
        0,
        "IMP-137b: Post-reset GPU dispatches"
    );
    assert_eq!(
        metrics.total_dispatches(),
        0,
        "IMP-137b: Post-reset total dispatches"
    );
    assert_eq!(
        metrics.cpu_latency_count(),
        0,
        "IMP-137b: Post-reset CPU latency count"
    );
    assert_eq!(
        metrics.gpu_latency_count(),
        0,
        "IMP-137b: Post-reset GPU latency count"
    );
}

// IMP-137c: reset() should reset all latency tracking (min/max/mean/variance)
#[test]
fn test_imp_137c_reset_clears_latency_tracking() {
    use crate::gguf::DispatchMetrics;
    use std::time::Duration;

    let metrics = DispatchMetrics::new();

    // Record some latencies
    metrics.record_cpu_latency(Duration::from_micros(100));
    metrics.record_cpu_latency(Duration::from_micros(500));
    metrics.record_cpu_latency(Duration::from_micros(1000));

    // Verify data was recorded
    assert!(
        metrics.cpu_latency_mean_us() > 0.0,
        "IMP-137c: Pre-reset mean should be > 0"
    );

    // Reset
    metrics.reset();

    // IMP-137c: All latency stats should be reset
    assert_eq!(
        metrics.cpu_latency_mean_us(),
        0.0,
        "IMP-137c: Post-reset CPU mean"
    );
    assert_eq!(
        metrics.cpu_latency_min_us(),
        0,
        "IMP-137c: Post-reset CPU min"
    );
    assert_eq!(
        metrics.cpu_latency_max_us(),
        0,
        "IMP-137c: Post-reset CPU max"
    );
    assert_eq!(
        metrics.cpu_latency_variance_us(),
        0.0,
        "IMP-137c: Post-reset CPU variance"
    );
    assert_eq!(
        metrics.cpu_latency_stddev_us(),
        0.0,
        "IMP-137c: Post-reset CPU stddev"
    );
}

// IMP-137d: reset() should reset bucket counts
#[test]
fn test_imp_137d_reset_clears_bucket_counts() {
    use crate::gguf::DispatchMetrics;
    use std::time::Duration;

    let metrics = DispatchMetrics::new();

    // Record latencies in different buckets
    metrics.record_cpu_latency(Duration::from_micros(50)); // bucket 0
    metrics.record_cpu_latency(Duration::from_micros(200)); // bucket 1
    metrics.record_cpu_latency(Duration::from_micros(750)); // bucket 2
    metrics.record_cpu_latency(Duration::from_micros(2000)); // bucket 3
    metrics.record_cpu_latency(Duration::from_micros(10000)); // bucket 4

    // Verify buckets have data
    let buckets_before = metrics.cpu_latency_buckets();
    assert_eq!(
        buckets_before.iter().sum::<usize>(),
        5,
        "IMP-137d: Pre-reset bucket total"
    );

    // Reset
    metrics.reset();

    // IMP-137d: All bucket counts should be zero
    let buckets_after = metrics.cpu_latency_buckets();
    assert_eq!(
        buckets_after,
        [0, 0, 0, 0, 0],
        "IMP-137d: Post-reset buckets should all be 0"
    );
}

// =============================================================================
// IMP-138: Add HTTP Endpoint for Metrics Reset (RED PHASE - FAILING TESTS)
// =============================================================================
//
// Per spec: Expose POST /v1/dispatch/reset endpoint to reset metrics via HTTP.
// This enables remote A/B testing and benchmark automation.
//
// Test TDD Anchors:
// - IMP-138a: POST /v1/dispatch/reset should exist
// - IMP-138b: Reset should return success response
// - IMP-138c: After reset, GET /v1/dispatch should show zero values
// - IMP-138d: Non-POST methods should return 405 Method Not Allowed

// IMP-138a: dispatch_reset_handler function exists and is callable
#[test]
fn test_imp_138a_dispatch_reset_handler_exists() {
    // IMP-138a: Verify handler function signature is correct
    // The handler exists and can be referenced (compile-time check)
    fn _assert_handler_exists<F, Fut>(f: F)
    where
        F: Fn(axum::extract::State<AppState>) -> Fut,
        Fut: std::future::Future<Output = axum::response::Response>,
    {
        let _ = f;
    }
    _assert_handler_exists(dispatch_reset_handler);
}

// IMP-138b: Reset endpoint should return success JSON
#[tokio::test]
async fn test_imp_138b_reset_returns_success_response() {
    use crate::gguf::DispatchMetrics;
    use std::sync::Arc;

    // Create metrics with some data
    let metrics = Arc::new(DispatchMetrics::new());
    metrics.record_cpu_dispatch();
    metrics.record_gpu_dispatch();

    // IMP-138b: Build reset response
    let response = DispatchResetResponse {
        success: true,
        message: "Metrics reset successfully".to_string(),
    };

    // Serialize and verify
    let json = serde_json::to_string(&response).expect("IMP-138b: Should serialize");
    assert!(
        json.contains("\"success\":true"),
        "IMP-138b: Should have success: true"
    );
    assert!(
        json.contains("reset successfully"),
        "IMP-138b: Should have success message"
    );
}

// IMP-138c: After reset, metrics should be zero
#[tokio::test]
async fn test_imp_138c_reset_endpoint_clears_metrics() {
    use crate::gguf::DispatchMetrics;
    use std::sync::Arc;
    use std::time::Duration;

    let metrics = Arc::new(DispatchMetrics::new());

    // Record some data
    for _ in 0..10 {
        metrics.record_cpu_dispatch();
        metrics.record_cpu_latency(Duration::from_micros(100));
    }

    // Verify data exists
    assert_eq!(metrics.cpu_dispatches(), 10, "IMP-138c: Pre-reset count");

    // Call reset
    metrics.reset();

    // IMP-138c: After reset, all should be zero
    assert_eq!(
        metrics.cpu_dispatches(),
        0,
        "IMP-138c: Post-reset CPU dispatches"
    );
    assert_eq!(
        metrics.gpu_dispatches(),
        0,
        "IMP-138c: Post-reset GPU dispatches"
    );
    assert_eq!(
        metrics.cpu_latency_count(),
        0,
        "IMP-138c: Post-reset latency count"
    );
}

// IMP-138d: DispatchResetResponse can be deserialized
#[test]
fn test_imp_138d_reset_response_deserialization() {
    // IMP-138d: Verify response can be deserialized (for client integration)
    let json = r#"{"success":true,"message":"Metrics reset successfully"}"#;
    let response: DispatchResetResponse =
        serde_json::from_str(json).expect("IMP-138d: Should deserialize");

    assert!(response.success, "IMP-138d: success should be true");
    assert_eq!(
        response.message, "Metrics reset successfully",
        "IMP-138d: message should match"
    );
}

// =============================================================================
// IMP-139: Add Reset Route to Main Router (RED PHASE - FAILING TESTS)
// =============================================================================
//
// Per spec: Wire up POST /metrics/dispatch/reset in create_router()
// This makes the reset endpoint available via the standard API.
//
// Test TDD Anchors:
// - IMP-139a: create_router should include reset route
// - IMP-139b: Reset route should accept POST method
// - IMP-139c: Reset route path should be /metrics/dispatch/reset
// - IMP-139d: Router should compile with reset route

// IMP-139a: create_router should include dispatch reset route
#[test]
fn test_imp_139a_router_includes_reset_route() {
    // IMP-139a: Verify create_router includes the reset route
    // This is a compile-time check - if the route is registered, the code compiles
    let state = AppState::with_cache(10);
    let router = create_router(state);

    // Router exists and is usable (compile-time check)
    let _ = router;
}

// IMP-139b: Reset route path should be correct
#[test]
fn test_imp_139b_reset_route_path() {
    // IMP-139b: The reset route should be at /metrics/dispatch/reset
    // This verifies the path constant matches expectation
    const EXPECTED_PATH: &str = "/metrics/dispatch/reset";

    // Path should be correctly formed
    assert!(
        EXPECTED_PATH.starts_with("/metrics/dispatch"),
        "IMP-139b: Reset route should be under /metrics/dispatch"
    );
    assert!(
        EXPECTED_PATH.ends_with("/reset"),
        "IMP-139b: Reset route should end with /reset"
    );
}

// IMP-139c: Router should have the dispatch_reset_handler wired
#[tokio::test]
async fn test_imp_139c_router_has_reset_handler() {
    use axum::body::Body;
    use hyper::Request;
    use tower::ServiceExt;

    let state = AppState::with_cache(10);
    let router = create_router(state);

    // Make a POST request to the reset endpoint
    let req = Request::builder()
        .method("POST")
        .uri("/metrics/dispatch/reset")
        .body(Body::empty())
        .expect("IMP-139c: Should build request");

    let response = router
        .oneshot(req)
        .await
        .expect("IMP-139c: Should get response");

    // Should not return 404 (route exists)
    // May return 503 if no GPU model, but that's fine
    assert_ne!(
        response.status().as_u16(),
        404,
        "IMP-139c: Reset route should exist (not 404)"
    );
}

// IMP-139d: GET method on reset route should return 405
#[tokio::test]
async fn test_imp_139d_reset_route_rejects_get() {
    use axum::body::Body;
    use hyper::Request;
    use tower::ServiceExt;

    let state = AppState::with_cache(10);
    let router = create_router(state);

    // Make a GET request to the reset endpoint (should fail)
    let req = Request::builder()
        .method("GET")
        .uri("/metrics/dispatch/reset")
        .body(Body::empty())
        .expect("IMP-139d: Should build request");

    let response = router
        .oneshot(req)
        .await
        .expect("IMP-139d: Should get response");

    // GET should return 405 Method Not Allowed
    assert_eq!(
        response.status().as_u16(),
        405,
        "IMP-139d: GET on reset route should return 405"
    );
}

// =============================================================================
// IMP-140: Add Throughput Metrics (RED PHASE - FAILING TESTS)
// =============================================================================
//
// Per spec: Track requests per second for performance monitoring.
// This enables throughput analysis and SLA validation.
//
// Test TDD Anchors:
// - IMP-140a: DispatchMetrics should track start time
// - IMP-140b: elapsed_seconds() should return time since start/reset
// - IMP-140c: throughput_rps() should return requests/second
// - IMP-140d: JSON response should include throughput_rps

// IMP-140a: DispatchMetrics should track start time
#[test]
fn test_imp_140a_dispatch_metrics_tracks_start_time() {
    use crate::gguf::DispatchMetrics;

    let metrics = DispatchMetrics::new();

    // IMP-140a: start_time_ms() should return milliseconds since epoch
    let start_time = metrics.start_time_ms();
    assert!(start_time > 0, "IMP-140a: Start time should be > 0");

    // Start time should be recent (within last minute)
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("IMP-140a: Should get time")
        .as_millis() as u64;
    assert!(
        now - start_time < 60_000,
        "IMP-140a: Start time should be within last minute"
    );
}

// IMP-140b: elapsed_seconds() should return time since start/reset
#[test]
fn test_imp_140b_elapsed_seconds() {
    use crate::gguf::DispatchMetrics;

    let metrics = DispatchMetrics::new();

    // IMP-140b: elapsed_seconds() should return positive duration
    let elapsed = metrics.elapsed_seconds();
    assert!(elapsed >= 0.0, "IMP-140b: Elapsed should be >= 0");
    assert!(elapsed < 10.0, "IMP-140b: Elapsed should be small (< 10s)");
}