|
| 1 | +import datetime |
| 2 | +import json |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +from pathlib import Path |
| 7 | +from tempfile import TemporaryDirectory |
| 8 | +from pystac import ( |
| 9 | + Catalog, |
| 10 | + StacIO, |
| 11 | + Collection, |
| 12 | + Extent, |
| 13 | + TemporalExtent, |
| 14 | + SpatialExtent, |
| 15 | + Item, |
| 16 | +) |
| 17 | + |
| 18 | +from ._base import Bench |
| 19 | +from ._util import get_data_path |
| 20 | + |
| 21 | + |
| 22 | +class CatalogBench(Bench): |
| 23 | + def setup(self) -> None: |
| 24 | + self.temp_dir = tempfile.mkdtemp() |
| 25 | + |
| 26 | + self.stac_io = StacIO.default() |
| 27 | + |
| 28 | + self.catalog_path = get_data_path("examples/1.0.0/catalog.json") |
| 29 | + with open(self.catalog_path) as src: |
| 30 | + self.catalog_dict = json.load(src) |
| 31 | + self.catalog = Catalog.from_file(self.catalog_path) |
| 32 | + |
| 33 | + def teardown(self) -> None: |
| 34 | + shutil.rmtree(self.temp_dir, ignore_errors=True) |
| 35 | + |
| 36 | + def time_catalog_from_file(self) -> None: |
| 37 | + """Deserialize an Item from file""" |
| 38 | + _ = Catalog.from_file(self.catalog_path) |
| 39 | + |
| 40 | + def time_catalog_from_dict(self) -> None: |
| 41 | + """Deserialize an Item from dictionary.""" |
| 42 | + _ = Catalog.from_dict(self.catalog_dict) |
| 43 | + |
| 44 | + def time_catalog_to_dict(self) -> None: |
| 45 | + """Serialize an Item to a dictionary.""" |
| 46 | + self.catalog.to_dict(include_self_link=True) |
| 47 | + |
| 48 | + def time_catalog_save(self) -> None: |
| 49 | + """Serialize an Item to a JSON file.""" |
| 50 | + self.catalog.save_object( |
| 51 | + include_self_link=True, |
| 52 | + dest_href=os.path.join(self.temp_dir, "time_catalog_save.json"), |
| 53 | + stac_io=self.stac_io, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +class WalkCatalogBench(Bench): |
| 58 | + def setup_cache(self) -> Catalog: |
| 59 | + return make_large_catalog() |
| 60 | + |
| 61 | + def time_walk(self, catalog: Catalog) -> None: |
| 62 | + for ( |
| 63 | + _, |
| 64 | + _, |
| 65 | + _, |
| 66 | + ) in catalog.walk(): |
| 67 | + pass |
| 68 | + |
| 69 | + def peakmem_walk(self, catalog: Catalog) -> None: |
| 70 | + for ( |
| 71 | + _, |
| 72 | + _, |
| 73 | + _, |
| 74 | + ) in catalog.walk(): |
| 75 | + pass |
| 76 | + |
| 77 | + |
| 78 | +class ReadCatalogBench(Bench): |
| 79 | + def setup(self) -> None: |
| 80 | + catalog = make_large_catalog() |
| 81 | + self.temporary_directory = TemporaryDirectory() |
| 82 | + self.path = str(Path(self.temporary_directory.name) / "catalog.json") |
| 83 | + catalog.normalize_and_save(self.temporary_directory.name) |
| 84 | + |
| 85 | + def teardown(self) -> None: |
| 86 | + shutil.rmtree(self.temporary_directory.name) |
| 87 | + |
| 88 | + def time_read_and_walk(self) -> None: |
| 89 | + catalog = Catalog.from_file(self.path) |
| 90 | + for _, _, _ in catalog.walk(): |
| 91 | + pass |
| 92 | + |
| 93 | + |
| 94 | +class WriteCatalogBench(Bench): |
| 95 | + def setup(self) -> None: |
| 96 | + self.catalog = make_large_catalog() |
| 97 | + self.temporary_directory = TemporaryDirectory() |
| 98 | + |
| 99 | + def teardown(self) -> None: |
| 100 | + shutil.rmtree(self.temporary_directory.name) |
| 101 | + |
| 102 | + def time_normalize_and_save(self) -> None: |
| 103 | + self.catalog.normalize_and_save(self.temporary_directory.name) |
| 104 | + |
| 105 | + |
| 106 | +def make_large_catalog() -> Catalog: |
| 107 | + catalog = Catalog("an-id", "a description") |
| 108 | + extent = Extent( |
| 109 | + SpatialExtent([[-180.0, -90.0, 180.0, 90.0]]), |
| 110 | + TemporalExtent([[datetime.datetime(2023, 1, 1), None]]), |
| 111 | + ) |
| 112 | + for i in range(0, 10): |
| 113 | + collection = Collection(f"collection-{i}", f"Collection {i}", extent) |
| 114 | + for j in range(0, 100): |
| 115 | + item = Item(f"item-{i}-{j}", None, None, datetime.datetime.now(), {}) |
| 116 | + collection.add_item(item) |
| 117 | + catalog.add_child(collection) |
| 118 | + return catalog |
0 commit comments