vibe_coding_tracker 2.2.2

Vibe Coding Tracker - AI coding assistant telemetry/usage parser, aggregate local session data into CodeAnalysis results
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
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use ratatui::{Terminal, backend::TestBackend};
use std::hint::black_box;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tempfile::TempDir;
use vibe_coding_tracker::cli::TimeRange;
use vibe_coding_tracker::config::ProvidersConfig;
use vibe_coding_tracker::constants::FastHashMap;
use vibe_coding_tracker::display::common::render_loading_frame;
use vibe_coding_tracker::display::usage::UsageFrameBenchmark;
use vibe_coding_tracker::models::ExtensionType;
use vibe_coding_tracker::pricing::{ModelPricingMap, normalize_model_name};
use vibe_coding_tracker::session::{
    ParseMode, parse_session_file_as, parse_session_file_typed_with_mode,
};
use vibe_coding_tracker::summary_cache::{SummaryScanCache, build_scan_pool};
use vibe_coding_tracker::usage::calculator::get_usage_from_paths_with_cache;
use vibe_coding_tracker::utils::{HelperPaths, resolve_paths_from_home};

/// Absolute path to a file under the repo's `tests/fixtures/` directory.
fn fixture(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join(name)
}

// ========== Pricing & String Operations ==========

fn benchmark_normalize_model_name(c: &mut Criterion) {
    c.bench_function("normalize_model_name simple", |b| {
        b.iter(|| normalize_model_name(black_box("claude-3-sonnet-20240229")))
    });

    c.bench_function("normalize_model_name with prefix", |b| {
        b.iter(|| normalize_model_name(black_box("bedrock/claude-3-opus-20240229")))
    });

    c.bench_function("normalize_model_name complex", |b| {
        b.iter(|| normalize_model_name(black_box("openai/gpt-4-turbo-20240409-v1.5")))
    });
}

fn benchmark_pricing_lookup(c: &mut Criterion) {
    use std::collections::HashMap;

    // Create a mock pricing map (ModelPricingMap requires std HashMap)
    let mut pricing_data = HashMap::new();
    pricing_data.insert(
        "claude-3-sonnet".to_string(),
        vibe_coding_tracker::pricing::ModelPricing::default(),
    );
    pricing_data.insert(
        "gpt-4-turbo".to_string(),
        vibe_coding_tracker::pricing::ModelPricing::default(),
    );
    pricing_data.insert(
        "gemini-pro".to_string(),
        vibe_coding_tracker::pricing::ModelPricing::default(),
    );
    pricing_data.insert(
        "copilot-gpt-4".to_string(),
        vibe_coding_tracker::pricing::ModelPricing::default(),
    );

    let pricing_map = ModelPricingMap::new(pricing_data);

    c.bench_function("pricing lookup exact match", |b| {
        b.iter(|| pricing_map.get(black_box("claude-3-sonnet")))
    });

    c.bench_function("pricing lookup normalized", |b| {
        b.iter(|| pricing_map.get(black_box("claude-3-sonnet-20240229")))
    });

    c.bench_function("pricing lookup fuzzy", |b| {
        b.iter(|| pricing_map.get(black_box("claude-sonnet-3")))
    });
}

fn benchmark_line_counting(c: &mut Criterion) {
    let short_text = "line1\nline2\nline3\n";
    let medium_text = (0..100).map(|i| format!("line{}\n", i)).collect::<String>();
    let long_text = (0..10000)
        .map(|i| format!("line{}\n", i))
        .collect::<String>();

    c.bench_function("count_lines short (3 lines)", |b| {
        b.iter(|| vibe_coding_tracker::utils::count_lines(black_box(short_text)))
    });

    c.bench_function("count_lines medium (100 lines)", |b| {
        b.iter(|| vibe_coding_tracker::utils::count_lines(black_box(&medium_text)))
    });

    c.bench_function("count_lines long (10k lines)", |b| {
        b.iter(|| vibe_coding_tracker::utils::count_lines(black_box(&long_text)))
    });
}

// ========== File Parsing Benchmarks ==========

fn benchmark_file_parsing(c: &mut Criterion) {
    let mut group = c.benchmark_group("file_parsing");

    // Test files paths
    let test_files = vec![
        ("claude", "sessions/claude_code.jsonl"),
        ("codex", "sessions/codex.jsonl"),
        ("copilot", "sessions/copilot.jsonl"),
        ("gemini", "sessions/gemini.jsonl"),
        ("grok", "sessions/grok/signals.json"),
    ];

    for (name, path) in test_files {
        let path_buf = fixture(path);
        if path_buf.exists() {
            group.bench_with_input(
                BenchmarkId::new("parse_session_file", name),
                &path_buf,
                |b, p| b.iter(|| vibe_coding_tracker::session::parse_session_file(black_box(p))),
            );
        }
    }

    group.finish();
}

fn benchmark_known_provider_parsing(c: &mut Criterion) {
    let mut group = c.benchmark_group("known_provider_parse");
    let fixtures = [
        (
            "claude",
            "sessions/claude_code.jsonl",
            ExtensionType::ClaudeCode,
        ),
        ("codex", "sessions/codex.jsonl", ExtensionType::Codex),
        ("copilot", "sessions/copilot.jsonl", ExtensionType::Copilot),
        ("gemini", "sessions/gemini.jsonl", ExtensionType::Gemini),
        ("grok", "sessions/grok/signals.json", ExtensionType::Grok),
    ];

    for (name, path, provider) in fixtures {
        let path = fixture(path);
        if !path.exists() {
            continue;
        }
        for (mode_name, mode) in [
            ("usage_only", ParseMode::UsageOnly),
            ("full", ParseMode::Full),
        ] {
            group.bench_function(BenchmarkId::new(name, mode_name), |b| {
                b.iter(|| {
                    black_box(
                        parse_session_file_as(black_box(&path), provider, mode)
                            .expect("parse known-provider benchmark fixture"),
                    )
                })
            });
        }
    }

    group.finish();
}

fn benchmark_long_preamble_detection(c: &mut Criterion) {
    let temp = TempDir::new().expect("create long-preamble benchmark directory");
    let path = temp.path().join("session.jsonl");
    let sentinel = r#"{"type":"file-history-snapshot","messageId":"m1","isSnapshotUpdate":false,"snapshot":{}}"#;
    let assistant = r#"{"type":"assistant","sessionId":"sess-1","parentUuid":"pu","timestamp":"2026-04-23T00:00:00.000Z","message":{"model":"claude-opus-4-7","usage":{"input_tokens":100,"output_tokens":50},"content":[]}}"#;
    let mut body = String::with_capacity((sentinel.len() + 1) * 10_000 + assistant.len() + 1);
    for _ in 0..10_000 {
        body.push_str(sentinel);
        body.push('\n');
    }
    body.push_str(assistant);
    body.push('\n');
    std::fs::write(&path, body).expect("write long-preamble benchmark fixture");

    let mut group = c.benchmark_group("single_file_detection");
    group.sample_size(10);
    group.bench_function("claude_after_10000_metadata_records", |b| {
        b.iter(|| {
            black_box(
                parse_session_file_typed_with_mode(black_box(&path), ParseMode::UsageOnly)
                    .expect("parse long-preamble benchmark fixture"),
            )
        })
    });
    group.finish();
}

fn claude_only() -> ProvidersConfig {
    ProvidersConfig {
        claude: true,
        codex: false,
        copilot: false,
        gemini: false,
        opencode: false,
        cursor: false,
        hermes: false,
        grok: false,
    }
}

fn build_scan_corpus(source_count: usize, fixture: &str) -> (TempDir, HelperPaths, PathBuf) {
    let temp = TempDir::new().expect("create scan benchmark directory");
    let paths = resolve_paths_from_home(temp.path());
    let project = paths.claude_session_dir.join("benchmark");
    std::fs::create_dir_all(&project).expect("create scan benchmark project");
    for index in 0..source_count {
        std::fs::write(project.join(format!("session-{index}.jsonl")), fixture)
            .expect("write scan benchmark fixture");
    }
    let changed = project.join("session-0.jsonl");
    (temp, paths, changed)
}

fn run_cached_scan(pool: &rayon::ThreadPool, paths: &HelperPaths, cache: &mut SummaryScanCache) {
    let result = pool
        .install(|| get_usage_from_paths_with_cache(paths, TimeRange::All, claude_only(), cache));
    black_box(result.expect("scan benchmark corpus"));
}

fn benchmark_summary_scan_cache(c: &mut Criterion) {
    let fixture = std::fs::read_to_string(fixture("sessions/claude_code.jsonl"))
        .expect("read scan benchmark fixture");
    let (_small_temp, small_paths, changed_path) = build_scan_corpus(100, &fixture);
    let (_large_temp, large_paths, _) = build_scan_corpus(1_000, &fixture);
    let pool = build_scan_pool(2).expect("build scan benchmark pool");

    let mut group = c.benchmark_group("summary_scan_cache");
    group.sample_size(10);
    group.measurement_time(Duration::from_secs(10));

    group.bench_function(BenchmarkId::new("cold", 100), |b| {
        b.iter_batched(
            SummaryScanCache::new,
            |mut cache| run_cached_scan(&pool, &small_paths, &mut cache),
            criterion::BatchSize::SmallInput,
        );
    });
    group.bench_function(BenchmarkId::new("cold", 1_000), |b| {
        b.iter_batched(
            SummaryScanCache::new,
            |mut cache| run_cached_scan(&pool, &large_paths, &mut cache),
            criterion::BatchSize::SmallInput,
        );
    });

    let mut warm_cache = SummaryScanCache::new();
    run_cached_scan(&pool, &small_paths, &mut warm_cache);
    group.bench_function(BenchmarkId::new("unchanged", 100), |b| {
        b.iter(|| {
            run_cached_scan(&pool, &small_paths, &mut warm_cache);
            debug_assert_eq!(warm_cache.stats().parsed_sources, 0);
        });
    });

    group.bench_function(BenchmarkId::new("single_changed", 100), |b| {
        b.iter_custom(|iterations| {
            std::fs::write(&changed_path, &fixture).expect("reset changed benchmark fixture");
            let mut cache = SummaryScanCache::new();
            run_cached_scan(&pool, &small_paths, &mut cache);
            let fixture_with_newline = format!("{fixture}\n");
            let mut elapsed = Duration::ZERO;
            for iteration in 0..iterations {
                let content = if iteration % 2 == 0 {
                    fixture_with_newline.as_str()
                } else {
                    fixture.as_str()
                };
                std::fs::write(&changed_path, content).expect("mutate changed benchmark fixture");
                let started = Instant::now();
                run_cached_scan(&pool, &small_paths, &mut cache);
                elapsed += started.elapsed();
                debug_assert_eq!(cache.stats().parsed_sources, 1);
            }
            elapsed
        });
    });

    group.finish();
}

// ========== Format Detection Benchmarks ==========

fn benchmark_format_detection(c: &mut Criterion) {
    use serde_json::json;
    use vibe_coding_tracker::session::detector::detect_extension_type;

    let claude_data = vec![
        json!({"parentUuid": null, "type": "user", "message": {"role": "user"}}),
        json!({"parentUuid": "abc", "type": "assistant", "message": {"role": "assistant"}}),
    ];

    let codex_data = vec![
        json!({"completion_response": {"usage": {}}, "total_token_usage": {}}),
        json!({"completion_response": {"usage": {}}}),
    ];

    let copilot_data = vec![json!({"sessionId": "test", "startTime": 123, "timeline": []})];

    let gemini_data = vec![json!({"sessionId": "test", "projectHash": "abc", "messages": []})];

    c.bench_function("detect_format claude", |b| {
        b.iter(|| detect_extension_type(black_box(&claude_data)))
    });

    c.bench_function("detect_format codex", |b| {
        b.iter(|| detect_extension_type(black_box(&codex_data)))
    });

    c.bench_function("detect_format copilot", |b| {
        b.iter(|| detect_extension_type(black_box(&copilot_data)))
    });

    c.bench_function("detect_format gemini", |b| {
        b.iter(|| detect_extension_type(black_box(&gemini_data)))
    });
}

// ========== Cache Performance Benchmarks ==========

fn benchmark_cache_operations(c: &mut Criterion) {
    use vibe_coding_tracker::cache::global_cache;

    let test_path = fixture("sessions/claude_code.jsonl");

    if !test_path.exists() {
        return;
    }

    // Warm up cache
    let _ = global_cache().get_or_parse(&test_path);

    c.bench_function("cache hit (warm)", |b| {
        b.iter(|| global_cache().get_or_parse(black_box(&test_path)))
    });

    c.bench_function("cache miss (cold)", |b| {
        b.iter_batched(
            || {
                // Clear cache before each iteration
                global_cache().invalidate(&test_path);
                test_path.clone()
            },
            |path| global_cache().get_or_parse(&path),
            criterion::BatchSize::SmallInput,
        )
    });

    c.bench_function("cache stats", |b| b.iter(|| global_cache().stats()));
}

// ========== HashMap Performance Benchmarks ==========

fn benchmark_hashmap_performance(c: &mut Criterion) {
    use std::collections::HashMap;

    let mut group = c.benchmark_group("hashmap");

    // Prepare test data
    let keys: Vec<String> = (0..1000).map(|i| format!("key_{}", i)).collect();
    let values: Vec<i32> = (0..1000).collect();

    // ahash::AHashMap (FastHashMap)
    group.bench_function("FastHashMap insert 1000", |b| {
        b.iter(|| {
            let mut map = FastHashMap::default();
            for (k, v) in keys.iter().zip(values.iter()) {
                map.insert(k.clone(), *v);
            }
            black_box(map);
        })
    });

    // std::collections::HashMap
    group.bench_function("std HashMap insert 1000", |b| {
        b.iter(|| {
            let mut map = HashMap::new();
            for (k, v) in keys.iter().zip(values.iter()) {
                map.insert(k.clone(), *v);
            }
            black_box(map);
        })
    });

    // Lookup benchmark
    let mut fast_map = FastHashMap::default();
    let mut std_map = HashMap::new();
    for (k, v) in keys.iter().zip(values.iter()) {
        fast_map.insert(k.clone(), *v);
        std_map.insert(k.clone(), *v);
    }

    group.bench_function("FastHashMap lookup 1000", |b| {
        b.iter(|| {
            for key in &keys {
                black_box(fast_map.get(key));
            }
        })
    });

    group.bench_function("std HashMap lookup 1000", |b| {
        b.iter(|| {
            for key in &keys {
                black_box(std_map.get(key));
            }
        })
    });

    group.finish();
}

// ========== Usage Aggregation Benchmarks ==========

fn benchmark_usage_aggregation(c: &mut Criterion) {
    use serde_json::json;
    use vibe_coding_tracker::models::usage::UsageResult;

    c.bench_function("aggregate usage 100 models", |b| {
        b.iter(|| {
            let mut result = UsageResult::default();
            for i in 0..100 {
                let model = format!("model-{}", i % 5);

                let usage = json!({
                    "input_tokens": 1000,
                    "output_tokens": 500,
                    "cache_read_input_tokens": 2000,
                    "cache_creation_input_tokens": 300,
                    "cost_usd": 0.01,
                    "matched_model": format!("matched-model-{}", i % 5)
                });

                result.insert(model, usage);
            }
            black_box(result);
        })
    });
}

// ========== Batch Analysis Benchmarks ==========

fn benchmark_batch_analysis(c: &mut Criterion) {
    // Only run if fixture files exist
    let claude_path = fixture("sessions/claude_code.jsonl");
    let codex_path = fixture("sessions/codex.jsonl");
    let copilot_path = fixture("sessions/copilot.jsonl");
    let gemini_path = fixture("sessions/gemini.jsonl");
    let grok_path = fixture("sessions/grok/signals.json");

    if !claude_path.exists()
        || !codex_path.exists()
        || !copilot_path.exists()
        || !gemini_path.exists()
        || !grok_path.exists()
    {
        return;
    }

    c.bench_function("batch analyze all formats", |b| {
        b.iter(|| {
            // Create temporary directory paths for testing
            let paths = vec![
                (claude_path.clone(), "claude"),
                (codex_path.clone(), "codex"),
                (copilot_path.clone(), "copilot"),
                (gemini_path.clone(), "gemini"),
                (grok_path.clone(), "grok"),
            ];

            // Simulate batch processing
            for (path, _name) in paths {
                let _ = vibe_coding_tracker::session::parse_session_file(black_box(&path));
            }
        })
    });
}

// ========== JSON Serialization Benchmarks ==========

fn benchmark_json_serialization(c: &mut Criterion) {
    use serde_json::json;
    use vibe_coding_tracker::models::usage::UsageResult;

    // Create sample data
    let mut result = UsageResult::default();
    for i in 0..50 {
        let model = format!("claude-sonnet-{}", i % 3);

        let usage = json!({
            "input_tokens": 1000 + i * 10,
            "output_tokens": 500 + i * 5,
            "cache_read_input_tokens": 2000 + i * 20,
            "cache_creation_input_tokens": 300 + i * 3,
            "cost_usd": 0.01 * (i as f64),
            "matched_model": "claude-sonnet"
        });

        result.insert(model, usage);
    }

    c.bench_function("serialize UsageResult", |b| {
        b.iter(|| serde_json::to_string(black_box(&result)))
    });

    c.bench_function("serialize UsageResult pretty", |b| {
        b.iter(|| serde_json::to_string_pretty(black_box(&result)))
    });
}

// ========== TUI Frame Rendering Benchmarks ==========

fn benchmark_tui_frame_render(c: &mut Criterion) {
    let mut group = c.benchmark_group("tui_frame_render");

    let mut loading =
        Terminal::new(TestBackend::new(160, 42)).expect("create loading frame benchmark terminal");
    let mut spinner_index = 0usize;
    group.bench_function("loading", |b| {
        b.iter(|| {
            render_loading_frame(&mut loading, black_box(spinner_index))
                .expect("render loading benchmark frame");
            spinner_index = spinner_index.wrapping_add(1);
        })
    });

    let mut ready =
        UsageFrameBenchmark::new(160, 42).expect("create ready frame benchmark fixture");
    group.bench_function("ready", |b| {
        b.iter(|| {
            ready
                .render(black_box(None))
                .expect("render ready benchmark frame")
        })
    });

    let mut refreshing =
        UsageFrameBenchmark::new(160, 42).expect("create refreshing frame benchmark fixture");
    group.bench_function("refreshing", |b| {
        b.iter(|| {
            refreshing
                .render(black_box(Some("Refreshing...")))
                .expect("render refreshing benchmark frame")
        })
    });

    group.finish();
}

criterion_group!(
    benches,
    benchmark_normalize_model_name,
    benchmark_pricing_lookup,
    benchmark_line_counting,
    benchmark_file_parsing,
    benchmark_known_provider_parsing,
    benchmark_long_preamble_detection,
    benchmark_summary_scan_cache,
    benchmark_format_detection,
    benchmark_cache_operations,
    benchmark_hashmap_performance,
    benchmark_usage_aggregation,
    benchmark_batch_analysis,
    benchmark_json_serialization,
    benchmark_tui_frame_render,
);
criterion_main!(benches);