oxigdal-bench 0.1.4

Comprehensive performance profiling and benchmarking suite for OxiGDAL
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
//! I/O performance benchmark scenarios.
//!
//! This module provides benchmark scenarios for I/O operations including:
//! - Sequential read/write performance
//! - Random access patterns
//! - Chunked I/O operations
//! - Different file formats
//! - Compression impact on I/O

use crate::error::{BenchError, Result};
use crate::scenarios::BenchmarkScenario;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::PathBuf;

/// Sequential read benchmark scenario.
pub struct SequentialReadScenario {
    input_path: PathBuf,
    buffer_size: usize,
    total_bytes_read: usize,
}

impl SequentialReadScenario {
    /// Creates a new sequential read benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
            buffer_size: 8192,
            total_bytes_read: 0,
        }
    }

    /// Sets the buffer size for reading.
    pub fn with_buffer_size(mut self, size: usize) -> Self {
        self.buffer_size = size;
        self
    }
}

impl BenchmarkScenario for SequentialReadScenario {
    fn name(&self) -> &str {
        "sequential_read"
    }

    fn description(&self) -> &str {
        "Benchmark sequential file reading performance"
    }

    fn setup(&mut self) -> Result<()> {
        if !self.input_path.exists() {
            return Err(BenchError::scenario_failed(
                self.name(),
                format!("Input file does not exist: {}", self.input_path.display()),
            ));
        }

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        let mut file = File::open(&self.input_path)?;
        let mut buffer = vec![0u8; self.buffer_size];
        self.total_bytes_read = 0;

        loop {
            let bytes_read = file.read(&mut buffer)?;
            if bytes_read == 0 {
                break;
            }
            self.total_bytes_read += bytes_read;
        }

        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        Ok(())
    }
}

/// Sequential write benchmark scenario.
pub struct SequentialWriteScenario {
    output_path: PathBuf,
    file_size: usize,
    buffer_size: usize,
    created: bool,
}

impl SequentialWriteScenario {
    /// Creates a new sequential write benchmark scenario.
    pub fn new<P: Into<PathBuf>>(output_path: P, file_size: usize) -> Self {
        Self {
            output_path: output_path.into(),
            file_size,
            buffer_size: 8192,
            created: false,
        }
    }

    /// Sets the buffer size for writing.
    pub fn with_buffer_size(mut self, size: usize) -> Self {
        self.buffer_size = size;
        self
    }
}

impl BenchmarkScenario for SequentialWriteScenario {
    fn name(&self) -> &str {
        "sequential_write"
    }

    fn description(&self) -> &str {
        "Benchmark sequential file writing performance"
    }

    fn setup(&mut self) -> Result<()> {
        if let Some(parent) = self.output_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        let mut file = File::create(&self.output_path)?;
        let buffer = vec![0u8; self.buffer_size];

        let mut remaining = self.file_size;
        while remaining > 0 {
            let to_write = remaining.min(self.buffer_size);
            file.write_all(&buffer[..to_write])?;
            remaining -= to_write;
        }

        file.sync_all()?;
        self.created = true;

        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        if self.created && self.output_path.exists() {
            std::fs::remove_file(&self.output_path)?;
        }
        Ok(())
    }
}

/// Random access read benchmark scenario.
pub struct RandomAccessScenario {
    input_path: PathBuf,
    access_count: usize,
    chunk_size: usize,
    file_size: u64,
}

impl RandomAccessScenario {
    /// Creates a new random access benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P, access_count: usize) -> Self {
        Self {
            input_path: input_path.into(),
            access_count,
            chunk_size: 4096,
            file_size: 0,
        }
    }

    /// Sets the chunk size for each random access.
    pub fn with_chunk_size(mut self, size: usize) -> Self {
        self.chunk_size = size;
        self
    }
}

impl BenchmarkScenario for RandomAccessScenario {
    fn name(&self) -> &str {
        "random_access"
    }

    fn description(&self) -> &str {
        "Benchmark random access read performance"
    }

    fn setup(&mut self) -> Result<()> {
        if !self.input_path.exists() {
            return Err(BenchError::scenario_failed(
                self.name(),
                format!("Input file does not exist: {}", self.input_path.display()),
            ));
        }

        self.file_size = std::fs::metadata(&self.input_path)?.len();

        if self.file_size < self.chunk_size as u64 {
            return Err(BenchError::scenario_failed(
                self.name(),
                "File too small for random access benchmark".to_string(),
            ));
        }

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        let mut file = File::open(&self.input_path)?;
        let mut buffer = vec![0u8; self.chunk_size];

        // Use a simple pseudo-random sequence for reproducibility
        let mut seed = 12345u64;
        for _ in 0..self.access_count {
            // Simple LCG for reproducible randomness
            seed = seed.wrapping_mul(1103515245).wrapping_add(12345);
            let max_offset = self.file_size.saturating_sub(self.chunk_size as u64);
            let offset = seed % max_offset.max(1);

            file.seek(SeekFrom::Start(offset))?;
            file.read_exact(&mut buffer)?;
        }

        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        Ok(())
    }
}

/// Chunked I/O benchmark scenario.
pub struct ChunkedIoScenario {
    input_path: PathBuf,
    output_path: PathBuf,
    chunk_sizes: Vec<usize>,
    created: bool,
}

impl ChunkedIoScenario {
    /// Creates a new chunked I/O benchmark scenario.
    pub fn new<P1, P2>(input_path: P1, output_path: P2) -> Self
    where
        P1: Into<PathBuf>,
        P2: Into<PathBuf>,
    {
        Self {
            input_path: input_path.into(),
            output_path: output_path.into(),
            chunk_sizes: vec![512, 1024, 4096, 8192, 16384, 65536],
            created: false,
        }
    }

    /// Sets the chunk sizes to benchmark.
    pub fn with_chunk_sizes(mut self, sizes: Vec<usize>) -> Self {
        self.chunk_sizes = sizes;
        self
    }
}

impl BenchmarkScenario for ChunkedIoScenario {
    fn name(&self) -> &str {
        "chunked_io"
    }

    fn description(&self) -> &str {
        "Benchmark different chunk sizes for I/O operations"
    }

    fn setup(&mut self) -> Result<()> {
        if !self.input_path.exists() {
            return Err(BenchError::scenario_failed(
                self.name(),
                format!("Input file does not exist: {}", self.input_path.display()),
            ));
        }

        if let Some(parent) = self.output_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        for &chunk_size in &self.chunk_sizes {
            let mut input = File::open(&self.input_path)?;
            let mut output = File::create(&self.output_path)?;
            let mut buffer = vec![0u8; chunk_size];

            loop {
                let bytes_read = input.read(&mut buffer)?;
                if bytes_read == 0 {
                    break;
                }
                output.write_all(&buffer[..bytes_read])?;
            }

            output.sync_all()?;
        }

        self.created = true;
        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        if self.created && self.output_path.exists() {
            std::fs::remove_file(&self.output_path)?;
        }
        Ok(())
    }
}

/// Buffered vs unbuffered I/O benchmark scenario.
pub struct BufferedIoScenario {
    input_path: PathBuf,
    use_buffering: bool,
    total_bytes: usize,
}

impl BufferedIoScenario {
    /// Creates a new buffered I/O benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P, use_buffering: bool) -> Self {
        Self {
            input_path: input_path.into(),
            use_buffering,
            total_bytes: 0,
        }
    }
}

impl BenchmarkScenario for BufferedIoScenario {
    fn name(&self) -> &str {
        if self.use_buffering {
            "buffered_io"
        } else {
            "unbuffered_io"
        }
    }

    fn description(&self) -> &str {
        "Benchmark buffered vs unbuffered I/O performance"
    }

    fn setup(&mut self) -> Result<()> {
        if !self.input_path.exists() {
            return Err(BenchError::scenario_failed(
                self.name(),
                format!("Input file does not exist: {}", self.input_path.display()),
            ));
        }

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        use std::io::BufReader;

        let file = File::open(&self.input_path)?;
        self.total_bytes = 0;

        if self.use_buffering {
            let mut reader = BufReader::new(file);
            let mut buffer = vec![0u8; 8192];
            loop {
                let bytes_read = reader.read(&mut buffer)?;
                if bytes_read == 0 {
                    break;
                }
                self.total_bytes += bytes_read;
            }
        } else {
            let mut reader = file;
            let mut buffer = vec![0u8; 8192];
            loop {
                let bytes_read = reader.read(&mut buffer)?;
                if bytes_read == 0 {
                    break;
                }
                self.total_bytes += bytes_read;
            }
        }

        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        Ok(())
    }
}

/// Memory-mapped I/O benchmark scenario.
pub struct MemoryMappedIoScenario {
    input_path: PathBuf,
    read_pattern: ReadPattern,
}

/// Read patterns for memory-mapped I/O.
#[derive(Debug, Clone, Copy)]
pub enum ReadPattern {
    /// Sequential read pattern.
    Sequential,
    /// Random read pattern.
    Random,
    /// Strided read pattern (every Nth byte).
    Strided(usize),
}

impl MemoryMappedIoScenario {
    /// Creates a new memory-mapped I/O benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
            read_pattern: ReadPattern::Sequential,
        }
    }

    /// Sets the read pattern.
    pub fn with_pattern(mut self, pattern: ReadPattern) -> Self {
        self.read_pattern = pattern;
        self
    }
}

impl BenchmarkScenario for MemoryMappedIoScenario {
    fn name(&self) -> &str {
        "memory_mapped_io"
    }

    fn description(&self) -> &str {
        "Benchmark memory-mapped file I/O performance"
    }

    fn setup(&mut self) -> Result<()> {
        if !self.input_path.exists() {
            return Err(BenchError::scenario_failed(
                self.name(),
                format!("Input file does not exist: {}", self.input_path.display()),
            ));
        }

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        // Note: This is a placeholder for memory-mapped I/O
        // Actual implementation would use memmap2 crate or similar

        // For now, we'll just read the file normally
        let mut file = File::open(&self.input_path)?;
        let mut buffer = Vec::new();
        file.read_to_end(&mut buffer)?;

        // Simulate different read patterns
        let _sum: u64 = match self.read_pattern {
            ReadPattern::Sequential => buffer.iter().map(|&b| b as u64).sum(),
            ReadPattern::Random => {
                let mut seed = 12345u64;
                let mut sum = 0u64;
                for _ in 0..buffer.len().min(10000) {
                    seed = seed.wrapping_mul(1103515245).wrapping_add(12345);
                    let idx = (seed as usize) % buffer.len();
                    sum = sum.wrapping_add(buffer[idx] as u64);
                }
                sum
            }
            ReadPattern::Strided(stride) => buffer.iter().step_by(stride).map(|&b| b as u64).sum(),
        };

        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        Ok(())
    }
}

/// Direct I/O benchmark scenario.
pub struct DirectIoScenario {
    input_path: PathBuf,
    alignment: usize,
}

impl DirectIoScenario {
    /// Creates a new direct I/O benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
            alignment: 4096,
        }
    }

    /// Sets the alignment requirement for direct I/O.
    pub fn with_alignment(mut self, alignment: usize) -> Self {
        self.alignment = alignment;
        self
    }
}

impl BenchmarkScenario for DirectIoScenario {
    fn name(&self) -> &str {
        "direct_io"
    }

    fn description(&self) -> &str {
        "Benchmark direct I/O (O_DIRECT) performance"
    }

    fn setup(&mut self) -> Result<()> {
        if !self.input_path.exists() {
            return Err(BenchError::scenario_failed(
                self.name(),
                format!("Input file does not exist: {}", self.input_path.display()),
            ));
        }

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        // Note: Direct I/O requires platform-specific APIs
        // This is a simplified version that just reads with aligned buffers

        let mut file = File::open(&self.input_path)?;
        let mut buffer = vec![0u8; self.alignment];

        loop {
            let bytes_read = file.read(&mut buffer)?;
            if bytes_read == 0 {
                break;
            }
        }

        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    fn create_test_file(path: &PathBuf, size: usize) -> std::io::Result<()> {
        let mut file = File::create(path)?;
        let data = vec![0u8; size];
        file.write_all(&data)?;
        file.sync_all()?;
        Ok(())
    }

    #[test]
    fn test_sequential_read_scenario_creation() {
        let scenario = SequentialReadScenario::new("/tmp/test.bin").with_buffer_size(16384);

        assert_eq!(scenario.name(), "sequential_read");
        assert_eq!(scenario.buffer_size, 16384);
    }

    #[test]
    fn test_sequential_write_scenario_creation() {
        let scenario =
            SequentialWriteScenario::new("/tmp/output.bin", 1024 * 1024).with_buffer_size(32768);

        assert_eq!(scenario.name(), "sequential_write");
        assert_eq!(scenario.buffer_size, 32768);
    }

    #[test]
    fn test_random_access_scenario_creation() {
        let scenario = RandomAccessScenario::new("/tmp/test.bin", 100).with_chunk_size(8192);

        assert_eq!(scenario.name(), "random_access");
        assert_eq!(scenario.chunk_size, 8192);
    }

    #[test]
    fn test_chunked_io_scenario() {
        let temp_dir = std::env::temp_dir();
        let input_path = temp_dir.join("test_chunked_input.bin");
        let output_path = temp_dir.join("test_chunked_output.bin");

        // Create test file
        create_test_file(&input_path, 10240).expect("Failed to create test file");

        let scenario = ChunkedIoScenario::new(&input_path, &output_path)
            .with_chunk_sizes(vec![512, 1024, 4096]);

        assert_eq!(scenario.name(), "chunked_io");
        assert_eq!(scenario.chunk_sizes.len(), 3);

        // Cleanup
        let _ = std::fs::remove_file(&input_path);
    }

    #[test]
    fn test_buffered_io_scenario_creation() {
        let scenario = BufferedIoScenario::new("/tmp/test.bin", true);
        assert_eq!(scenario.name(), "buffered_io");

        let scenario = BufferedIoScenario::new("/tmp/test.bin", false);
        assert_eq!(scenario.name(), "unbuffered_io");
    }
}