thread-flow 0.1.0

Thread dataflow integration for data processing pipelines, using CocoIndex.
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// SPDX-FileCopyrightText: 2026 Knitli Inc.
// SPDX-License-Identifier: AGPL-3.0-or-later

//! D1 Query Profiling Benchmarks
//!
//! Measures D1-related performance metrics and validates constitutional requirements.
//!
//! # Benchmark Coverage
//!
//! 1. SQL statement generation latency
//! 2. Cache lookup performance
//! 3. Performance metrics overhead
//! 4. Context creation overhead
//!
//! # Running Benchmarks
//!
//! ```bash
//! # All D1 profiling benchmarks
//! cargo bench --bench d1_profiling --features caching
//!
//! # Specific benchmark group
//! cargo bench --bench d1_profiling statement_generation
//! cargo bench --bench d1_profiling cache_operations
//! cargo bench --bench d1_profiling metrics_tracking
//! ```
//!
//! # Constitutional Compliance
//!
//! - Database p95 latency target: <50ms (D1)
//! - Cache hit rate target: >90%
//! - These benchmarks measure infrastructure overhead, not actual D1 API latency

use criterion::{Criterion, criterion_group, criterion_main};
use recoco::base::schema::{BasicValueType, EnrichedValueType, FieldSchema, ValueType};
use recoco::base::value::{BasicValue, FieldValues, KeyPart, KeyValue};
use std::hint::black_box;
use std::sync::Arc;
use std::time::Duration;
use thread_flow::monitoring::performance::PerformanceMetrics;
use thread_flow::targets::d1::D1ExportContext;

/// Helper to create test FieldSchema
fn test_field_schema(name: &str, value_type: BasicValueType, nullable: bool) -> FieldSchema {
    FieldSchema::new(
        name,
        // spellchecker:off
        EnrichedValueType {
            typ: ValueType::Basic(value_type),
            nullable,
            attrs: Default::default(),
        },
        // spellchecker:on
    )
}

/// Create a test D1 context for benchmarking
fn create_benchmark_context() -> D1ExportContext {
    let metrics = PerformanceMetrics::new();

    let key_schema = vec![
        test_field_schema("content_hash", BasicValueType::Str, false),
        test_field_schema("file_path", BasicValueType::Str, false),
    ];

    let value_schema = vec![
        test_field_schema("symbol_name", BasicValueType::Str, false),
        test_field_schema("symbol_type", BasicValueType::Str, false),
        test_field_schema("line_number", BasicValueType::Int64, false),
    ];

    D1ExportContext::new_with_default_client(
        "benchmark-database".to_string(),
        "code_symbols".to_string(),
        "benchmark-account".to_string(),
        "benchmark-token".to_string(),
        key_schema,
        value_schema,
        metrics,
    )
    .expect("Failed to create benchmark context")
}

/// Benchmark 1: SQL Statement Generation
///
/// Measures overhead of building UPSERT/DELETE SQL statements.
fn bench_statement_generation(c: &mut Criterion) {
    let mut group = c.benchmark_group("statement_generation");

    let context = create_benchmark_context();

    let test_key = KeyValue(Box::new([
        KeyPart::Str("abc123def456".into()),
        KeyPart::Str("src/main.rs".into()),
    ]));

    let test_values = FieldValues {
        fields: vec![
            recoco::base::value::Value::Basic(BasicValue::Str("main".into())),
            recoco::base::value::Value::Basic(BasicValue::Str("function".into())),
            recoco::base::value::Value::Basic(BasicValue::Int64(42)),
        ],
    };

    group.bench_function("build_upsert_statement", |b| {
        b.iter(|| {
            let _ = black_box(context.build_upsert_stmt(&test_key, &test_values));
        });
    });

    group.bench_function("build_delete_statement", |b| {
        b.iter(|| {
            let _ = black_box(context.build_delete_stmt(&test_key));
        });
    });

    // Benchmark batch statement generation
    group.bench_function("build_10_upsert_statements", |b| {
        let keys_values: Vec<_> = (0..10)
            .map(|i| {
                let key = KeyValue(Box::new([
                    KeyPart::Str(format!("hash{:08x}", i).into()),
                    KeyPart::Str(format!("src/file{}.rs", i).into()),
                ]));
                let values = test_values.clone();
                (key, values)
            })
            .collect();

        b.iter(|| {
            for (key, values) in &keys_values {
                let _ = black_box(context.build_upsert_stmt(key, values));
            }
        });
    });

    group.finish();
}

/// Benchmark 2: Cache Operations
///
/// Measures cache lookup and insertion performance.
#[cfg(feature = "caching")]
fn bench_cache_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("cache_operations");

    let context = create_benchmark_context();
    let runtime = tokio::runtime::Runtime::new().unwrap();

    // Warm cache with entries
    runtime.block_on(async {
        for i in 0..100 {
            let key = format!("warm{:08x}", i);
            context
                .query_cache
                .insert(key, serde_json::json!({"value": i}))
                .await;
        }
    });

    group.bench_function("cache_hit_lookup", |b| {
        b.iter(|| {
            runtime.block_on(async {
                let _ = black_box(context.query_cache.get(&"warm00000000".to_string()).await);
            });
        });
    });

    group.bench_function("cache_miss_lookup", |b| {
        b.iter(|| {
            runtime.block_on(async {
                let _ = black_box(context.query_cache.get(&"nonexistent".to_string()).await);
            });
        });
    });

    group.bench_function("cache_insert", |b| {
        let mut counter = 0u64;
        b.iter(|| {
            runtime.block_on(async {
                let key = format!("insert{:016x}", counter);
                counter += 1;
                context
                    .query_cache
                    .insert(key, serde_json::json!({"value": counter}))
                    .await;
            });
        });
    });

    group.bench_function("cache_stats_retrieval", |b| {
        b.iter(|| {
            runtime.block_on(async {
                let _ = black_box(context.cache_stats().await);
            });
        });
    });

    group.bench_function("cache_entry_count", |b| {
        b.iter(|| {
            let _ = black_box(context.query_cache.entry_count());
        });
    });

    group.finish();
}

/// Benchmark 3: Performance Metrics Tracking
///
/// Measures overhead of metrics collection.
fn bench_metrics_tracking(c: &mut Criterion) {
    let mut group = c.benchmark_group("metrics_tracking");

    let metrics = PerformanceMetrics::new();

    group.bench_function("record_cache_hit", |b| {
        b.iter(|| {
            metrics.record_cache_hit();
        });
    });

    group.bench_function("record_cache_miss", |b| {
        b.iter(|| {
            metrics.record_cache_miss();
        });
    });

    group.bench_function("record_query_10ms", |b| {
        b.iter(|| {
            metrics.record_query(Duration::from_millis(10), true);
        });
    });

    group.bench_function("record_query_50ms", |b| {
        b.iter(|| {
            metrics.record_query(Duration::from_millis(50), true);
        });
    });

    group.bench_function("record_query_error", |b| {
        b.iter(|| {
            metrics.record_query(Duration::from_millis(100), false);
        });
    });

    group.bench_function("get_cache_stats", |b| {
        b.iter(|| {
            black_box(metrics.cache_stats());
        });
    });

    group.bench_function("get_query_stats", |b| {
        b.iter(|| {
            black_box(metrics.query_stats());
        });
    });

    group.bench_function("export_prometheus", |b| {
        b.iter(|| {
            black_box(metrics.export_prometheus());
        });
    });

    group.finish();
}

/// Benchmark 4: Context Creation Overhead
///
/// Measures D1ExportContext initialization performance.
fn bench_context_creation(c: &mut Criterion) {
    let mut group = c.benchmark_group("context_creation");

    let key_schema = vec![
        test_field_schema("content_hash", BasicValueType::Str, false),
        test_field_schema("file_path", BasicValueType::Str, false),
    ];

    let value_schema = vec![
        test_field_schema("symbol_name", BasicValueType::Str, false),
        test_field_schema("symbol_type", BasicValueType::Str, false),
        test_field_schema("line_number", BasicValueType::Int64, false),
    ];

    group.bench_function("create_d1_context", |b| {
        b.iter(|| {
            let metrics = PerformanceMetrics::new();
            let _ = black_box(D1ExportContext::new_with_default_client(
                "benchmark-database".to_string(),
                "code_symbols".to_string(),
                "benchmark-account".to_string(),
                "benchmark-token".to_string(),
                key_schema.clone(),
                value_schema.clone(),
                metrics,
            ));
        });
    });

    group.bench_function("create_performance_metrics", |b| {
        b.iter(|| {
            let _ = black_box(PerformanceMetrics::new());
        });
    });

    group.finish();
}

/// Benchmark 5: Value Conversion Performance
///
/// Measures JSON conversion overhead for D1 API calls.
fn bench_value_conversion(c: &mut Criterion) {
    let mut group = c.benchmark_group("value_conversion");

    use thread_flow::targets::d1::{basic_value_to_json, key_part_to_json, value_to_json};

    let test_str_value = BasicValue::Str("test_string".into());
    let test_int_value = BasicValue::Int64(42);
    let test_bool_value = BasicValue::Bool(true);

    group.bench_function("basic_value_to_json_str", |b| {
        b.iter(|| {
            let _ = black_box(basic_value_to_json(&test_str_value));
        });
    });

    group.bench_function("basic_value_to_json_int", |b| {
        b.iter(|| {
            let _ = black_box(basic_value_to_json(&test_int_value));
        });
    });

    group.bench_function("basic_value_to_json_bool", |b| {
        b.iter(|| {
            let _ = black_box(basic_value_to_json(&test_bool_value));
        });
    });

    let test_key_part_str = KeyPart::Str("test_key".into());
    let test_key_part_int = KeyPart::Int64(123456);

    group.bench_function("key_part_to_json_str", |b| {
        b.iter(|| {
            let _ = black_box(key_part_to_json(&test_key_part_str));
        });
    });

    group.bench_function("key_part_to_json_int", |b| {
        b.iter(|| {
            let _ = black_box(key_part_to_json(&test_key_part_int));
        });
    });

    let test_value = recoco::base::value::Value::Basic(BasicValue::Str("test".into()));

    group.bench_function("value_to_json", |b| {
        b.iter(|| {
            let _ = black_box(value_to_json(&test_value));
        });
    });

    group.finish();
}

/// Benchmark 6: HTTP Connection Pool Performance
///
/// Validates connection pool efficiency from Task #59.
fn bench_http_pool_performance(c: &mut Criterion) {
    let mut group = c.benchmark_group("http_pool_performance");

    // Create shared HTTP client with connection pooling
    let http_client = Arc::new(
        reqwest::Client::builder()
            .pool_max_idle_per_host(10)
            .pool_idle_timeout(Some(Duration::from_secs(90)))
            .tcp_keepalive(Some(Duration::from_secs(60)))
            .http2_keep_alive_interval(Some(Duration::from_secs(30)))
            .timeout(Duration::from_secs(30))
            .build()
            .expect("Failed to create HTTP client"),
    );

    // Benchmark context creation with shared client
    group.bench_function("create_context_with_shared_client", |b| {
        let metrics = PerformanceMetrics::new();
        let key_schema = vec![test_field_schema("id", BasicValueType::Int64, false)];
        let value_schema = vec![test_field_schema("data", BasicValueType::Str, false)];

        b.iter(|| {
            let client = Arc::clone(&http_client);
            let _ = black_box(D1ExportContext::new(
                "test-db".to_string(),
                "test_table".to_string(),
                "test-account".to_string(),
                "test-token".to_string(),
                client,
                key_schema.clone(),
                value_schema.clone(),
                metrics.clone(),
            ));
        });
    });

    // Benchmark Arc cloning overhead (should be negligible)
    group.bench_function("arc_clone_http_client", |b| {
        b.iter(|| {
            let _ = black_box(Arc::clone(&http_client));
        });
    });

    // Create 10 contexts sharing the same pool
    group.bench_function("create_10_contexts_shared_pool", |b| {
        b.iter(|| {
            let contexts: Vec<_> = (0..10)
                .map(|i| {
                    let metrics = PerformanceMetrics::new();
                    let key_schema = vec![test_field_schema("id", BasicValueType::Int64, false)];
                    let value_schema = vec![test_field_schema("data", BasicValueType::Str, false)];
                    let client = Arc::clone(&http_client);

                    D1ExportContext::new(
                        format!("db-{}", i),
                        format!("table_{}", i),
                        "account".to_string(),
                        "token".to_string(),
                        client,
                        key_schema,
                        value_schema,
                        metrics,
                    )
                    .expect("Failed to create context")
                })
                .collect();
            black_box(contexts)
        });
    });

    group.finish();
}

/// Benchmark 7: End-to-End Query Pipeline
///
/// Simulates complete D1 query pipeline with cache integration.
#[cfg(feature = "caching")]
fn bench_e2e_query_pipeline(c: &mut Criterion) {
    let mut group = c.benchmark_group("e2e_query_pipeline");

    let context = create_benchmark_context();
    let runtime = tokio::runtime::Runtime::new().unwrap();

    // Create test data
    let test_entries: Vec<_> = (0..100)
        .map(|i| {
            let key = KeyValue(Box::new([
                KeyPart::Str(format!("hash{:08x}", i).into()),
                KeyPart::Str(format!("src/file{}.rs", i).into()),
            ]));
            let values = FieldValues {
                fields: vec![
                    recoco::base::value::Value::Basic(BasicValue::Str(
                        format!("func_{}", i).into(),
                    )),
                    recoco::base::value::Value::Basic(BasicValue::Str("function".into())),
                    recoco::base::value::Value::Basic(BasicValue::Int64(i as i64)),
                ],
            };
            (key, values)
        })
        .collect();

    // Warm cache with all entries
    runtime.block_on(async {
        for (i, (key, values)) in test_entries.iter().enumerate() {
            let query_key = format!("query_{:08x}", i);
            let result = serde_json::json!({
                "key": format!("{:?}", key),
                "values": format!("{:?}", values),
            });
            context.query_cache.insert(query_key, result).await;
        }
    });

    // Benchmark: Cache hit path (optimal scenario)
    group.bench_function("pipeline_cache_hit_100_percent", |b| {
        let mut idx = 0;
        b.iter(|| {
            runtime.block_on(async {
                // 1. Check cache (should hit)
                let query_key = format!("query_{:08x}", idx % 100);
                let cached = context.query_cache.get(&query_key).await;
                black_box(cached);
                idx += 1;
            });
        });
    });

    // Benchmark: Cache miss path (worst case)
    group.bench_function("pipeline_cache_miss", |b| {
        let mut idx = 0;
        b.iter(|| {
            runtime.block_on(async {
                let (key, values) = &test_entries[idx % 100];

                // 1. Check cache (will miss)
                let query_key = format!("miss_{:08x}", idx);
                let cached = context.query_cache.get(&query_key).await;

                if cached.is_none() {
                    // 2. Build SQL statement
                    let stmt = context.build_upsert_stmt(key, values);
                    let _ = black_box(stmt);

                    // 3. Would execute HTTP request here (simulated)
                    // 4. Cache result
                    let result = serde_json::json!({"simulated": true});
                    context.query_cache.insert(query_key, result).await;
                }
                idx += 1;
            });
        });
    });

    // Benchmark: 90/10 cache hit/miss ratio (constitutional target)
    group.bench_function("pipeline_90_percent_cache_hit", |b| {
        let mut idx = 0;
        b.iter(|| {
            runtime.block_on(async {
                let (key, values) = &test_entries[idx % 100];

                // 90% of requests use cached queries, 10% are new
                let query_key = if idx % 10 == 0 {
                    format!("new_{:08x}", idx) // Cache miss (10%)
                } else {
                    format!("query_{:08x}", idx % 100) // Cache hit (90%)
                };

                let cached = context.query_cache.get(&query_key).await;

                if cached.is_none() {
                    let stmt = context.build_upsert_stmt(key, values);
                    let _ = black_box(stmt);
                    let result = serde_json::json!({"simulated": true});
                    context.query_cache.insert(query_key, result).await;
                }
                idx += 1;
            });
        });
    });

    group.finish();
}

/// Benchmark 8: Batch Operation Performance
///
/// Measures bulk operation efficiency for realistic workloads.
fn bench_batch_operations(c: &mut Criterion) {
    let mut group = c.benchmark_group("batch_operations");

    let context = create_benchmark_context();

    // Create batch test data
    let batch_10: Vec<_> = (0..10).map(create_test_entry).collect();
    let batch_100: Vec<_> = (0..100).map(create_test_entry).collect();
    let batch_1000: Vec<_> = (0..1000).map(create_test_entry).collect();

    group.bench_function("batch_upsert_10_entries", |b| {
        b.iter(|| {
            for (key, values) in &batch_10 {
                let _ = black_box(context.build_upsert_stmt(key, values));
            }
        });
    });

    group.bench_function("batch_upsert_100_entries", |b| {
        b.iter(|| {
            for (key, values) in &batch_100 {
                let _ = black_box(context.build_upsert_stmt(key, values));
            }
        });
    });

    group.bench_function("batch_upsert_1000_entries", |b| {
        b.iter(|| {
            for (key, values) in &batch_1000 {
                let _ = black_box(context.build_upsert_stmt(key, values));
            }
        });
    });

    group.bench_function("batch_delete_10_entries", |b| {
        b.iter(|| {
            for (key, _) in &batch_10 {
                let _ = black_box(context.build_delete_stmt(key));
            }
        });
    });

    group.bench_function("batch_delete_100_entries", |b| {
        b.iter(|| {
            for (key, _) in &batch_100 {
                let _ = black_box(context.build_delete_stmt(key));
            }
        });
    });

    group.finish();
}

/// Helper function to create test entry
fn create_test_entry(idx: usize) -> (KeyValue, FieldValues) {
    let key = KeyValue(Box::new([
        KeyPart::Str(format!("hash{:08x}", idx).into()),
        KeyPart::Str(format!("src/file{}.rs", idx).into()),
    ]));
    let values = FieldValues {
        fields: vec![
            recoco::base::value::Value::Basic(BasicValue::Str(format!("symbol_{}", idx).into())),
            recoco::base::value::Value::Basic(BasicValue::Str("function".into())),
            recoco::base::value::Value::Basic(BasicValue::Int64(idx as i64)),
        ],
    };
    (key, values)
}

/// Benchmark 9: P95 Latency Validation
///
/// Validates constitutional requirement: D1 p95 latency <50ms
#[cfg(feature = "caching")]
fn bench_p95_latency_validation(c: &mut Criterion) {
    let mut group = c.benchmark_group("p95_latency_validation");
    group.sample_size(1000); // Larger sample for accurate p95 calculation

    let context = create_benchmark_context();
    let runtime = tokio::runtime::Runtime::new().unwrap();

    // Warm cache
    runtime.block_on(async {
        for i in 0..1000 {
            let query_key = format!("warm{:08x}", i);
            context
                .query_cache
                .insert(query_key, serde_json::json!({"value": i}))
                .await;
        }
    });

    // Simulate realistic workload: mostly cache hits with some misses
    group.bench_function("realistic_workload_p95", |b| {
        let mut idx = 0;
        b.iter(|| {
            runtime.block_on(async {
                // 95% cache hits, 5% misses (better than constitutional 90% target)
                let query_key = if idx % 20 == 0 {
                    format!("miss{:08x}", idx)
                } else {
                    format!("warm{:08x}", idx % 1000)
                };

                let result = context.query_cache.get(&query_key).await;

                if result.is_none() {
                    // Simulate query execution overhead
                    let (key, values) = create_test_entry(idx);
                    let stmt = context.build_upsert_stmt(&key, &values);
                    let _ = black_box(stmt);
                    context
                        .query_cache
                        .insert(query_key, serde_json::json!({"new": true}))
                        .await;
                }

                idx += 1;
            });
        });
    });

    group.finish();
}

// Benchmark groups
criterion_group!(
    benches,
    bench_statement_generation,
    bench_metrics_tracking,
    bench_context_creation,
    bench_value_conversion,
    bench_http_pool_performance,
    bench_batch_operations,
);

#[cfg(feature = "caching")]
criterion_group!(
    cache_benches,
    bench_cache_operations,
    bench_e2e_query_pipeline,
    bench_p95_latency_validation,
);

// Main benchmark runner
#[cfg(feature = "caching")]
criterion_main!(benches, cache_benches);

#[cfg(not(feature = "caching"))]
criterion_main!(benches);