string_pipeline 0.13.1

A flexible, template-driven string transformation pipeline for Rust.
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
use clap::{Arg, Command};
use std::collections::HashMap;
use std::time::{Duration, Instant};
use string_pipeline::Template;

#[derive(Debug, Clone)]
struct BenchmarkResult {
    name: String,
    iterations: usize,
    average_time: Duration,
    min_time: Duration,
    max_time: Duration,
}

impl BenchmarkResult {
    fn new(name: String, times: Vec<Duration>) -> Self {
        let iterations = times.len();
        let filtered_times = remove_outliers(times.clone());

        let average_time = if filtered_times.is_empty() {
            Duration::from_nanos(0)
        } else {
            let sum: Duration = filtered_times.iter().sum();
            sum / filtered_times.len() as u32
        };

        // Calculate min and max from filtered data (after outlier removal)
        let min_time = filtered_times
            .iter()
            .min()
            .copied()
            .unwrap_or(Duration::from_nanos(0));
        let max_time = filtered_times
            .iter()
            .max()
            .copied()
            .unwrap_or(Duration::from_nanos(0));

        BenchmarkResult {
            name,
            iterations,
            average_time,
            min_time,
            max_time,
        }
    }
}

fn remove_outliers(mut times: Vec<Duration>) -> Vec<Duration> {
    if times.len() < 4 {
        return times;
    }

    times.sort();
    let len = times.len();

    // Remove top and bottom 5% as outliers
    let outlier_count = (len as f64 * 0.05).ceil() as usize;
    let start_idx = outlier_count;
    let end_idx = len - outlier_count;

    let filtered: Vec<Duration> = times[start_idx..end_idx].to_vec();

    filtered
}

struct BenchmarkSuite {
    iterations: usize,
    warmup_iterations: usize,
    test_data: String,
    quiet: bool,
}

impl BenchmarkSuite {
    fn new(iterations: usize, quiet: bool) -> Self {
        // Create some realistic test data
        let test_data = "apple,banana,cherry,date,elderberry,fig,grape,honeydew,ice_fruit,jackfruit,kiwi,lemon,mango,nectarine,orange,papaya,quince,raspberry,strawberry,tomato,ugli_fruit,vanilla,watermelon,xigua,yellow_apple,zucchini".to_string();

        // Use 10% of iterations for warmup
        let warmup_iterations = iterations / 10;

        BenchmarkSuite {
            iterations,
            warmup_iterations,
            test_data,
            quiet,
        }
    }

    fn run_all_benchmarks(&self) -> Vec<BenchmarkResult> {
        let mut results = Vec::new();

        if !self.quiet {
            println!("Running comprehensive pipeline benchmarks...");
            println!("Warmup iterations: {}", self.warmup_iterations);
            println!("Measurement iterations: {}", self.iterations);
            println!("Test data size: {} characters", self.test_data.len());
            println!();
        }

        // Single operation benchmarks
        results.extend(self.run_single_operation_benchmarks());

        // Multiple simple operations benchmarks
        results.extend(self.run_multiple_simple_benchmarks());

        // Multiple operations with map benchmarks
        results.extend(self.run_multiple_map_benchmarks());

        // Complex nested operations benchmarks
        results.extend(self.run_complex_benchmarks());

        results
    }

    fn run_single_operation_benchmarks(&self) -> Vec<BenchmarkResult> {
        if !self.quiet {
            println!("🔸 Running single operation benchmarks...");
        }

        let benchmarks = vec![
            ("Single: split", "{split:,:..|join:,}"),
            ("Single: upper", "{upper}"),
            ("Single: lower", "{lower}"),
            ("Single: trim", "{trim}"),
            ("Single: reverse", "{reverse}"),
            ("Single: sort", "{split:,:..|sort|join:,}"),
            ("Single: unique", "{split:,:..|unique|join:,}"),
            ("Single: replace", "{replace:s/a/A/g}"),
            ("Single: filter", "{split:,:..|filter:^[a-m]|join:,}"),
        ];

        benchmarks
            .into_iter()
            .map(|(name, template_str)| {
                if !self.quiet {
                    print!("  {name} ... ");
                }
                let result = self.benchmark_template(name, template_str);
                if !self.quiet {
                    println!("✓ avg: {:?}", result.average_time);
                }
                result
            })
            .collect()
    }

    fn run_multiple_simple_benchmarks(&self) -> Vec<BenchmarkResult> {
        if !self.quiet {
            println!("\n🔸 Running multiple simple operations benchmarks...");
        }

        let benchmarks = vec![
            ("Multi: split + join", "{split:,:..|join: }"),
            ("Multi: split + sort + join", "{split:,:..|sort|join:;}"),
            ("Multi: split + unique + join", "{split:,:..|unique|join:,}"),
            (
                "Multi: split + reverse + join",
                "{split:,:..|reverse|join:-}",
            ),
            (
                "Multi: split + filter + join",
                "{split:,:..|filter:^[a-m]|join:,}",
            ),
            (
                "Multi: split + slice + join",
                "{split:,:..|slice:0..5|join:&}",
            ),
            (
                "Multi: upper + trim + replace",
                "{upper|trim|replace:s/,/ /g}",
            ),
            (
                "Multi: split + sort + unique + join",
                "{split:,:..|sort|unique|join:+}",
            ),
        ];

        benchmarks
            .into_iter()
            .map(|(name, template_str)| {
                if !self.quiet {
                    print!("  {name} ... ");
                }
                let result = self.benchmark_template(name, template_str);
                if !self.quiet {
                    println!("✓ avg: {:?}", result.average_time);
                }
                result
            })
            .collect()
    }

    fn run_multiple_map_benchmarks(&self) -> Vec<BenchmarkResult> {
        if !self.quiet {
            println!("\n🔸 Running multiple operations with map benchmarks...");
        }

        let benchmarks = vec![
            (
                "Map: split + map(upper) + join",
                "{split:,:..|map:{upper}|join:,}",
            ),
            (
                "Map: split + map(trim+upper) + join",
                "{split:,:..|map:{trim|upper}|join: }",
            ),
            (
                "Map: split + map(prepend) + join",
                "{split:,:..|map:{prepend:item}|join:,}",
            ),
            (
                "Map: split + map(append) + join",
                "{split:,:..|map:{append:-fruit}|join:;}",
            ),
            (
                "Map: split + map(reverse) + join",
                "{split:,:..|map:{reverse}|join:,}",
            ),
            (
                "Map: split + map(substring) + join",
                "{split:,:..|map:{substring:0..3}|join: }",
            ),
            (
                "Map: split + map(pad) + join",
                "{split:,:..|map:{pad:10:_}|join:,}",
            ),
            (
                "Map: split + map(replace) + join",
                "{split:,:..|map:{replace:s/e/E/g}|join:,}",
            ),
        ];

        benchmarks
            .into_iter()
            .map(|(name, template_str)| {
                if !self.quiet {
                    print!("  {name} ... ");
                }
                let result = self.benchmark_template(name, template_str);
                if !self.quiet {
                    println!("✓ avg: {:?}", result.average_time);
                }
                result
            })
            .collect()
    }

    fn run_complex_benchmarks(&self) -> Vec<BenchmarkResult> {
        if !self.quiet {
            println!("\n🔸 Running complex nested operations benchmarks...");
        }

        let benchmarks = vec![
            (
                "Complex: split + map(split+join) + join",
                "{split:,:..|map:{split:_:..|join:-}|join: }",
            ),
            (
                "Complex: split + map(upper+substring) + join",
                "{split:,:..|map:{upper|substring:0..5}|join:,}",
            ),
            (
                "Complex: split + filter + map(reverse) + join",
                "{split:,:..|filter:^[a-m]|map:{reverse}|join:&}",
            ),
            (
                "Complex: split + map(upper+replace) + join",
                "{split:,:..|map:{upper|replace:s/A/a/g}|join:;}",
            ),
            (
                "Complex: split + unique + map(upper) + join",
                "{split:,:..|unique|map:{upper}|join:,}",
            ),
            (
                "Complex: split + map(replace+upper)",
                "{split:,:..|map:{replace:s/a/A/g|upper}|join:,}",
            ),
            (
                "Complex: map with substring and pad",
                "{split:,:..|map:{substring:0..3|pad:5:_}|join:+}",
            ),
            (
                "Complex: multi-level processing",
                "{split:,:..|filter:^[a-z]|map:{upper}|sort|join: }",
            ),
        ];

        benchmarks
            .into_iter()
            .map(|(name, template_str)| {
                if !self.quiet {
                    print!("  {name} ... ");
                }
                let result = self.benchmark_template(name, template_str);
                if !self.quiet {
                    println!("✓ avg: {:?}", result.average_time);
                }
                result
            })
            .collect()
    }

    fn benchmark_template(&self, name: &str, template_str: &str) -> BenchmarkResult {
        let template = Template::parse(template_str)
            .unwrap_or_else(|e| panic!("Failed to parse template '{template_str}': {e}"));

        // Warmup phase - run operations without timing to warm up caches and system state
        for _ in 0..self.warmup_iterations {
            let _ = template
                .format(&self.test_data)
                .unwrap_or_else(|e| panic!("Failed to execute template '{template_str}': {e}"));
        }

        // Actual measurement phase
        let mut times = Vec::with_capacity(self.iterations);

        for _ in 0..self.iterations {
            let start = Instant::now();
            let _ = template
                .format(&self.test_data)
                .unwrap_or_else(|e| panic!("Failed to execute template '{template_str}': {e}"));
            let duration = start.elapsed();
            times.push(duration);
        }

        BenchmarkResult::new(name.to_string(), times)
    }
}

fn format_duration(duration: Duration) -> String {
    let nanos = duration.as_nanos();
    if nanos < 1_000 {
        format!("{nanos}ns")
    } else if nanos < 1_000_000 {
        format!("{:.2}μs", nanos as f64 / 1_000.0)
    } else if nanos < 1_000_000_000 {
        format!("{:.2}ms", nanos as f64 / 1_000_000.0)
    } else {
        format!("{:.2}s", duration.as_secs_f64())
    }
}

fn print_text_report(results: &[BenchmarkResult], total_time: Duration, warmup_iterations: usize) {
    println!("\n{}", "=".repeat(80));
    println!("                          BENCHMARK RESULTS");
    println!("{}", "=".repeat(80));

    println!("\n📊 Summary:");
    println!("• Total benchmarks run: {}", results.len());
    println!("• Total execution time: {}", format_duration(total_time));
    println!(
        "• Measurement iterations per benchmark: {}",
        results.first().map(|r| r.iterations).unwrap_or(0)
    );
    println!("• Warmup iterations per benchmark: {warmup_iterations} (10% of measurements)");

    println!("\n📈 Detailed Results:");
    println!(
        "{:<50} {:>12} {:>12} {:>12}",
        "Benchmark", "Average", "Min", "Max"
    );
    println!("{}", "-".repeat(88));

    for result in results {
        println!(
            "{:<50} {:>12} {:>12} {:>12}",
            result.name,
            format_duration(result.average_time),
            format_duration(result.min_time),
            format_duration(result.max_time)
        );
    }

    // Category analysis
    let mut categories: HashMap<&str, Vec<&BenchmarkResult>> = HashMap::new();
    for result in results {
        let category = if result.name.starts_with("Single:") {
            "Single Operations"
        } else if result.name.starts_with("Multi:") {
            "Multiple Simple Operations"
        } else if result.name.starts_with("Map:") {
            "Map Operations"
        } else if result.name.starts_with("Complex:") {
            "Complex Operations"
        } else {
            "Other"
        };
        categories.entry(category).or_default().push(result);
    }

    println!("\n📋 Performance by Category:");
    println!("{}", "-".repeat(80));

    for (category, category_results) in categories {
        let avg_time: Duration = category_results
            .iter()
            .map(|r| r.average_time)
            .sum::<Duration>()
            / category_results.len() as u32;

        let fastest = category_results
            .iter()
            .min_by_key(|r| r.average_time)
            .unwrap();

        let slowest = category_results
            .iter()
            .max_by_key(|r| r.average_time)
            .unwrap();

        println!("🔹 {} ({} tests)", category, category_results.len());
        println!(
            "   Average: {} | Fastest: {} ({}) | Slowest: {} ({})",
            format_duration(avg_time),
            format_duration(fastest.average_time),
            fastest.name,
            format_duration(slowest.average_time),
            slowest.name
        );
        println!();
    }
}

fn main() {
    let matches = Command::new("String Pipeline Benchmark")
        .version(env!("CARGO_PKG_VERSION"))
        .about("Comprehensive benchmarking tool for string pipeline operations")
        .arg(
            Arg::new("iterations")
                .short('n')
                .long("iterations")
                .value_name("COUNT")
                .help("Number of iterations to run for each benchmark")
                .default_value("1000"),
        )
        .get_matches();

    let iterations: usize = matches
        .get_one::<String>("iterations")
        .unwrap()
        .parse()
        .expect("Invalid number of iterations");

    if iterations < 10 {
        eprintln!("Warning: Running with less than 10 iterations may produce unreliable results");
    }

    let suite = BenchmarkSuite::new(iterations, false);
    let start_time = Instant::now();
    let results = suite.run_all_benchmarks();
    let total_time = start_time.elapsed();

    print_text_report(&results, total_time, suite.warmup_iterations);
}