#![feature(test)] extern crate geo_types; extern crate geojson; extern crate sfcgal; extern crate test; use sfcgal::{FromGeoJSON, SFCGeometry, ToCoordinates, ToGeoJSON, TryInto}; use std::convert::TryFrom; use std::io::Read; use test::Bencher; type Point2d = (f64, f64); type Point3d = (f64, f64, f64); fn read_example_file() -> Vec { let path = "examples/abc.geojson"; let mut file = std::fs::File::open(path).unwrap_or_else(|err| { println!("Unable to open layer at path: \"{}\"\nError: {}", path, err); std::process::exit(1) }); let mut raw_json = String::new(); file.read_to_string(&mut raw_json).unwrap(); let decoded_geojson = raw_json.parse::().unwrap(); let features = match decoded_geojson { geojson::GeoJson::FeatureCollection(ref collection) => &collection.features, _ => panic!("Error: expected a Feature collection!"), }; let mut geoms = Vec::new(); features .iter() .map(|feature| { if feature.geometry.is_some() { geoms.push(feature.geometry.clone().take().unwrap()); } }) .for_each(drop); geoms } fn make_sfgal_geom() -> SFCGeometry { let geoms = read_example_file(); SFCGeometry::from_geojson::(&geoms[0].value).unwrap() } // #[bench] // fn bench_geojson_to_geotypes_to_sfcgal_2d(b: &mut Bencher) { // let mut geom = read_example_file(); // let g = geom.get_mut(0).unwrap(); // b.iter(|| { // let geo_polygon: geo_types::Polygon = g.value.clone().try_into().unwrap(); // geo_polygon.to_sfcgal().unwrap(); // }); // } // // #[bench] // fn bench_geotypes_to_sfcgal_2d(b: &mut Bencher) { // let mut geom = read_example_file(); // let g = geom.get_mut(0).unwrap(); // let geo_polygon: geo_types::Polygon = g.value.clone().try_into().unwrap(); // b.iter(|| { // geo_polygon.to_sfcgal().unwrap(); // }); // } #[bench] fn bench_geojson_to_sfcgal_3d(b: &mut Bencher) { let geom = read_example_file(); b.iter(|| SFCGeometry::from_geojson::(&geom[0].value).unwrap()); } #[bench] fn bench_geojson_to_sfcgal_2d(b: &mut Bencher) { let geom = read_example_file(); b.iter(|| SFCGeometry::from_geojson::(&geom[0].value).unwrap()); } #[bench] fn bench_sfcgal_2d_to_coordinates(b: &mut Bencher) { let geom = make_sfgal_geom(); b.iter(|| geom.to_coordinates::().unwrap()); } #[bench] fn bench_sfcgal_2d_to_geojson(b: &mut Bencher) { let geom = make_sfgal_geom(); b.iter(|| { let _coords = geom.to_geojson::().unwrap(); }); } #[bench] fn bench_sfcgal_2d_to_geotype_geometry(b: &mut Bencher) { let geom = make_sfgal_geom(); b.iter(|| { let _polyg: geo_types::Geometry = TryInto::try_into(geom.clone()).unwrap(); }); } #[bench] fn bench_sfcgal_2d_to_geotype_concrete_type(b: &mut Bencher) { let geom = make_sfgal_geom(); b.iter(|| { let _polyg: geo_types::Polygon = geo_types::Polygon::try_from(TryInto::try_into(geom.clone()).unwrap()).unwrap(); }); }