oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! Query Profiling System
//!
//! Advanced profiling and performance analysis for SPARQL queries.

use super::{ToolResult, ToolStats};
use std::path::PathBuf;
use std::time::{Duration, Instant};

/// Configuration for query profiling
pub struct ProfilingConfig {
    pub query: Option<String>,
    pub query_file: Option<PathBuf>,
    pub data: Vec<PathBuf>,
    pub iterations: usize,
    pub warmup: usize,
    pub memory_profile: bool,
    pub output_format: String,
}

/// Profile information for a query execution
#[derive(Debug, Clone)]
pub struct QueryProfile {
    pub total_time: Duration,
    pub parsing_time: Duration,
    pub optimization_time: Duration,
    pub execution_time: Duration,
    pub result_materialization_time: Duration,
    pub memory_used: usize,
    pub intermediate_results: usize,
    pub final_results: usize,
    pub phases: Vec<PhaseProfile>,
}

/// Profile information for a query execution phase
#[derive(Debug, Clone)]
pub struct PhaseProfile {
    pub name: String,
    pub duration: Duration,
    pub memory_delta: isize,
    pub results_count: usize,
}

/// Statistics from multiple profiling runs
#[derive(Debug)]
pub struct ProfilingStats {
    pub runs: Vec<QueryProfile>,
    pub mean_time: Duration,
    pub median_time: Duration,
    pub min_time: Duration,
    pub max_time: Duration,
    pub std_dev: Duration,
    pub throughput: f64, // queries per second
}

/// Run query profiling
pub async fn run(config: ProfilingConfig) -> ToolResult {
    let mut stats = ToolStats::new();

    println!("SPARQL Query Profiler");
    println!("====================\n");

    // Get query string
    let query_string = if let Some(q) = config.query {
        q
    } else if let Some(ref path) = config.query_file {
        std::fs::read_to_string(path)?
    } else {
        return Err("Must specify either --query or --query-file".into());
    };

    println!("Query:");
    println!("---");
    println!("{query_string}");
    println!("---\n");

    // Warmup runs
    if config.warmup > 0 {
        println!("Warmup: {} iteration(s)...", config.warmup);
        for i in 0..config.warmup {
            let profile = profile_query_execution(&query_string, &config.data)?;
            println!("  Warmup {}: {:?}", i + 1, profile.total_time);
        }
        println!();
    }

    // Profiling runs
    println!("Profiling: {} iteration(s)...", config.iterations);
    let mut profiles = Vec::new();

    for i in 0..config.iterations {
        let profile = profile_query_execution(&query_string, &config.data)?;
        println!(
            "  Run {}: {:?} ({} results)",
            i + 1,
            profile.total_time,
            profile.final_results
        );
        profiles.push(profile);
    }

    // Calculate statistics
    let profiling_stats = calculate_statistics(&profiles);

    // Display results
    println!("\n=== Profiling Results ===\n");
    display_statistics(&profiling_stats, &config.output_format)?;

    if config.memory_profile {
        display_memory_profile(&profiles)?;
    }

    display_bottlenecks(&profiles)?;
    display_recommendations(&profiling_stats)?;

    stats.items_processed = config.iterations;
    stats.finish();
    stats.print_summary("Profiler");

    Ok(())
}

/// Profile a single query execution
fn profile_query_execution(query: &str, _data_sources: &[PathBuf]) -> ToolResult<QueryProfile> {
    let start = Instant::now();

    // Phase 1: Parsing
    let parse_start = Instant::now();
    let _parsed_query = parse_query(query)?;
    let parsing_time = parse_start.elapsed();

    // Phase 2: Optimization
    let opt_start = Instant::now();
    let _optimized_query = optimize_query_plan(query)?;
    let optimization_time = opt_start.elapsed();

    // Phase 3: Execution
    let exec_start = Instant::now();
    let intermediate_count = execute_query(query)?;
    let execution_time = exec_start.elapsed();

    // Phase 4: Result materialization
    let mat_start = Instant::now();
    let final_count = materialize_results()?;
    let result_materialization_time = mat_start.elapsed();

    let total_time = start.elapsed();

    // Build phase profiles
    let phases = vec![
        PhaseProfile {
            name: "Parsing".to_string(),
            duration: parsing_time,
            memory_delta: 0,
            results_count: 0,
        },
        PhaseProfile {
            name: "Optimization".to_string(),
            duration: optimization_time,
            memory_delta: 0,
            results_count: 0,
        },
        PhaseProfile {
            name: "Execution".to_string(),
            duration: execution_time,
            memory_delta: 0,
            results_count: intermediate_count,
        },
        PhaseProfile {
            name: "Materialization".to_string(),
            duration: result_materialization_time,
            memory_delta: 0,
            results_count: final_count,
        },
    ];

    Ok(QueryProfile {
        total_time,
        parsing_time,
        optimization_time,
        execution_time,
        result_materialization_time,
        memory_used: 0, // Would track actual memory in real implementation
        intermediate_results: intermediate_count,
        final_results: final_count,
        phases,
    })
}

/// Parse query (simulated)
fn parse_query(_query: &str) -> ToolResult<String> {
    // Simulate parsing time
    std::thread::sleep(Duration::from_micros(100));
    Ok(String::from("parsed"))
}

/// Optimize query plan (simulated)
fn optimize_query_plan(_query: &str) -> ToolResult<String> {
    // Simulate optimization time
    std::thread::sleep(Duration::from_micros(200));
    Ok(String::from("optimized"))
}

/// Execute query (simulated)
fn execute_query(_query: &str) -> ToolResult<usize> {
    // Simulate execution time
    std::thread::sleep(Duration::from_millis(5));
    Ok(100) // Intermediate results count
}

/// Materialize results (simulated)
fn materialize_results() -> ToolResult<usize> {
    // Simulate materialization time
    std::thread::sleep(Duration::from_micros(500));
    Ok(10) // Final results count
}

/// Calculate profiling statistics
fn calculate_statistics(profiles: &[QueryProfile]) -> ProfilingStats {
    let mut times: Vec<Duration> = profiles.iter().map(|p| p.total_time).collect();
    times.sort();

    let total_time: Duration = times.iter().sum();
    let mean_time = total_time / times.len() as u32;

    let median_time = if times.len() % 2 == 0 {
        let mid = times.len() / 2;
        (times[mid - 1] + times[mid]) / 2
    } else {
        times[times.len() / 2]
    };

    let min_time = *times.first().expect("collection validated to be non-empty");
    let max_time = *times.last().expect("collection validated to be non-empty");

    // Calculate standard deviation
    let variance: f64 = times
        .iter()
        .map(|&t| {
            let diff = t.as_secs_f64() - mean_time.as_secs_f64();
            diff * diff
        })
        .sum::<f64>()
        / times.len() as f64;
    let std_dev = Duration::from_secs_f64(variance.sqrt());

    // Calculate throughput (queries per second)
    let throughput = times.len() as f64 / total_time.as_secs_f64();

    ProfilingStats {
        runs: profiles.to_vec(),
        mean_time,
        median_time,
        min_time,
        max_time,
        std_dev,
        throughput,
    }
}

/// Display profiling statistics
fn display_statistics(stats: &ProfilingStats, format: &str) -> ToolResult<()> {
    match format {
        "table" => display_stats_table(stats),
        "json" => display_stats_json(stats),
        _ => display_stats_table(stats),
    }
}

/// Display statistics as table
fn display_stats_table(stats: &ProfilingStats) -> ToolResult<()> {
    println!("Execution Time Statistics:");
    println!("  Runs:      {}", stats.runs.len());
    println!("  Mean:      {:?}", stats.mean_time);
    println!("  Median:    {:?}", stats.median_time);
    println!("  Min:       {:?}", stats.min_time);
    println!("  Max:       {:?}", stats.max_time);
    println!("  Std Dev:   {:?}", stats.std_dev);
    println!("  Throughput: {:.2} queries/sec", stats.throughput);

    if !stats.runs.is_empty() {
        let first = &stats.runs[0];
        println!("\nPhase Breakdown (first run):");
        println!("  Parsing:          {:?}", first.parsing_time);
        println!("  Optimization:     {:?}", first.optimization_time);
        println!("  Execution:        {:?}", first.execution_time);
        println!(
            "  Materialization:  {:?}",
            first.result_materialization_time
        );
    }

    Ok(())
}

/// Display statistics as JSON
fn display_stats_json(stats: &ProfilingStats) -> ToolResult<()> {
    println!("{{");
    println!("  \"runs\": {},", stats.runs.len());
    println!("  \"mean_ms\": {},", stats.mean_time.as_millis());
    println!("  \"median_ms\": {},", stats.median_time.as_millis());
    println!("  \"min_ms\": {},", stats.min_time.as_millis());
    println!("  \"max_ms\": {},", stats.max_time.as_millis());
    println!("  \"std_dev_ms\": {},", stats.std_dev.as_millis());
    println!("  \"throughput_qps\": {:.2}", stats.throughput);

    if !stats.runs.is_empty() {
        let first = &stats.runs[0];
        println!("  \"phases\": {{");
        println!("    \"parsing_ms\": {},", first.parsing_time.as_millis());
        println!(
            "    \"optimization_ms\": {},",
            first.optimization_time.as_millis()
        );
        println!(
            "    \"execution_ms\": {},",
            first.execution_time.as_millis()
        );
        println!(
            "    \"materialization_ms\": {}",
            first.result_materialization_time.as_millis()
        );
        println!("  }}");
    }

    println!("}}");
    Ok(())
}

/// Display memory profiling information
fn display_memory_profile(profiles: &[QueryProfile]) -> ToolResult<()> {
    println!("\n=== Memory Profile ===\n");

    if profiles.is_empty() {
        println!("No profiling data available");
        return Ok(());
    }

    let avg_memory: usize = profiles.iter().map(|p| p.memory_used).sum::<usize>() / profiles.len();
    let max_memory = profiles.iter().map(|p| p.memory_used).max().unwrap_or(0);

    println!("Memory Usage:");
    println!("  Average: {} KB", avg_memory / 1024);
    println!("  Maximum: {} KB", max_memory / 1024);

    // Show memory by phase (from first run)
    let first = &profiles[0];
    println!("\nMemory by Phase:");
    for phase in &first.phases {
        let delta_str = if phase.memory_delta >= 0 {
            format!("+{} KB", phase.memory_delta / 1024)
        } else {
            format!("{} KB", phase.memory_delta / 1024)
        };
        println!("  {}: {}", phase.name, delta_str);
    }

    Ok(())
}

/// Display performance bottlenecks
fn display_bottlenecks(profiles: &[QueryProfile]) -> ToolResult<()> {
    println!("\n=== Performance Bottlenecks ===\n");

    if profiles.is_empty() {
        println!("No profiling data available");
        return Ok(());
    }

    // Analyze first run for bottlenecks
    let profile = &profiles[0];

    // Calculate percentage of time spent in each phase
    let total_micros = profile.total_time.as_micros();
    let mut phase_percentages: Vec<(String, f64)> = profile
        .phases
        .iter()
        .map(|phase| {
            let percent = (phase.duration.as_micros() as f64 / total_micros as f64) * 100.0;
            (phase.name.clone(), percent)
        })
        .collect();

    phase_percentages.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

    println!("Time Distribution:");
    for (name, percent) in &phase_percentages {
        let bar_len = (*percent as usize).min(50);
        let bar = "".repeat(bar_len);
        println!("  {:<20} {:>6.2}% {}", name, percent, bar);
    }

    // Identify bottlenecks (phases taking >30% of time)
    println!("\nIdentified Bottlenecks:");
    let mut found_bottleneck = false;
    for (name, percent) in &phase_percentages {
        if *percent > 30.0 {
            println!("{}: {:.1}% of execution time", name, percent);
            found_bottleneck = true;
        }
    }

    if !found_bottleneck {
        println!("  ✓ No major bottlenecks detected");
    }

    Ok(())
}

/// Display performance recommendations
fn display_recommendations(stats: &ProfilingStats) -> ToolResult<()> {
    println!("\n=== Recommendations ===\n");

    let mut recommendations = Vec::new();

    if stats.runs.is_empty() {
        println!("No profiling data for recommendations");
        return Ok(());
    }

    let first = &stats.runs[0];

    // Check execution time
    if first.execution_time > Duration::from_millis(100) {
        recommendations
            .push("Execution phase is slow - consider adding indexes or optimizing query patterns");
    }

    // Check parsing time
    if first.parsing_time > Duration::from_millis(10) {
        recommendations.push("Query parsing is slow - consider simplifying query syntax");
    }

    // Check intermediate results
    if first.intermediate_results > 10000 {
        recommendations.push(
            "Large number of intermediate results - add FILTER or LIMIT clauses earlier in query",
        );
    }

    // Check variability
    let variability = stats.std_dev.as_secs_f64() / stats.mean_time.as_secs_f64();
    if variability > 0.2 {
        recommendations.push("High execution time variability - query performance may be unstable");
    }

    // Check throughput
    if stats.throughput < 1.0 {
        recommendations.push("Low throughput - consider query optimization or hardware upgrades");
    }

    if recommendations.is_empty() {
        println!("✓ Query performance looks good - no recommendations");
    } else {
        for (i, rec) in recommendations.iter().enumerate() {
            println!("{}. {}", i + 1, rec);
        }
    }

    Ok(())
}