oxigdal-bench 0.1.7

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
//! Vector operation benchmark scenarios.
//!
//! This module provides benchmark scenarios for vector operations including:
//! - Reading and writing vector formats (GeoJSON, Shapefile, etc.)
//! - Geometry simplification
//! - Buffer operations
//! - Spatial indexing
//! - Geometric operations

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

/// GeoJSON read benchmark scenario.
pub struct GeoJsonReadScenario {
    input_path: PathBuf,
    feature_count: Option<usize>,
}

impl GeoJsonReadScenario {
    /// Creates a new GeoJSON read benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
            feature_count: None,
        }
    }

    /// Sets the expected feature count for validation.
    pub fn with_feature_count(mut self, count: usize) -> Self {
        self.feature_count = Some(count);
        self
    }
}

impl BenchmarkScenario for GeoJsonReadScenario {
    fn name(&self) -> &str {
        "geojson_read"
    }

    fn description(&self) -> &str {
        "Benchmark GeoJSON 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<()> {
        #[cfg(feature = "vector")]
        {
            // let reader = GeoJsonReader::open(&self.input_path)?;
            // let features = reader.read_all()?;

            // if let Some(expected) = self.feature_count {
            //     if features.len() != expected {
            //         return Err(BenchError::DataValidation(
            //             format!("Expected {} features, got {}", expected, features.len())
            //         ));
            //     }
            // }
        }

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

        Ok(())
    }

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

/// GeoJSON write benchmark scenario.
pub struct GeoJsonWriteScenario {
    output_path: PathBuf,
    #[allow(dead_code)]
    feature_count: usize,
    geometry_complexity: usize,
    created: bool,
}

impl GeoJsonWriteScenario {
    /// Creates a new GeoJSON write benchmark scenario.
    pub fn new<P: Into<PathBuf>>(output_path: P, feature_count: usize) -> Self {
        Self {
            output_path: output_path.into(),
            feature_count,
            geometry_complexity: 10,
            created: false,
        }
    }

    /// Sets the geometry complexity (number of coordinates per geometry).
    pub fn with_complexity(mut self, complexity: usize) -> Self {
        self.geometry_complexity = complexity;
        self
    }
}

impl BenchmarkScenario for GeoJsonWriteScenario {
    fn name(&self) -> &str {
        "geojson_write"
    }

    fn description(&self) -> &str {
        "Benchmark GeoJSON 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<()> {
        #[cfg(feature = "vector")]
        {
            // Generate test features
            // let features = generate_test_features(
            //     self.feature_count,
            //     self.geometry_complexity,
            // )?;

            // Write to GeoJSON
            // let writer = GeoJsonWriter::create(&self.output_path)?;
            // writer.write_features(&features)?;

            self.created = true;
        }

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

        Ok(())
    }

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

/// Geometry simplification benchmark scenario.
pub struct SimplificationScenario {
    input_path: PathBuf,
    #[allow(dead_code)]
    tolerance: f64,
    algorithm: SimplificationAlgorithm,
}

/// Simplification algorithms to benchmark.
#[derive(Debug, Clone, Copy)]
pub enum SimplificationAlgorithm {
    /// Douglas-Peucker algorithm.
    DouglasPeucker,
    /// Visvalingam-Whyatt algorithm.
    VisvalingamWhyatt,
    /// Topology-preserving simplification.
    TopologyPreserving,
}

impl SimplificationScenario {
    /// Creates a new simplification benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P, tolerance: f64) -> Self {
        Self {
            input_path: input_path.into(),
            tolerance,
            algorithm: SimplificationAlgorithm::DouglasPeucker,
        }
    }

    /// Sets the simplification algorithm.
    pub fn with_algorithm(mut self, algorithm: SimplificationAlgorithm) -> Self {
        self.algorithm = algorithm;
        self
    }
}

impl BenchmarkScenario for SimplificationScenario {
    fn name(&self) -> &str {
        "geometry_simplification"
    }

    fn description(&self) -> &str {
        "Benchmark geometry simplification algorithms"
    }

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

    #[allow(unreachable_code)]
    fn execute(&mut self) -> Result<()> {
        #[cfg(all(feature = "vector", feature = "algorithms"))]
        {
            // let reader = GeoJsonReader::open(&self.input_path)?;
            // let features = reader.read_all()?;

            // for feature in features {
            //     let simplified = match self.algorithm {
            //         SimplificationAlgorithm::DouglasPeucker => {
            //             simplify_douglas_peucker(&feature.geometry, self.tolerance)?
            //         }
            //         SimplificationAlgorithm::VisvalingamWhyatt => {
            //             simplify_visvalingam_whyatt(&feature.geometry, self.tolerance)?
            //         }
            //         SimplificationAlgorithm::TopologyPreserving => {
            //             simplify_topology_preserving(&feature.geometry, self.tolerance)?
            //         }
            //     };
            // }
        }

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

        Ok(())
    }

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

/// Buffer operation benchmark scenario.
pub struct BufferScenario {
    input_path: PathBuf,
    #[allow(dead_code)]
    buffer_distance: f64,
    resolution: usize,
}

impl BufferScenario {
    /// Creates a new buffer benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P, buffer_distance: f64) -> Self {
        Self {
            input_path: input_path.into(),
            buffer_distance,
            resolution: 16,
        }
    }

    /// Sets the buffer resolution (number of segments per quadrant).
    pub fn with_resolution(mut self, resolution: usize) -> Self {
        self.resolution = resolution;
        self
    }
}

impl BenchmarkScenario for BufferScenario {
    fn name(&self) -> &str {
        "geometry_buffer"
    }

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

        Ok(())
    }

    #[allow(unreachable_code)]
    fn execute(&mut self) -> Result<()> {
        #[cfg(all(feature = "vector", feature = "algorithms"))]
        {
            // let reader = GeoJsonReader::open(&self.input_path)?;
            // let features = reader.read_all()?;

            // for feature in features {
            //     let buffered = buffer(
            //         &feature.geometry,
            //         self.buffer_distance,
            //         self.resolution,
            //     )?;
            // }
        }

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

        Ok(())
    }

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

/// Spatial indexing benchmark scenario.
pub struct SpatialIndexScenario {
    input_path: PathBuf,
    index_type: SpatialIndexType,
    query_count: usize,
}

/// Spatial index types to benchmark.
#[derive(Debug, Clone, Copy)]
pub enum SpatialIndexType {
    /// R-tree index.
    RTree,
    /// Quadtree index.
    Quadtree,
    /// KD-tree index.
    KdTree,
}

impl SpatialIndexScenario {
    /// Creates a new spatial indexing benchmark scenario.
    pub fn new<P: Into<PathBuf>>(input_path: P) -> Self {
        Self {
            input_path: input_path.into(),
            index_type: SpatialIndexType::RTree,
            query_count: 1000,
        }
    }

    /// Sets the index type.
    pub fn with_index_type(mut self, index_type: SpatialIndexType) -> Self {
        self.index_type = index_type;
        self
    }

    /// Sets the number of queries to perform.
    pub fn with_query_count(mut self, count: usize) -> Self {
        self.query_count = count;
        self
    }
}

impl BenchmarkScenario for SpatialIndexScenario {
    fn name(&self) -> &str {
        "spatial_indexing"
    }

    fn description(&self) -> &str {
        "Benchmark spatial index construction and query 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(())
    }

    #[allow(unreachable_code)]
    fn execute(&mut self) -> Result<()> {
        #[cfg(all(feature = "vector", feature = "algorithms"))]
        {
            // let reader = GeoJsonReader::open(&self.input_path)?;
            // let features = reader.read_all()?;

            // Build index
            // let index = match self.index_type {
            //     SpatialIndexType::RTree => build_rtree_index(&features)?,
            //     SpatialIndexType::Quadtree => build_quadtree_index(&features)?,
            //     SpatialIndexType::KdTree => build_kdtree_index(&features)?,
            // };

            // Perform queries
            // for _ in 0..self.query_count {
            //     let bbox = generate_random_bbox();
            //     let results = index.query(&bbox)?;
            // }
        }

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

        Ok(())
    }

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

/// Intersection operation benchmark scenario.
pub struct IntersectionScenario {
    input_path1: PathBuf,
    input_path2: PathBuf,
}

impl IntersectionScenario {
    /// Creates a new intersection benchmark scenario.
    pub fn new<P1, P2>(input_path1: P1, input_path2: P2) -> Self
    where
        P1: Into<PathBuf>,
        P2: Into<PathBuf>,
    {
        Self {
            input_path1: input_path1.into(),
            input_path2: input_path2.into(),
        }
    }
}

impl BenchmarkScenario for IntersectionScenario {
    fn name(&self) -> &str {
        "geometry_intersection"
    }

    fn description(&self) -> &str {
        "Benchmark geometry intersection operations"
    }

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

        if !self.input_path2.exists() {
            return Err(BenchError::scenario_failed(
                self.name(),
                format!(
                    "Input file 2 does not exist: {}",
                    self.input_path2.display()
                ),
            ));
        }

        Ok(())
    }

    #[allow(unreachable_code)]
    fn execute(&mut self) -> Result<()> {
        #[cfg(all(feature = "vector", feature = "algorithms"))]
        {
            // let reader1 = GeoJsonReader::open(&self.input_path1)?;
            // let features1 = reader1.read_all()?;

            // let reader2 = GeoJsonReader::open(&self.input_path2)?;
            // let features2 = reader2.read_all()?;

            // for f1 in &features1 {
            //     for f2 in &features2 {
            //         let intersection = intersect(&f1.geometry, &f2.geometry)?;
            //     }
            // }
        }

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

        Ok(())
    }

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

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

    #[test]
    fn test_geojson_read_scenario_creation() {
        let scenario = GeoJsonReadScenario::new(std::env::temp_dir().join("test.geojson"))
            .with_feature_count(100);

        assert_eq!(scenario.name(), "geojson_read");
        assert_eq!(scenario.feature_count, Some(100));
    }

    #[test]
    fn test_simplification_scenario_creation() {
        let scenario =
            SimplificationScenario::new(std::env::temp_dir().join("test.geojson"), 0.001)
                .with_algorithm(SimplificationAlgorithm::VisvalingamWhyatt);

        assert_eq!(scenario.name(), "geometry_simplification");
        assert_eq!(scenario.tolerance, 0.001);
    }

    #[test]
    fn test_buffer_scenario_creation() {
        let scenario = BufferScenario::new(std::env::temp_dir().join("test.geojson"), 10.0)
            .with_resolution(32);

        assert_eq!(scenario.name(), "geometry_buffer");
        assert_eq!(scenario.resolution, 32);
    }

    #[test]
    fn test_spatial_index_scenario_creation() {
        let scenario = SpatialIndexScenario::new(std::env::temp_dir().join("test.geojson"))
            .with_index_type(SpatialIndexType::Quadtree)
            .with_query_count(500);

        assert_eq!(scenario.name(), "spatial_indexing");
        assert_eq!(scenario.query_count, 500);
    }
}