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
//! Raster operation benchmark scenarios.
//!
//! This module provides benchmark scenarios for raster operations including:
//! - Reading and writing GeoTIFF files
//! - Raster reprojection
//! - Band operations
//! - COG operations
//! - Compression benchmarks

use crate::error::{BenchError, Result};
use crate::scenarios::BenchmarkScenario;
use std::collections::HashMap;
use std::path::PathBuf;

/// GeoTIFF read benchmark scenario.
pub struct GeoTiffReadScenario {
    input_path: PathBuf,
    tile_size: Option<(usize, usize)>,
    #[allow(dead_code)]
    metadata: HashMap<String, String>,
}

impl GeoTiffReadScenario {
    /// Creates a new GeoTIFF read benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
            tile_size: None,
            metadata: HashMap::new(),
        }
    }

    /// Sets the tile size for tiled reading.
    pub fn with_tile_size(mut self, width: usize, height: usize) -> Self {
        self.tile_size = Some((width, height));
        self
    }
}

impl BenchmarkScenario for GeoTiffReadScenario {
    fn name(&self) -> &str {
        "geotiff_read"
    }

    fn description(&self) -> &str {
        "Benchmark GeoTIFF 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<()> {
        // This is a placeholder - actual implementation would use oxigdal-geotiff
        // when the feature is enabled

        #[cfg(feature = "raster")]
        {
            use oxigdal_core::io::FileDataSource;
            use oxigdal_geotiff::GeoTiffReader;

            let data_source = FileDataSource::open(&self.input_path).map_err(|e| {
                BenchError::scenario_failed(self.name(), format!("Failed to open file: {e}"))
            })?;
            let _reader = GeoTiffReader::open(data_source).map_err(|e| {
                BenchError::scenario_failed(self.name(), format!("Failed to open GeoTIFF: {e}"))
            })?;

            // Read metadata
            // let metadata = reader.metadata()?;

            // Read data based on tile size
            // if let Some((width, height)) = self.tile_size {
            //     // Read tiles
            // } else {
            //     // Read entire raster
            // }
        }

        #[cfg(not(feature = "raster"))]
        {
            return Err(BenchError::missing_dependency("oxigdal-geotiff", "raster"));
        }

        Ok(())
    }

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

/// GeoTIFF write benchmark scenario.
pub struct GeoTiffWriteScenario {
    output_path: PathBuf,
    #[allow(dead_code)]
    width: usize,
    #[allow(dead_code)]
    height: usize,
    compression: String,
    created: bool,
}

impl GeoTiffWriteScenario {
    /// Creates a new GeoTIFF write benchmark scenario.
    pub fn new<P: Into<PathBuf>>(output_path: P, width: usize, height: usize) -> Self {
        Self {
            output_path: output_path.into(),
            width,
            height,
            compression: "none".to_string(),
            created: false,
        }
    }

    /// Sets the compression method.
    pub fn with_compression<S: Into<String>>(mut self, compression: S) -> Self {
        self.compression = compression.into();
        self
    }
}

impl BenchmarkScenario for GeoTiffWriteScenario {
    fn name(&self) -> &str {
        "geotiff_write"
    }

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

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

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        #[cfg(feature = "raster")]
        {
            // Placeholder for actual implementation
            // Create test data
            // let data = vec![0u16; self.width * self.height];

            // Write GeoTIFF
            // let writer = GeoTiffWriter::create(&self.output_path)?;
            // writer.set_compression(&self.compression)?;
            // writer.write_band(&data, self.width, self.height)?;

            self.created = true;
        }

        #[cfg(not(feature = "raster"))]
        {
            return Err(BenchError::missing_dependency("oxigdal-geotiff", "raster"));
        }

        Ok(())
    }

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

/// Raster reprojection benchmark scenario.
pub struct RasterReprojectionScenario {
    input_path: PathBuf,
    output_path: PathBuf,
    #[allow(dead_code)]
    target_crs: String,
    created: bool,
}

impl RasterReprojectionScenario {
    /// Creates a new raster reprojection benchmark scenario.
    pub fn new<P1, P2, S>(input_path: P1, output_path: P2, target_crs: S) -> Self
    where
        P1: Into<PathBuf>,
        P2: Into<PathBuf>,
        S: Into<String>,
    {
        Self {
            input_path: input_path.into(),
            output_path: output_path.into(),
            target_crs: target_crs.into(),
            created: false,
        }
    }
}

impl BenchmarkScenario for RasterReprojectionScenario {
    fn name(&self) -> &str {
        "raster_reprojection"
    }

    fn description(&self) -> &str {
        "Benchmark raster reprojection 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()),
            ));
        }

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

        Ok(())
    }

    #[allow(unreachable_code)]
    fn execute(&mut self) -> Result<()> {
        #[cfg(all(feature = "raster", feature = "algorithms"))]
        {
            // Placeholder for actual reprojection
            // let reader = GeoTiffReader::open(&self.input_path)?;
            // let data = reader.read_all()?;
            // let reprojected = reproject(data, &self.target_crs)?;
            // let writer = GeoTiffWriter::create(&self.output_path)?;
            // writer.write(&reprojected)?;

            self.created = true;
        }

        #[cfg(not(all(feature = "raster", feature = "algorithms")))]
        {
            return Err(BenchError::missing_dependency(
                "oxigdal reprojection",
                "raster and algorithms",
            ));
        }

        #[allow(unreachable_code)]
        Ok(())
    }

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

/// COG validation benchmark scenario.
pub struct CogValidationScenario {
    input_path: PathBuf,
}

impl CogValidationScenario {
    /// Creates a new COG validation benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
        }
    }
}

impl BenchmarkScenario for CogValidationScenario {
    fn name(&self) -> &str {
        "cog_validation"
    }

    fn description(&self) -> &str {
        "Benchmark Cloud-Optimized GeoTIFF validation 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<()> {
        #[cfg(feature = "raster")]
        {
            // Placeholder for COG validation
            // let validator = CogValidator::new();
            // let report = validator.validate(&self.input_path)?;
        }

        #[cfg(not(feature = "raster"))]
        {
            return Err(BenchError::missing_dependency(
                "oxigdal-geotiff COG",
                "raster",
            ));
        }

        Ok(())
    }

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

/// Raster compression benchmark scenario.
pub struct CompressionBenchmarkScenario {
    input_path: PathBuf,
    output_dir: PathBuf,
    compression_methods: Vec<String>,
    created_files: Vec<PathBuf>,
}

impl CompressionBenchmarkScenario {
    /// Creates a new compression benchmark scenario.
    pub fn new<P1, P2>(input_path: P1, output_dir: P2) -> Self
    where
        P1: Into<PathBuf>,
        P2: Into<PathBuf>,
    {
        Self {
            input_path: input_path.into(),
            output_dir: output_dir.into(),
            compression_methods: vec![
                "none".to_string(),
                "lzw".to_string(),
                "deflate".to_string(),
                "zstd".to_string(),
            ],
            created_files: Vec::new(),
        }
    }

    /// Sets the compression methods to benchmark.
    pub fn with_methods(mut self, methods: Vec<String>) -> Self {
        self.compression_methods = methods;
        self
    }
}

impl BenchmarkScenario for CompressionBenchmarkScenario {
    fn name(&self) -> &str {
        "raster_compression"
    }

    fn description(&self) -> &str {
        "Benchmark different raster compression methods"
    }

    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()),
            ));
        }

        std::fs::create_dir_all(&self.output_dir)?;

        Ok(())
    }

    fn execute(&mut self) -> Result<()> {
        #[cfg(feature = "raster")]
        {
            // Read input data once
            // let reader = GeoTiffReader::open(&self.input_path)?;
            // let data = reader.read_all()?;

            // Benchmark each compression method
            for method in &self.compression_methods {
                let output_path = self.output_dir.join(format!("compressed_{method}.tif"));

                // Write with compression
                // let writer = GeoTiffWriter::create(&output_path)?;
                // writer.set_compression(method)?;
                // writer.write(&data)?;

                self.created_files.push(output_path);
            }
        }

        #[cfg(not(feature = "raster"))]
        {
            return Err(BenchError::missing_dependency("oxigdal-geotiff", "raster"));
        }

        Ok(())
    }

    fn teardown(&mut self) -> Result<()> {
        for path in &self.created_files {
            if path.exists() {
                let _ = std::fs::remove_file(path);
            }
        }
        self.created_files.clear();
        Ok(())
    }
}

/// Band statistics calculation benchmark scenario.
pub struct BandStatisticsScenario {
    input_path: PathBuf,
    band_count: usize,
}

impl BandStatisticsScenario {
    /// Creates a new band statistics benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
            band_count: 1,
        }
    }

    /// Sets the number of bands to process.
    pub fn with_band_count(mut self, count: usize) -> Self {
        self.band_count = count;
        self
    }
}

impl BenchmarkScenario for BandStatisticsScenario {
    fn name(&self) -> &str {
        "band_statistics"
    }

    fn description(&self) -> &str {
        "Benchmark raster band statistics calculation"
    }

    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<()> {
        #[cfg(feature = "raster")]
        {
            // let reader = GeoTiffReader::open(&self.input_path)?;

            // for band_idx in 0..self.band_count {
            //     let data = reader.read_band(band_idx)?;
            //     let stats = calculate_statistics(&data)?;
            // }
        }

        #[cfg(not(feature = "raster"))]
        {
            return Err(BenchError::missing_dependency("oxigdal-geotiff", "raster"));
        }

        Ok(())
    }

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

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

    #[test]
    fn test_geotiff_read_scenario_creation() {
        let scenario = GeoTiffReadScenario::new("/tmp/test.tif").with_tile_size(256, 256);

        assert_eq!(scenario.name(), "geotiff_read");
        assert_eq!(scenario.tile_size, Some((256, 256)));
    }

    #[test]
    fn test_geotiff_write_scenario_creation() {
        let scenario =
            GeoTiffWriteScenario::new("/tmp/output.tif", 512, 512).with_compression("lzw");

        assert_eq!(scenario.name(), "geotiff_write");
        assert_eq!(scenario.compression, "lzw");
    }

    #[test]
    fn test_compression_benchmark_creation() {
        let scenario = CompressionBenchmarkScenario::new("/tmp/input.tif", "/tmp/output");

        assert_eq!(scenario.name(), "raster_compression");
        assert!(!scenario.compression_methods.is_empty());
    }
}